anyconv-video-basic 1.0.1__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- anyconv_video_basic/__init__.py +5 -0
- anyconv_video_basic/anyconv.module.yaml +186 -0
- anyconv_video_basic/ffmpeg.py +100 -0
- anyconv_video_basic/module.py +312 -0
- anyconv_video_basic-1.0.1.dist-info/METADATA +66 -0
- anyconv_video_basic-1.0.1.dist-info/RECORD +10 -0
- anyconv_video_basic-1.0.1.dist-info/WHEEL +5 -0
- anyconv_video_basic-1.0.1.dist-info/entry_points.txt +2 -0
- anyconv_video_basic-1.0.1.dist-info/licenses/LICENSE +21 -0
- anyconv_video_basic-1.0.1.dist-info/top_level.txt +1 -0
|
@@ -0,0 +1,186 @@
|
|
|
1
|
+
spec: "^1.0"
|
|
2
|
+
api: ">=1.0,<2.0"
|
|
3
|
+
id: com.gitlab.raceozr.video-basic
|
|
4
|
+
version: 1.0.1
|
|
5
|
+
title:
|
|
6
|
+
en: "Basic video"
|
|
7
|
+
ru: "Базовое видео"
|
|
8
|
+
description:
|
|
9
|
+
en: "Video conversion and audio extraction through FFmpeg: MP4, WebM, MKV, MOV, AVI."
|
|
10
|
+
ru: "Конвертация видео и извлечение аудио через FFmpeg: MP4, WebM, MKV, MOV, AVI."
|
|
11
|
+
license: MIT
|
|
12
|
+
homepage: "https://gitlab.com/anyconv/anyconv-modules"
|
|
13
|
+
keywords: [video, mp4, webm, mkv, mov, avi, audio, ffmpeg]
|
|
14
|
+
stability: stable
|
|
15
|
+
entrypoint:
|
|
16
|
+
kind: python
|
|
17
|
+
target: "anyconv_video_basic.module:VideoModule"
|
|
18
|
+
config:
|
|
19
|
+
schema:
|
|
20
|
+
type: object
|
|
21
|
+
additionalProperties: false
|
|
22
|
+
properties:
|
|
23
|
+
ffmpeg_path: {type: string}
|
|
24
|
+
requires:
|
|
25
|
+
python: ">=3.10"
|
|
26
|
+
packages: ["anyconv-core>=1.0,<2.0"]
|
|
27
|
+
|
|
28
|
+
capabilities:
|
|
29
|
+
- id: video-convert
|
|
30
|
+
op: convert
|
|
31
|
+
since: 1.0.0
|
|
32
|
+
routing: auto
|
|
33
|
+
fidelity: lossy
|
|
34
|
+
determinism: stochastic
|
|
35
|
+
cache: {reuse: false}
|
|
36
|
+
cost: {weight: 40}
|
|
37
|
+
limits: {timeout_s: 600, max_input_bytes: 1073741824, max_outputs: 1}
|
|
38
|
+
inputs:
|
|
39
|
+
- name: source
|
|
40
|
+
cardinality: one
|
|
41
|
+
accept:
|
|
42
|
+
formats: [video:mp4, video:webm, video:mkv, video:mov, video:avi]
|
|
43
|
+
outputs:
|
|
44
|
+
- name: result
|
|
45
|
+
cardinality: one
|
|
46
|
+
kind: data
|
|
47
|
+
produce:
|
|
48
|
+
formats: [video:mp4, video:webm, video:mkv, video:mov, video:avi]
|
|
49
|
+
settings:
|
|
50
|
+
type: object
|
|
51
|
+
additionalProperties: false
|
|
52
|
+
properties:
|
|
53
|
+
video_bitrate_kbps: {type: integer, minimum: 200, maximum: 20000, default: 2000}
|
|
54
|
+
resolution:
|
|
55
|
+
enum: [original, 1080p, 720p, 480p, 360p]
|
|
56
|
+
default: original
|
|
57
|
+
fps:
|
|
58
|
+
enum: ["original", "60", "30", "24", "15"]
|
|
59
|
+
default: original
|
|
60
|
+
audio_bitrate_kbps: {type: integer, minimum: 64, maximum: 320, default: 192}
|
|
61
|
+
mute: {type: boolean, default: false}
|
|
62
|
+
|
|
63
|
+
- id: video-contact-sheet
|
|
64
|
+
op: convert
|
|
65
|
+
since: 1.0.0
|
|
66
|
+
routing: auto
|
|
67
|
+
fidelity: lossy
|
|
68
|
+
determinism: stochastic
|
|
69
|
+
cache: {reuse: false}
|
|
70
|
+
cost: {weight: 14}
|
|
71
|
+
limits: {timeout_s: 300, max_input_bytes: 1073741824, max_outputs: 1}
|
|
72
|
+
inputs:
|
|
73
|
+
- name: source
|
|
74
|
+
cardinality: one
|
|
75
|
+
accept:
|
|
76
|
+
formats: [video:mp4, video:webm, video:mkv, video:mov, video:avi]
|
|
77
|
+
outputs:
|
|
78
|
+
- name: result
|
|
79
|
+
cardinality: one
|
|
80
|
+
kind: data
|
|
81
|
+
produce:
|
|
82
|
+
formats: [image:png]
|
|
83
|
+
settings:
|
|
84
|
+
type: object
|
|
85
|
+
additionalProperties: false
|
|
86
|
+
properties:
|
|
87
|
+
interval_s: {type: number, minimum: 0.1, maximum: 600, default: 2}
|
|
88
|
+
width: {type: integer, minimum: 64, maximum: 1920, default: 320}
|
|
89
|
+
columns: {type: integer, minimum: 1, maximum: 8, default: 4}
|
|
90
|
+
rows: {type: integer, minimum: 1, maximum: 8, default: 3}
|
|
91
|
+
|
|
92
|
+
- id: video-frames
|
|
93
|
+
op: extract
|
|
94
|
+
since: 1.0.0
|
|
95
|
+
routing: auto
|
|
96
|
+
fidelity: lossy
|
|
97
|
+
determinism: stochastic
|
|
98
|
+
cache: {reuse: false}
|
|
99
|
+
cost: {weight: 18}
|
|
100
|
+
limits: {timeout_s: 300, max_input_bytes: 1073741824, max_outputs: 60}
|
|
101
|
+
inputs:
|
|
102
|
+
- name: source
|
|
103
|
+
cardinality: one
|
|
104
|
+
accept:
|
|
105
|
+
formats: [video:mp4, video:webm, video:mkv, video:mov, video:avi]
|
|
106
|
+
outputs:
|
|
107
|
+
- name: frames
|
|
108
|
+
cardinality: many
|
|
109
|
+
kind: data
|
|
110
|
+
ordered: true
|
|
111
|
+
produce:
|
|
112
|
+
formats: [image:png]
|
|
113
|
+
settings:
|
|
114
|
+
type: object
|
|
115
|
+
additionalProperties: false
|
|
116
|
+
properties:
|
|
117
|
+
interval_s: {type: number, minimum: 0.1, maximum: 600, default: 1}
|
|
118
|
+
count: {type: integer, minimum: 1, maximum: 60, default: 6}
|
|
119
|
+
width: {type: integer, minimum: 64, maximum: 1920, default: 480}
|
|
120
|
+
|
|
121
|
+
- id: video-to-gif
|
|
122
|
+
op: convert
|
|
123
|
+
since: 1.0.0
|
|
124
|
+
routing: auto
|
|
125
|
+
fidelity: lossy
|
|
126
|
+
determinism: stochastic
|
|
127
|
+
cache: {reuse: false}
|
|
128
|
+
cost: {weight: 25}
|
|
129
|
+
limits: {timeout_s: 300, max_input_bytes: 1073741824, max_outputs: 1}
|
|
130
|
+
inputs:
|
|
131
|
+
- name: source
|
|
132
|
+
cardinality: one
|
|
133
|
+
accept:
|
|
134
|
+
formats: [video:mp4, video:webm, video:mkv, video:mov, video:avi]
|
|
135
|
+
outputs:
|
|
136
|
+
- name: result
|
|
137
|
+
cardinality: one
|
|
138
|
+
kind: data
|
|
139
|
+
produce:
|
|
140
|
+
formats: [image:gif]
|
|
141
|
+
settings:
|
|
142
|
+
type: object
|
|
143
|
+
additionalProperties: false
|
|
144
|
+
properties:
|
|
145
|
+
fps: {type: integer, minimum: 1, maximum: 30, default: 12}
|
|
146
|
+
width: {type: integer, minimum: 64, maximum: 1280, default: 480}
|
|
147
|
+
|
|
148
|
+
- id: video-extract-audio
|
|
149
|
+
op: convert
|
|
150
|
+
since: 1.0.0
|
|
151
|
+
routing: auto
|
|
152
|
+
fidelity: lossy
|
|
153
|
+
determinism: stochastic
|
|
154
|
+
cache: {reuse: false}
|
|
155
|
+
cost: {weight: 30}
|
|
156
|
+
limits: {timeout_s: 300, max_input_bytes: 1073741824, max_outputs: 1}
|
|
157
|
+
inputs:
|
|
158
|
+
- name: source
|
|
159
|
+
cardinality: one
|
|
160
|
+
accept:
|
|
161
|
+
formats: [video:mp4, video:webm, video:mkv, video:mov, video:avi]
|
|
162
|
+
outputs:
|
|
163
|
+
- name: result
|
|
164
|
+
cardinality: one
|
|
165
|
+
kind: data
|
|
166
|
+
produce:
|
|
167
|
+
formats:
|
|
168
|
+
- audio:wav
|
|
169
|
+
- audio:mp3
|
|
170
|
+
- audio:flac
|
|
171
|
+
- audio:ogg
|
|
172
|
+
- audio:opus
|
|
173
|
+
- audio:m4a
|
|
174
|
+
- audio:aac
|
|
175
|
+
- audio:aiff
|
|
176
|
+
settings:
|
|
177
|
+
type: object
|
|
178
|
+
additionalProperties: false
|
|
179
|
+
properties:
|
|
180
|
+
bitrate_kbps: {type: integer, minimum: 32, maximum: 320, default: 192}
|
|
181
|
+
sample_rate_hz:
|
|
182
|
+
enum: ["original", "8000", "16000", "22050", "32000", "44100", "48000", "96000"]
|
|
183
|
+
default: original
|
|
184
|
+
channels:
|
|
185
|
+
enum: [original, mono, stereo]
|
|
186
|
+
default: original
|
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
"""Small FFmpeg subprocess helper shared by the video module."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import shutil
|
|
6
|
+
import subprocess
|
|
7
|
+
from collections.abc import Mapping, Sequence
|
|
8
|
+
from pathlib import Path
|
|
9
|
+
from typing import Any
|
|
10
|
+
|
|
11
|
+
from anyconv.sdk.errors import CorruptInput, ModuleUnavailable, Timeout
|
|
12
|
+
|
|
13
|
+
_ENCODER_CACHE: dict[str, set[str]] = {}
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
def find_ffmpeg(config: Mapping[str, Any] | None = None) -> str | None:
|
|
17
|
+
configured = str((config or {}).get("ffmpeg_path", "")).strip()
|
|
18
|
+
if configured:
|
|
19
|
+
path = Path(configured)
|
|
20
|
+
return str(path) if path.is_file() else None
|
|
21
|
+
found = shutil.which("ffmpeg")
|
|
22
|
+
if found:
|
|
23
|
+
return found
|
|
24
|
+
try:
|
|
25
|
+
import imageio_ffmpeg
|
|
26
|
+
|
|
27
|
+
return imageio_ffmpeg.get_ffmpeg_exe()
|
|
28
|
+
except Exception:
|
|
29
|
+
return None
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
def encoders(ffmpeg: str) -> set[str]:
|
|
33
|
+
cached = _ENCODER_CACHE.get(ffmpeg)
|
|
34
|
+
if cached is not None:
|
|
35
|
+
return cached
|
|
36
|
+
completed = subprocess.run(
|
|
37
|
+
[ffmpeg, "-hide_banner", "-encoders"],
|
|
38
|
+
check=False,
|
|
39
|
+
capture_output=True,
|
|
40
|
+
text=True,
|
|
41
|
+
encoding="utf-8",
|
|
42
|
+
errors="replace",
|
|
43
|
+
)
|
|
44
|
+
names = set()
|
|
45
|
+
for line in completed.stdout.splitlines():
|
|
46
|
+
parts = line.split()
|
|
47
|
+
if len(parts) >= 2 and len(parts[0]) == 6:
|
|
48
|
+
names.add(parts[1])
|
|
49
|
+
_ENCODER_CACHE[ffmpeg] = names
|
|
50
|
+
return names
|
|
51
|
+
|
|
52
|
+
|
|
53
|
+
def choose_encoder(ffmpeg: str, candidates: Sequence[str]) -> str:
|
|
54
|
+
available = encoders(ffmpeg)
|
|
55
|
+
for candidate in candidates:
|
|
56
|
+
if candidate in available:
|
|
57
|
+
return candidate
|
|
58
|
+
raise ModuleUnavailable(
|
|
59
|
+
"Ни один из FFmpeg-кодеков не доступен",
|
|
60
|
+
details={"codecs": list(candidates)},
|
|
61
|
+
)
|
|
62
|
+
|
|
63
|
+
|
|
64
|
+
def run_ffmpeg(ffmpeg: str, args: Sequence[str], timeout: float | None = None) -> None:
|
|
65
|
+
command = [ffmpeg, "-hide_banner", "-loglevel", "error", "-nostdin", "-y", *args]
|
|
66
|
+
try:
|
|
67
|
+
completed = subprocess.run(
|
|
68
|
+
command,
|
|
69
|
+
check=False,
|
|
70
|
+
capture_output=True,
|
|
71
|
+
text=True,
|
|
72
|
+
encoding="utf-8",
|
|
73
|
+
errors="replace",
|
|
74
|
+
timeout=timeout,
|
|
75
|
+
)
|
|
76
|
+
except FileNotFoundError as exc:
|
|
77
|
+
raise ModuleUnavailable("FFmpeg не найден") from exc
|
|
78
|
+
except subprocess.TimeoutExpired as exc:
|
|
79
|
+
raise Timeout("FFmpeg превысил допустимое время работы") from exc
|
|
80
|
+
if completed.returncode != 0:
|
|
81
|
+
detail = (completed.stderr or completed.stdout or "").strip()
|
|
82
|
+
raise CorruptInput(
|
|
83
|
+
f"FFmpeg не смог обработать файл: {detail[-500:]}",
|
|
84
|
+
details={"returncode": completed.returncode},
|
|
85
|
+
)
|
|
86
|
+
|
|
87
|
+
|
|
88
|
+
def input_path(ctx: Any, source: Any, formats: Mapping[str, str]) -> Path:
|
|
89
|
+
if source.path is not None:
|
|
90
|
+
return Path(source.path)
|
|
91
|
+
path = ctx.tmp()
|
|
92
|
+
suffix = formats.get(source.typeref.format if source.typeref else "", "")
|
|
93
|
+
if suffix:
|
|
94
|
+
path = path.with_suffix(f".{suffix}")
|
|
95
|
+
path.write_bytes(ctx.read_input("source"))
|
|
96
|
+
return path
|
|
97
|
+
|
|
98
|
+
|
|
99
|
+
def output_path(ctx: Any, suffix: str) -> Path:
|
|
100
|
+
return ctx.tmp().with_suffix(f".{suffix}")
|
|
@@ -0,0 +1,312 @@
|
|
|
1
|
+
"""Basic video conversion and audio extraction module for AnyConv."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
from importlib.resources import files
|
|
6
|
+
from typing import Any
|
|
7
|
+
|
|
8
|
+
import yaml
|
|
9
|
+
from anyconv.sdk import Availability, CheckContext, Context, OpResult, Request
|
|
10
|
+
from anyconv.sdk.errors import ModuleUnavailable, OpNotImplemented, UnsupportedType
|
|
11
|
+
|
|
12
|
+
from .ffmpeg import choose_encoder, find_ffmpeg, input_path, output_path, run_ffmpeg
|
|
13
|
+
|
|
14
|
+
VIDEO_FORMATS: dict[str, tuple[str, tuple[str, ...], tuple[str, ...]]] = {
|
|
15
|
+
"video:mp4": ("mp4", ("libx264", "libopenh264", "mpeg4"), ("aac",)),
|
|
16
|
+
"video:webm": ("webm", ("libvpx-vp9", "libvpx"), ("libopus",)),
|
|
17
|
+
"video:mkv": ("mkv", ("libx264", "libopenh264", "mpeg4"), ("aac",)),
|
|
18
|
+
"video:mov": ("mov", ("libx264", "libopenh264", "mpeg4"), ("aac",)),
|
|
19
|
+
"video:avi": ("avi", ("libx264", "libopenh264", "mpeg4"), ("libmp3lame",)),
|
|
20
|
+
}
|
|
21
|
+
AUDIO_FORMATS: dict[str, tuple[str, tuple[str, ...]]] = {
|
|
22
|
+
"audio:wav": ("wav", ("pcm_s16le",)),
|
|
23
|
+
"audio:mp3": ("mp3", ("libmp3lame",)),
|
|
24
|
+
"audio:flac": ("flac", ("flac",)),
|
|
25
|
+
"audio:ogg": ("ogg", ("libvorbis", "libopus")),
|
|
26
|
+
"audio:opus": ("opus", ("libopus",)),
|
|
27
|
+
"audio:m4a": ("m4a", ("aac",)),
|
|
28
|
+
"audio:aac": ("aac", ("aac",)),
|
|
29
|
+
"audio:aiff": ("aiff", ("pcm_s16be",)),
|
|
30
|
+
}
|
|
31
|
+
AUDIO_LOSSY = {"audio:mp3", "audio:ogg", "audio:opus", "audio:m4a", "audio:aac"}
|
|
32
|
+
HEIGHTS = {"1080p": 1080, "720p": 720, "480p": 480, "360p": 360}
|
|
33
|
+
CHANNELS = {"mono": "1", "stereo": "2"}
|
|
34
|
+
FORMAT_SUFFIXES = {
|
|
35
|
+
**{key: value[0] for key, value in VIDEO_FORMATS.items()},
|
|
36
|
+
**{key: value[0] for key, value in AUDIO_FORMATS.items()},
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
class VideoModule:
|
|
41
|
+
@staticmethod
|
|
42
|
+
def manifest() -> dict[str, Any]:
|
|
43
|
+
return yaml.safe_load(
|
|
44
|
+
files("anyconv_video_basic")
|
|
45
|
+
.joinpath("anyconv.module.yaml")
|
|
46
|
+
.read_text("utf-8")
|
|
47
|
+
)
|
|
48
|
+
|
|
49
|
+
def check(self, ctx: CheckContext) -> Availability:
|
|
50
|
+
if find_ffmpeg(ctx.config) is None:
|
|
51
|
+
return Availability(status="unavailable", missing=["binary:ffmpeg"])
|
|
52
|
+
return Availability(status="ready")
|
|
53
|
+
|
|
54
|
+
def convert(self, req: Request, ctx: Context) -> OpResult:
|
|
55
|
+
if req.capability_id == "video-convert":
|
|
56
|
+
return self._convert_video(req, ctx)
|
|
57
|
+
if req.capability_id == "video-extract-audio":
|
|
58
|
+
return self._extract_audio(req, ctx)
|
|
59
|
+
if req.capability_id == "video-to-gif":
|
|
60
|
+
return self._to_gif(req, ctx)
|
|
61
|
+
if req.capability_id == "video-contact-sheet":
|
|
62
|
+
return self._contact_sheet(req, ctx)
|
|
63
|
+
raise OpNotImplemented(
|
|
64
|
+
f"Неизвестная возможность {req.capability_id!r}",
|
|
65
|
+
details={"capability": req.capability_id},
|
|
66
|
+
)
|
|
67
|
+
|
|
68
|
+
def _convert_video(self, req: Request, ctx: Context) -> OpResult:
|
|
69
|
+
target = req.target.format if req.target is not None else None
|
|
70
|
+
if target not in VIDEO_FORMATS:
|
|
71
|
+
raise UnsupportedType(
|
|
72
|
+
f"Видеоформат {target!r} не поддерживается",
|
|
73
|
+
details={"format": target, "supported": sorted(VIDEO_FORMATS)},
|
|
74
|
+
)
|
|
75
|
+
ffmpeg = find_ffmpeg(ctx.config)
|
|
76
|
+
if ffmpeg is None:
|
|
77
|
+
raise ModuleUnavailable(
|
|
78
|
+
"FFmpeg не найден",
|
|
79
|
+
details={"missing": ["binary:ffmpeg"]},
|
|
80
|
+
)
|
|
81
|
+
suffix, video_codecs, audio_codecs = VIDEO_FORMATS[target]
|
|
82
|
+
source = req.inputs["source"][0]
|
|
83
|
+
source_path = input_path(ctx, source, FORMAT_SUFFIXES)
|
|
84
|
+
destination = output_path(ctx, suffix)
|
|
85
|
+
video_codec = choose_encoder(ffmpeg, video_codecs)
|
|
86
|
+
audio_codec = choose_encoder(ffmpeg, audio_codecs)
|
|
87
|
+
args: list[str] = [
|
|
88
|
+
"-i",
|
|
89
|
+
str(source_path),
|
|
90
|
+
"-map",
|
|
91
|
+
"0:v:0",
|
|
92
|
+
"-map",
|
|
93
|
+
"0:a:0?",
|
|
94
|
+
"-c:v",
|
|
95
|
+
video_codec,
|
|
96
|
+
"-b:v",
|
|
97
|
+
f"{int(ctx.settings.get('video_bitrate_kbps', 2000))}k",
|
|
98
|
+
"-pix_fmt",
|
|
99
|
+
"yuv420p",
|
|
100
|
+
]
|
|
101
|
+
resolution = str(ctx.settings.get("resolution", "original"))
|
|
102
|
+
if resolution in HEIGHTS:
|
|
103
|
+
args += ["-vf", f"scale=-2:{HEIGHTS[resolution]}"]
|
|
104
|
+
fps = str(ctx.settings.get("fps", "original"))
|
|
105
|
+
if fps != "original":
|
|
106
|
+
args += ["-r", fps]
|
|
107
|
+
if bool(ctx.settings.get("mute", False)):
|
|
108
|
+
args += ["-an"]
|
|
109
|
+
else:
|
|
110
|
+
args += [
|
|
111
|
+
"-c:a",
|
|
112
|
+
audio_codec,
|
|
113
|
+
"-b:a",
|
|
114
|
+
f"{int(ctx.settings.get('audio_bitrate_kbps', 192))}k",
|
|
115
|
+
]
|
|
116
|
+
if target in ("video:mp4", "video:mov"):
|
|
117
|
+
args += ["-movflags", "+faststart"]
|
|
118
|
+
args.append(str(destination))
|
|
119
|
+
run_ffmpeg(ffmpeg, args, timeout=ctx.limits.timeout_s)
|
|
120
|
+
provenance = {
|
|
121
|
+
"target_format": target,
|
|
122
|
+
"video_codec": video_codec,
|
|
123
|
+
"audio_codec": None if bool(ctx.settings.get("mute", False)) else audio_codec,
|
|
124
|
+
"video_bitrate_kbps": ctx.settings.get("video_bitrate_kbps", 2000),
|
|
125
|
+
"resolution": resolution,
|
|
126
|
+
"fps": fps,
|
|
127
|
+
"mute": bool(ctx.settings.get("mute", False)),
|
|
128
|
+
}
|
|
129
|
+
return self._finish(ctx, destination, target, provenance)
|
|
130
|
+
|
|
131
|
+
def _to_gif(self, req: Request, ctx: Context) -> OpResult:
|
|
132
|
+
ffmpeg = find_ffmpeg(ctx.config)
|
|
133
|
+
if ffmpeg is None:
|
|
134
|
+
raise ModuleUnavailable(
|
|
135
|
+
"FFmpeg не найден",
|
|
136
|
+
details={"missing": ["binary:ffmpeg"]},
|
|
137
|
+
)
|
|
138
|
+
source = req.inputs["source"][0]
|
|
139
|
+
source_path = input_path(ctx, source, FORMAT_SUFFIXES)
|
|
140
|
+
destination = output_path(ctx, "gif")
|
|
141
|
+
fps = int(ctx.settings.get("fps", 12))
|
|
142
|
+
width = int(ctx.settings.get("width", 480))
|
|
143
|
+
filter_graph = (
|
|
144
|
+
f"fps={fps},scale={width}:-1:flags=lanczos,"
|
|
145
|
+
"split[s0][s1];[s0]palettegen[p];[s1][p]paletteuse"
|
|
146
|
+
)
|
|
147
|
+
run_ffmpeg(
|
|
148
|
+
ffmpeg,
|
|
149
|
+
[
|
|
150
|
+
"-i",
|
|
151
|
+
str(source_path),
|
|
152
|
+
"-filter_complex",
|
|
153
|
+
filter_graph,
|
|
154
|
+
"-loop",
|
|
155
|
+
"0",
|
|
156
|
+
"-y",
|
|
157
|
+
str(destination),
|
|
158
|
+
],
|
|
159
|
+
timeout=ctx.limits.timeout_s,
|
|
160
|
+
)
|
|
161
|
+
provenance = {"target_format": "image:gif", "fps": fps, "width": width}
|
|
162
|
+
return self._finish(ctx, destination, "image:gif", provenance)
|
|
163
|
+
|
|
164
|
+
def _contact_sheet(self, req: Request, ctx: Context) -> OpResult:
|
|
165
|
+
ffmpeg = find_ffmpeg(ctx.config)
|
|
166
|
+
if ffmpeg is None:
|
|
167
|
+
raise ModuleUnavailable(
|
|
168
|
+
"FFmpeg не найден",
|
|
169
|
+
details={"missing": ["binary:ffmpeg"]},
|
|
170
|
+
)
|
|
171
|
+
source = req.inputs["source"][0]
|
|
172
|
+
source_path = input_path(ctx, source, FORMAT_SUFFIXES)
|
|
173
|
+
destination = output_path(ctx, "png")
|
|
174
|
+
interval = float(ctx.settings.get("interval_s", 2))
|
|
175
|
+
width = int(ctx.settings.get("width", 320))
|
|
176
|
+
columns = int(ctx.settings.get("columns", 4))
|
|
177
|
+
rows = int(ctx.settings.get("rows", 3))
|
|
178
|
+
filter_graph = (
|
|
179
|
+
f"fps=1/{interval},scale={width}:-1,"
|
|
180
|
+
f"tile={columns}x{rows}"
|
|
181
|
+
)
|
|
182
|
+
run_ffmpeg(
|
|
183
|
+
ffmpeg,
|
|
184
|
+
[
|
|
185
|
+
"-i",
|
|
186
|
+
str(source_path),
|
|
187
|
+
"-vf",
|
|
188
|
+
filter_graph,
|
|
189
|
+
"-frames:v",
|
|
190
|
+
"1",
|
|
191
|
+
"-y",
|
|
192
|
+
str(destination),
|
|
193
|
+
],
|
|
194
|
+
timeout=ctx.limits.timeout_s,
|
|
195
|
+
)
|
|
196
|
+
provenance = {
|
|
197
|
+
"target_format": "image:png",
|
|
198
|
+
"interval_s": interval,
|
|
199
|
+
"width": width,
|
|
200
|
+
"columns": columns,
|
|
201
|
+
"rows": rows,
|
|
202
|
+
}
|
|
203
|
+
return self._finish(ctx, destination, "image:png", provenance)
|
|
204
|
+
|
|
205
|
+
def extract(self, req: Request, ctx: Context) -> OpResult:
|
|
206
|
+
if req.capability_id != "video-frames":
|
|
207
|
+
raise OpNotImplemented(
|
|
208
|
+
f"Неизвестная возможность {req.capability_id!r}",
|
|
209
|
+
details={"capability": req.capability_id},
|
|
210
|
+
)
|
|
211
|
+
ffmpeg = find_ffmpeg(ctx.config)
|
|
212
|
+
if ffmpeg is None:
|
|
213
|
+
raise ModuleUnavailable(
|
|
214
|
+
"FFmpeg не найден",
|
|
215
|
+
details={"missing": ["binary:ffmpeg"]},
|
|
216
|
+
)
|
|
217
|
+
source = req.inputs["source"][0]
|
|
218
|
+
source_path = input_path(ctx, source, FORMAT_SUFFIXES)
|
|
219
|
+
frame_dir = ctx.tmp().parent / "frames"
|
|
220
|
+
frame_dir.mkdir(parents=True, exist_ok=True)
|
|
221
|
+
interval = float(ctx.settings.get("interval_s", 1))
|
|
222
|
+
count = int(ctx.settings.get("count", 6))
|
|
223
|
+
width = int(ctx.settings.get("width", 480))
|
|
224
|
+
pattern = frame_dir / "frame-%03d.png"
|
|
225
|
+
run_ffmpeg(
|
|
226
|
+
ffmpeg,
|
|
227
|
+
[
|
|
228
|
+
"-i",
|
|
229
|
+
str(source_path),
|
|
230
|
+
"-vf",
|
|
231
|
+
f"fps=1/{interval},scale={width}:-1",
|
|
232
|
+
"-frames:v",
|
|
233
|
+
str(count),
|
|
234
|
+
"-y",
|
|
235
|
+
str(pattern),
|
|
236
|
+
],
|
|
237
|
+
timeout=ctx.limits.timeout_s,
|
|
238
|
+
)
|
|
239
|
+
files = sorted(frame_dir.glob("frame-*.png"))
|
|
240
|
+
items = []
|
|
241
|
+
for index, path in enumerate(files):
|
|
242
|
+
ctx.check_cancel()
|
|
243
|
+
with ctx.out_blob("frame", format="image:png") as writer:
|
|
244
|
+
writer.write(path.read_bytes())
|
|
245
|
+
assert writer.output is not None
|
|
246
|
+
writer.output.name = f"frame-{index + 1:03d}.png"
|
|
247
|
+
items.append(writer.output)
|
|
248
|
+
collection = ctx.out_collection("frames", items, ordered=True)
|
|
249
|
+
provenance = {
|
|
250
|
+
"target_format": "image:png",
|
|
251
|
+
"frames": len(items),
|
|
252
|
+
"interval_s": interval,
|
|
253
|
+
"width": width,
|
|
254
|
+
}
|
|
255
|
+
ctx.emit_provenance(provenance)
|
|
256
|
+
return OpResult(outputs={"frames": collection}, provenance=provenance)
|
|
257
|
+
|
|
258
|
+
def _extract_audio(self, req: Request, ctx: Context) -> OpResult:
|
|
259
|
+
target = req.target.format if req.target is not None else None
|
|
260
|
+
if target not in AUDIO_FORMATS:
|
|
261
|
+
raise UnsupportedType(
|
|
262
|
+
f"Аудиоформат {target!r} не поддерживается",
|
|
263
|
+
details={"format": target, "supported": sorted(AUDIO_FORMATS)},
|
|
264
|
+
)
|
|
265
|
+
ffmpeg = find_ffmpeg(ctx.config)
|
|
266
|
+
if ffmpeg is None:
|
|
267
|
+
raise ModuleUnavailable(
|
|
268
|
+
"FFmpeg не найден",
|
|
269
|
+
details={"missing": ["binary:ffmpeg"]},
|
|
270
|
+
)
|
|
271
|
+
suffix, codecs = AUDIO_FORMATS[target]
|
|
272
|
+
source = req.inputs["source"][0]
|
|
273
|
+
source_path = input_path(ctx, source, FORMAT_SUFFIXES)
|
|
274
|
+
destination = output_path(ctx, suffix)
|
|
275
|
+
codec = choose_encoder(ffmpeg, codecs)
|
|
276
|
+
args: list[str] = ["-i", str(source_path), "-vn", "-map", "0:a:0?", "-c:a", codec]
|
|
277
|
+
if target in AUDIO_LOSSY:
|
|
278
|
+
args += ["-b:a", f"{int(ctx.settings.get('bitrate_kbps', 192))}k"]
|
|
279
|
+
sample_rate = str(ctx.settings.get("sample_rate_hz", "original"))
|
|
280
|
+
if sample_rate != "original":
|
|
281
|
+
args += ["-ar", sample_rate]
|
|
282
|
+
channels = str(ctx.settings.get("channels", "original"))
|
|
283
|
+
if channels in CHANNELS:
|
|
284
|
+
args += ["-ac", CHANNELS[channels]]
|
|
285
|
+
args.append(str(destination))
|
|
286
|
+
run_ffmpeg(ffmpeg, args, timeout=ctx.limits.timeout_s)
|
|
287
|
+
provenance = {
|
|
288
|
+
"target_format": target,
|
|
289
|
+
"codec": codec,
|
|
290
|
+
"bitrate_kbps": ctx.settings.get("bitrate_kbps", 192),
|
|
291
|
+
"sample_rate_hz": sample_rate,
|
|
292
|
+
"channels": channels,
|
|
293
|
+
}
|
|
294
|
+
return self._finish(ctx, destination, target, provenance)
|
|
295
|
+
|
|
296
|
+
def _finish(
|
|
297
|
+
self,
|
|
298
|
+
ctx: Context,
|
|
299
|
+
destination: Any,
|
|
300
|
+
target: str,
|
|
301
|
+
provenance: dict[str, Any],
|
|
302
|
+
) -> OpResult:
|
|
303
|
+
with destination.open("rb") as source_file:
|
|
304
|
+
data = source_file.read()
|
|
305
|
+
with ctx.out_blob("result", format=target) as writer:
|
|
306
|
+
writer.write(data)
|
|
307
|
+
ctx.emit_provenance(provenance)
|
|
308
|
+
assert writer.output is not None
|
|
309
|
+
return OpResult(outputs={"result": writer.output}, provenance=provenance)
|
|
310
|
+
|
|
311
|
+
|
|
312
|
+
__all__ = ["VideoModule"]
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: anyconv-video-basic
|
|
3
|
+
Version: 1.0.1
|
|
4
|
+
Summary: AnyConv basic video module: MP4, WebM, MKV, MOV, AVI and audio extraction
|
|
5
|
+
Author: AnyConv
|
|
6
|
+
License-Expression: MIT
|
|
7
|
+
Keywords: anyconv,video,mp4,webm,mkv,mov,avi,ffmpeg
|
|
8
|
+
Requires-Python: >=3.10
|
|
9
|
+
Description-Content-Type: text/markdown
|
|
10
|
+
License-File: LICENSE
|
|
11
|
+
Requires-Dist: anyconv-core<2.0,>=1.0
|
|
12
|
+
Provides-Extra: dev
|
|
13
|
+
Requires-Dist: pytest>=7; extra == "dev"
|
|
14
|
+
Requires-Dist: ruff>=0.5; extra == "dev"
|
|
15
|
+
Dynamic: license-file
|
|
16
|
+
|
|
17
|
+
# anyconv-video-basic
|
|
18
|
+
|
|
19
|
+
Базовая конвертация видео и извлечение аудио для AnyConv через FFmpeg.
|
|
20
|
+
|
|
21
|
+
## Форматы
|
|
22
|
+
|
|
23
|
+
- Видео: MP4, WebM, MKV, MOV, AVI.
|
|
24
|
+
- Извлечение аудио: WAV, MP3, FLAC, Ogg, Opus, M4A (AAC), AAC, AIFF.
|
|
25
|
+
- **`video-to-gif`** — видео → GIF с настройкой FPS и ширины.
|
|
26
|
+
- **`video-frames`** — извлечение выбранного числа кадров PNG.
|
|
27
|
+
- **`video-contact-sheet`** — контактный лист кадров PNG.
|
|
28
|
+
|
|
29
|
+
## Требования
|
|
30
|
+
|
|
31
|
+
Нужен `ffmpeg` в `PATH` или путь в конфигурации модуля:
|
|
32
|
+
|
|
33
|
+
```yaml
|
|
34
|
+
modules:
|
|
35
|
+
config:
|
|
36
|
+
com.gitlab.raceozr.video-basic:
|
|
37
|
+
ffmpeg_path: /usr/bin/ffmpeg
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
Можно установить переносимый бинарник через `pip install imageio-ffmpeg` — модуль
|
|
41
|
+
найдёт его автоматически.
|
|
42
|
+
|
|
43
|
+
## Настройки
|
|
44
|
+
|
|
45
|
+
| Настройка | По умолчанию | Описание |
|
|
46
|
+
|---|---|---|
|
|
47
|
+
| `video_bitrate_kbps` | `2000` | Битрейт видео, 200–20000 кбит/с |
|
|
48
|
+
| `resolution` | `original` | `original`, `1080p`, `720p`, `480p`, `360p` |
|
|
49
|
+
| `fps` | `original` | `original`, `60`, `30`, `24`, `15` |
|
|
50
|
+
| `audio_bitrate_kbps` | `192` | Битрейт аудиодорожки, 64–320 кбит/с |
|
|
51
|
+
| `mute` | `false` | Удалить аудиодорожку |
|
|
52
|
+
|
|
53
|
+
Для извлечения аудио используются `bitrate_kbps`, `sample_rate_hz` и `channels`.
|
|
54
|
+
|
|
55
|
+
## Установка и проверка
|
|
56
|
+
|
|
57
|
+
```bash
|
|
58
|
+
pip install "anyconv-core[all] @ git+https://gitlab.com/anyconv/anyconv-core.git@v1.0.1"
|
|
59
|
+
pip install "git+https://gitlab.com/anyconv/anyconv-modules.git@v1.0.1#subdirectory=modules/video-basic"
|
|
60
|
+
# ?????????? ?? checkout anyconv-modules
|
|
61
|
+
cd modules/video-basic
|
|
62
|
+
pip install -e ".[dev]"
|
|
63
|
+
pytest
|
|
64
|
+
anyconv module validate .
|
|
65
|
+
anyconv module test .
|
|
66
|
+
```
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
anyconv_video_basic/__init__.py,sha256=NIHstoEZnITXiDpTjv9vty0djPJhW_oCrmoPiBlFVJk,98
|
|
2
|
+
anyconv_video_basic/anyconv.module.yaml,sha256=scbrb1jvGwF6_I8CNDjVQx30qecY4o6XsP5Ks2hAUs8,5441
|
|
3
|
+
anyconv_video_basic/ffmpeg.py,sha256=ptKzIm4JkYWbXtqL0jvbdl6iGhhXqQjL6JC6amtAIOo,3129
|
|
4
|
+
anyconv_video_basic/module.py,sha256=6xawNqVFzF74FYnfujU5QQ73ey7tbKjDidaWWG2bPYU,12186
|
|
5
|
+
anyconv_video_basic-1.0.1.dist-info/licenses/LICENSE,sha256=Wahb1xasoYXZBxdcuh2ETI6eaWi4JazPi9yhb0eFBxU,1077
|
|
6
|
+
anyconv_video_basic-1.0.1.dist-info/METADATA,sha256=0gw2KtI4xWKNZbByLAdqaJGoo4quHWDHIFv8jYPh0cs,2427
|
|
7
|
+
anyconv_video_basic-1.0.1.dist-info/WHEEL,sha256=mOdXR9C3wMVov7cSY8PY-XCzo3SmmWCHvGQr5csJoao,91
|
|
8
|
+
anyconv_video_basic-1.0.1.dist-info/entry_points.txt,sha256=B9kt9W5kExv2tK-j7n3KxO0TH1TTcVgO2n2wwOuNi7M,71
|
|
9
|
+
anyconv_video_basic-1.0.1.dist-info/top_level.txt,sha256=gJ62GYkm4Qwqp-0TnmMNOqHdI3H-p-NJTq7lEnslYtw,20
|
|
10
|
+
anyconv_video_basic-1.0.1.dist-info/RECORD,,
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 AnyConv contributors
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
anyconv_video_basic
|