ffmpeg-skill 0.10.0 → 0.12.0

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.
@@ -0,0 +1,128 @@
1
+ #!/usr/bin/env python3
2
+ """Turn a still image into a silent, timed video clip.
3
+
4
+ Produces a fixed-duration, constant-frame-rate video from one image -- for
5
+ example a title card, an end slate, or a placeholder to slot into join.py
6
+ alongside real footage. The output has no audio track: pair it with audio.py
7
+ or export.py's own audio handling if the surrounding edit needs sound under
8
+ the still.
9
+
10
+ --width/--height set the output frame size the same way fit.py does: give
11
+ one and the other follows the image's own aspect; give both for an exact
12
+ frame (the image is scaled to fill it, centre-cropping any excess -- never
13
+ distorted). Omit both to keep the image's native size (evened for 4:2:0).
14
+
15
+ --zoom in|out applies a Ken Burns effect: a slow, linear zoom across the
16
+ clip's duration (--zoom-amount sets the end/start zoom factor, default 1.3 =
17
+ 30% zoomed in by the end). --pan left|right|up|down drifts the visible
18
+ window across the image while zoomed (ignored, with a warning, if --zoom is
19
+ not also given -- panning needs the extra image area a zoom exposes).
20
+
21
+ Examples:
22
+ python3 insert.py title.png --duration 3
23
+ python3 insert.py slate.jpg --duration 5 --width 1920 --height 1080 --fps 30 -o slate.mp4
24
+ python3 insert.py photo.jpg --duration 6 --zoom in --pan right --width 1920 --height 1080
25
+ """
26
+ import argparse
27
+ import math
28
+ import sys
29
+
30
+ from _common import add_common, apply_common, default_output, die, emit, ffmpeg_base, info, parse_time, probe, run, video_args
31
+
32
+
33
+ def even(n: float) -> int:
34
+ v = int(round(n))
35
+ return v if v % 2 == 0 else v + 1
36
+
37
+
38
+ def main() -> int:
39
+ ap = argparse.ArgumentParser(description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter)
40
+ ap.add_argument("input", help="still image (PNG/JPG/...)")
41
+ ap.add_argument("-o", "--output", help="output file (default: <name>_insert.mp4)")
42
+ ap.add_argument("--duration", required=True, help="clip duration (seconds or mm:ss)")
43
+ ap.add_argument("--width", type=int, help="output width in px; with --height also given, both are used directly")
44
+ ap.add_argument("--height", type=int, help="output height in px; with --width also given, both are used directly")
45
+ ap.add_argument("--fps", type=float, default=30.0, help="output frame rate (default 30)")
46
+ ap.add_argument("--zoom", choices=["in", "out"], help="Ken Burns: slow linear zoom in or out across the clip")
47
+ ap.add_argument("--zoom-amount", type=float, default=1.3, help="end (zoom in) or start (zoom out) zoom factor, > 1.0 (default 1.3)")
48
+ ap.add_argument("--pan", choices=["left", "right", "up", "down"], help="drift the visible window this direction while zoomed (needs --zoom)")
49
+ ap.add_argument("--crf", type=int, default=18, help="x264 CRF (default 18)")
50
+ ap.add_argument("--preset", default="medium", help="x264 preset")
51
+ add_common(ap)
52
+ args = ap.parse_args()
53
+ apply_common(args)
54
+
55
+ target = parse_time(args.duration)
56
+ if target <= 0:
57
+ die("--duration must be > 0")
58
+ if args.fps <= 0:
59
+ die("--fps must be > 0")
60
+ if args.zoom_amount <= 1.0:
61
+ die(f"--zoom-amount must be > 1.0, got {args.zoom_amount}")
62
+ if args.pan and not args.zoom:
63
+ die("--pan needs --zoom in|out")
64
+
65
+ meta = probe(args.input)
66
+ if not meta.get("video"):
67
+ die("input has no image/video stream")
68
+ sw, sh = meta["video"]["width"], meta["video"]["height"]
69
+ ratio = sw / sh
70
+
71
+ if args.width and args.height:
72
+ out_w, out_h = even(args.width), even(args.height)
73
+ elif args.width:
74
+ out_w = even(args.width)
75
+ out_h = even(out_w / ratio)
76
+ elif args.height:
77
+ out_h = even(args.height)
78
+ out_w = even(out_h * ratio)
79
+ else:
80
+ out_w, out_h = even(sw), even(sh)
81
+
82
+ if args.zoom:
83
+ frames = max(1, round(target * args.fps))
84
+ amount = args.zoom_amount
85
+ if args.zoom == "in":
86
+ zexpr = f"if(eq(on,0),1,min(zoom+{(amount - 1) / frames:.8f},{amount:g}))"
87
+ else:
88
+ zexpr = f"if(eq(on,0),{amount:g},max(zoom-{(amount - 1) / frames:.8f},1))"
89
+ pan_x = {
90
+ "left": f"(iw-iw/zoom)*(1-on/{frames})",
91
+ "right": f"(iw-iw/zoom)*on/{frames}",
92
+ }.get(args.pan, "iw/2-(iw/zoom/2)")
93
+ pan_y = {
94
+ "up": f"(ih-ih/zoom)*(1-on/{frames})",
95
+ "down": f"(ih-ih/zoom)*on/{frames}",
96
+ }.get(args.pan, "ih/2-(ih/zoom/2)")
97
+ # zoompan samples from the still at its native resolution; scale it up first so the
98
+ # zoomed-in crop still has real pixels to draw from instead of upscaling blur.
99
+ upscale = max(2, math.ceil(amount * 2))
100
+ vf = [
101
+ f"scale={out_w * upscale}:{out_h * upscale}:force_original_aspect_ratio=increase",
102
+ f"crop={out_w * upscale}:{out_h * upscale}",
103
+ f"zoompan=z='{zexpr}':x='{pan_x}':y='{pan_y}':d={frames}:s={out_w}x{out_h}:fps={args.fps:g}",
104
+ "setsar=1",
105
+ ]
106
+ else:
107
+ vf = [
108
+ f"scale={out_w}:{out_h}:force_original_aspect_ratio=increase",
109
+ f"crop={out_w}:{out_h}",
110
+ "setsar=1",
111
+ f"fps={args.fps:g}",
112
+ ]
113
+
114
+ output = args.output or default_output(args.input, "insert", "mp4")
115
+ cmd = ffmpeg_base() + ["-loop", "1", "-i", args.input, "-t", f"{target:.3f}", "-vf", ",".join(vf)]
116
+ cmd += video_args(None, args.crf, args.preset)
117
+ cmd += ["-an", output]
118
+ run(cmd)
119
+
120
+ result = probe(output, role="output")
121
+ v = result["video"]
122
+ info(f"wrote {output} ({result['duration']:.3f}s, {v['width']}x{v['height']}, {v['fps']:g}fps)")
123
+ emit(output)
124
+ return 0
125
+
126
+
127
+ if __name__ == "__main__":
128
+ sys.exit(main())
package/scripts/join.py CHANGED
@@ -121,9 +121,11 @@ def main() -> int:
121
121
  if args.width and args.height:
122
122
  w, h = args.width, args.height
123
123
  elif args.width:
124
- w, h = args.width, int(round(args.width * fh / fw))
124
+ w = args.width
125
+ h = int(round(args.width * fh / fw)) if fw else args.width
125
126
  elif args.height:
126
- w, h = int(round(args.height * fw / fh)), args.height
127
+ h = args.height
128
+ w = int(round(args.height * fw / fh)) if fh else args.height
127
129
  else:
128
130
  w, h = fw, fh
129
131
  fps = args.fps or first.get("fps") or 30.0
@@ -141,15 +143,22 @@ def main() -> int:
141
143
  n = len(args.inputs)
142
144
  for i, (p, m) in enumerate(zip(args.inputs, metas)):
143
145
  cmd += ["-i", p]
144
- # silent audio for clips without an audio track
146
+ # silent audio for clips without an audio track. `idx` is this ffmpeg input's position, i.e. n +
147
+ # how many synthetic inputs were already added -- not len(extra_inputs), which counts the six
148
+ # argv tokens ("-f", "lavfi", "-t", duration, "-i", "anullsrc=...") each synthetic input adds, not
149
+ # the input itself. With one no-audio clip both counts coincide (n + 0); from the second no-audio
150
+ # clip onward they diverge, and the previous `n + len(extra_inputs)` named a nonexistent, far-out-of-
151
+ # range ffmpeg input index -- found via a real multi-camera join where every clip lacked audio.
145
152
  audio_src: List[str] = []
153
+ added = 0
146
154
  for i, m in enumerate(metas):
147
155
  if m.get("audio"):
148
156
  audio_src.append(f"{i}:a:0")
149
157
  else:
150
- idx = n + len(extra_inputs)
158
+ idx = n + added
151
159
  extra_inputs += ["-f", "lavfi", "-t", f"{durs[i]:.3f}", "-i", "anullsrc=r=48000:cl=stereo"]
152
160
  audio_src.append(f"{idx}:a:0")
161
+ added += 1
153
162
  cmd += extra_inputs
154
163
 
155
164
  if args.fit == "crop":
@@ -180,7 +189,7 @@ def main() -> int:
180
189
  cmd += video_args(metas[0], args.crf, args.preset) + aac_args() + [output]
181
190
  run(cmd)
182
191
  expected = sum(durs) - d * (n - 1)
183
- r = probe(output)
192
+ r = probe(output, role="output")
184
193
  info(f"wrote {output} ({r['duration']:.3f}s, expected ~{expected:.3f}s, {w}x{h} @ {fps:g}fps, {n} clips, {args.transition})")
185
194
  emit(output, mode="video", clips=n, transition=args.transition, expected_duration=round(expected, 3))
186
195
  return 0
@@ -24,13 +24,13 @@ from _common import STATE, add_common, apply_common, emit, AUDIO_CODECS, audio_c
24
24
 
25
25
  def measure(path: str, I: float, tp: float, lra: float) -> dict:
26
26
  if STATE["dry_run"]:
27
- return {"input_i": "-20.0", "input_tp": "-3.0", "input_lra": "8.0", "input_thresh": "-30.0", "target_offset": "0.0"}
27
+ return {"input_i": "-20.0", "input_tp": "-3.0", "input_lra": "8.0", "input_thresh": "-30.0", "target_offset": "0.0", "silent": False}
28
28
  ffmpeg = require_tool("ffmpeg")
29
29
  cmd = [ffmpeg, "-hide_banner", "-nostdin", "-i", path, "-vn", "-af", f"loudnorm=I={I}:TP={tp}:LRA={lra}:print_format=json", "-f", "null", "-"]
30
30
  proc = run(cmd, check=False)
31
31
  m = re.search(r"\{[^{}]*\"input_i\"[^{}]*\}", proc.stderr, re.S)
32
32
  if proc.returncode != 0 or not m:
33
- die(f"loudness measurement failed:\n{proc.stderr.strip()[-1500:]}")
33
+ die(f"loudness measurement failed:\n{proc.stderr.strip()[-1500:]}", kind="ffmpeg")
34
34
  data = json.loads(m.group(0))
35
35
  for k in ("input_i", "input_tp", "input_lra", "input_thresh", "target_offset"):
36
36
  if data.get(k) in (None, "-inf", "inf", "nan"):
@@ -89,7 +89,7 @@ def main() -> int:
89
89
  after = measure(output, args.lufs, args.tp, args.lra)
90
90
  if not after.get("silent"):
91
91
  info(f"result: {float(after['input_i']):.1f} LUFS, TP {float(after['input_tp']):.1f} dBTP (target {args.lufs} LUFS)")
92
- emit(output)
92
+ emit(output, result={k: after[k] for k in ("input_i", "input_tp", "input_lra", "input_thresh", "target_offset", "silent")})
93
93
  return 0
94
94
 
95
95
 
@@ -197,7 +197,7 @@ def main() -> int:
197
197
  cmd += ["-filter_complex", ";".join(parts), "-map", "[vout]", "-map", "[aout]"]
198
198
  cmd += video_args(metas[0], args.crf, args.preset) + aac_args() + ["-shortest", output]
199
199
  run(cmd)
200
- r = probe(output)
200
+ r = probe(output, role="output")
201
201
  info(f"wrote {output} ({r['duration']:.3f}s, {len(filled)} cuts, audio from input {a})")
202
202
  emit(output, cuts=[[round(s, 3), round(e, 3), c] for s, e, c in filled], **report)
203
203
  return 0
@@ -5,11 +5,20 @@ opacity and fade in/out.
5
5
  Positions: top-left, top, top-right, left, center, right, bottom-left, bottom,
6
6
  bottom-right, or explicit "X,Y" pixels (negative counts from the far edge).
7
7
 
8
+ --video composites a second VIDEO as a picture-in-picture layer (position,
9
+ scale, opacity, time-range -- same knobs as --image), instead of a still
10
+ image or text. Only the main input's audio is kept; the PiP layer's own
11
+ audio track, if any, is dropped -- mixing two audio tracks is a job for
12
+ audio.py, not this tool. --chromakey COLOR (with --video) turns that colour
13
+ transparent first (green-screen removal) before compositing.
14
+
8
15
  Examples:
9
16
  python3 overlay.py input.mp4 --image logo.png --position top-right --scale 200 --opacity 0.8
10
17
  python3 overlay.py input.mp4 --image lower_third.png --position bottom-left --start 2 --end 8 --fade 0.5
11
18
  python3 overlay.py input.mp4 --text "Episode 12" --position bottom --font-size 48 --start 1 --end 5 --fade 0.3
12
19
  python3 overlay.py input.mp4 --text "こんにちは" --font-file /path/NotoSansCJK-Bold.ttc --box
20
+ python3 overlay.py input.mp4 --video webcam.mp4 --position bottom-right --scale 480 --opacity 0.9
21
+ python3 overlay.py bg.mp4 --video greenscreen.mp4 --chromakey 0x00ff00 --chromakey-similarity 0.15
13
22
  """
14
23
  import argparse
15
24
  import sys
@@ -76,10 +85,19 @@ def main() -> int:
76
85
  ap = argparse.ArgumentParser(description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter)
77
86
  ap.add_argument("input")
78
87
  ap.add_argument("-o", "--output", help="output file (default: <name>_overlay.<ext>)")
88
+ ap.add_argument("--audio-stream", type=int, default=0,
89
+ help="which audio stream of the input to keep, 0-based in file order (probe.py lists them under "
90
+ "audio_streams) -- matters on a multi-track input (dubbed languages, M&E stems); default 0, "
91
+ "the first track, same as leaving it unset always did")
79
92
  src = ap.add_mutually_exclusive_group()
80
93
  src.add_argument("--image", help="PNG/JPG (alpha respected) to composite")
81
94
  src.add_argument("--text", help="text to draw (drawtext)")
82
95
  src.add_argument("--logo", action="store_true", help="composite the brand logo from --brand (position/scale/opacity from brand.json)")
96
+ src.add_argument("--video", help="a second video to composite as a picture-in-picture layer")
97
+ ck = ap.add_argument_group("chroma key (with --video)")
98
+ ck.add_argument("--chromakey", help="colour to key out (green-screen removal), e.g. 0x00ff00 or green")
99
+ ck.add_argument("--chromakey-similarity", type=float, default=0.15, help="how close a pixel must be to --chromakey to become transparent, 0..1 (default 0.15)")
100
+ ck.add_argument("--chromakey-blend", type=float, default=0.05, help="soften the key edge, 0..1 (default 0.05)")
83
101
  ap.add_argument("--brand", help="brand.json (logo, font, colours, safe margin)")
84
102
  ap.add_argument("--position", default="top-right", help="named position or X,Y (default top-right)")
85
103
  ap.add_argument("--margin", type=int, default=24, help="margin from the edges in px (default 24)")
@@ -117,8 +135,8 @@ def main() -> int:
117
135
  args.scale = int(brand.get("logo_scale", 160))
118
136
  if args.opacity == 1.0:
119
137
  args.opacity = float(brand.get("logo_opacity", 1.0))
120
- if not (args.image or args.text):
121
- die("give --image, --text or --logo")
138
+ if not (args.image or args.text or args.video):
139
+ die("give --image, --text, --logo or --video")
122
140
  if args.brand:
123
141
  if args.margin == ap.get_default("margin"):
124
142
  args.margin = int(brand.get("safe_margin", args.margin))
@@ -129,6 +147,11 @@ def main() -> int:
129
147
  meta = probe(args.input)
130
148
  if not meta.get("video"):
131
149
  die("input has no video stream")
150
+ audio_streams = meta.get("audio_streams") or []
151
+ if audio_streams and not (0 <= args.audio_stream < len(audio_streams)):
152
+ die(f"--audio-stream {args.audio_stream}: input has {len(audio_streams)} audio stream(s), 0..{len(audio_streams) - 1}")
153
+ if args.audio_stream and not audio_streams:
154
+ die("--audio-stream needs an input with audio streams")
132
155
  vw = meta["video"]["width"]
133
156
  start = parse_time(args.start) if args.start else None
134
157
  end = parse_time(args.end) if args.end else None
@@ -136,6 +159,12 @@ def main() -> int:
136
159
  die("--end must be after --start")
137
160
  if not 0 <= args.opacity <= 1:
138
161
  die("--opacity must be within 0..1")
162
+ if args.chromakey and not args.video:
163
+ die("--chromakey needs --video")
164
+ if not 0 < args.chromakey_similarity <= 1:
165
+ die("--chromakey-similarity must be within (0, 1]")
166
+ if not 0 <= args.chromakey_blend <= 1:
167
+ die("--chromakey-blend must be within 0..1")
139
168
 
140
169
  output = args.output or default_output(args.input, "overlay")
141
170
  enable = enable_expr(start, end)
@@ -164,12 +193,35 @@ def main() -> int:
164
193
  # -loop 1 turns the still into a timed stream so fade/enable expressions see real timestamps
165
194
  cmd = ffmpeg_base() + ["-i", args.input, "-loop", "1", "-i", args.image]
166
195
  fc = f"[1:v]{','.join(chain)},setpts=PTS-STARTPTS[ov];[0:v][ov]{ov}[out]"
167
- cmd += ["-filter_complex", fc, "-map", "[out]", "-map", "0:a:0?", "-shortest"]
196
+ cmd += ["-filter_complex", fc, "-map", "[out]", "-map", f"0:a:{args.audio_stream}?", "-shortest"]
168
197
  # -shortest alone is not exact on FFmpeg 7+: the muxer keeps up to shortest_buf_duration (10 s)
169
198
  # of the looped still after the video ended, and the file came out 2 s long on 8.1 / 9.0.
170
199
  # The output must be as long as the main input, so say so explicitly.
171
200
  if meta.get("duration"):
172
201
  cmd += ["-t", f"{meta['duration']:.3f}"]
202
+ elif args.video:
203
+ pip_meta = probe(args.video)
204
+ if not pip_meta.get("video"):
205
+ die(f"--video {args.video} has no video stream")
206
+ chain = []
207
+ if args.scale_percent:
208
+ chain.append(f"scale={int(vw * args.scale_percent / 100)}:-2")
209
+ elif args.scale:
210
+ chain.append(f"scale={args.scale}:-2")
211
+ chain.append("format=yuva420p")
212
+ if args.chromakey:
213
+ chain.append(f"chromakey={args.chromakey}:{args.chromakey_similarity:g}:{args.chromakey_blend:g}")
214
+ if args.opacity < 1:
215
+ chain.append(f"colorchannelmixer=aa={args.opacity:g}")
216
+ x, y = position_exprs(args.position, args.margin, text_mode=False)
217
+ ov = f"overlay={x}:{y}:format=auto"
218
+ if enable:
219
+ ov += f":enable='{enable}'"
220
+ cmd = ffmpeg_base() + ["-i", args.input, "-i", args.video]
221
+ fc = f"[1:v]{','.join(chain)}[ov];[0:v][ov]{ov}[out]"
222
+ cmd += ["-filter_complex", fc, "-map", "[out]", "-map", f"0:a:{args.audio_stream}?", "-shortest"]
223
+ if meta.get("duration"):
224
+ cmd += ["-t", f"{meta['duration']:.3f}"]
173
225
  else:
174
226
  x, y = position_exprs(args.position, args.margin, text_mode=True)
175
227
  opts = [f"text='{escape_drawtext(args.text)}'", f"fontsize={args.font_size}", f"x={x}", f"y={y}",
@@ -187,14 +239,16 @@ def main() -> int:
187
239
  opts += ["box=1", f"boxcolor={args.box_color}", "boxborderw=12"]
188
240
  if enable:
189
241
  opts.append(f"enable='{enable}'")
190
- cmd += ["-vf", "drawtext=" + ":".join(opts)]
242
+ cmd += ["-vf", "drawtext=" + ":".join(opts), "-map", "0:v:0"]
243
+ if meta.get("audio"):
244
+ cmd += ["-map", f"0:a:{args.audio_stream}"]
191
245
 
192
246
  cmd += video_args(meta, args.crf, args.preset) + cfr_args(meta)
193
247
  cmd += aac_args() if meta.get("audio") else ["-an"]
194
248
  cmd.append(output)
195
249
  run(cmd)
196
250
  if not STATE.dry_run:
197
- result = probe(output)
251
+ result = probe(output, role="output")
198
252
  info(f"wrote {output} ({result['duration']:.3f}s)")
199
253
  emit(output)
200
254
  return 0
@@ -0,0 +1,82 @@
1
+ #!/usr/bin/env python3
2
+ """Generate a small, low-bitrate proxy of a video: cheap for a machine to decode,
3
+ not meant for delivery. Intended for downstream AI analysis, preview or
4
+ editing-decision workflows that only need to look at (or feed a model) a
5
+ much smaller stand-in for the original.
6
+
7
+ Resizes to --width (default 640px, height follows the source aspect) or by
8
+ --scale factor, re-encodes at a proxy-grade --crf (default 30 - well above any
9
+ delivery preset's 18-24 in export.py, since a proxy trades visual quality for
10
+ size and speed), and always uses the fastest x264/x265 preset. Keeps the
11
+ source's own dynamic range (an HDR source proxies to HEVC10, same as every
12
+ other re-encoding tool here) rather than guessing whether SDR is wanted -
13
+ run color.py --to-sdr first if it is.
14
+
15
+ This tool only executes the spec it is given: it does not decide which asset
16
+ should be proxied, what resolution or bitrate is "right" for a given
17
+ downstream use, or what the proxy will be used for - those are the calling
18
+ agent's call.
19
+
20
+ Examples:
21
+ python3 proxy.py input.mov # 640px wide, CRF 30, keeps audio
22
+ python3 proxy.py input.mov --width 480 --no-audio # smaller, video-only
23
+ python3 proxy.py input.mov --scale 0.25 --fps 10 # quarter-size, 10fps (e.g. for a vision model)
24
+ """
25
+ import argparse
26
+ import sys
27
+
28
+ from _common import add_common, apply_common, cfr_args, default_output, die, emit, ffmpeg_base, info, probe, run, video_args
29
+
30
+
31
+ def even(n: float) -> int:
32
+ v = int(round(n))
33
+ return v if v % 2 == 0 else v + 1
34
+
35
+
36
+ def main() -> int:
37
+ ap = argparse.ArgumentParser(description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter)
38
+ ap.add_argument("input")
39
+ ap.add_argument("-o", "--output", help="output file (default: <name>_proxy.<ext>)")
40
+ ap.add_argument("--width", type=int, default=640, help="output width in px, height follows the source aspect (default 640)")
41
+ ap.add_argument("--scale", type=float, help="scale factor applied to the source dimensions instead of --width (0 < scale <= 1)")
42
+ ap.add_argument("--crf", type=int, default=30, help="proxy-grade CRF, higher = smaller/lower quality (default 30)")
43
+ ap.add_argument("--fps", type=float, help="force a constant output frame rate")
44
+ ap.add_argument("--no-audio", action="store_true", help="drop audio entirely (default: keep it)")
45
+ add_common(ap)
46
+ args = ap.parse_args()
47
+ apply_common(args)
48
+
49
+ if args.scale is not None and not 0.0 < args.scale <= 1.0:
50
+ die(f"--scale must be > 0 and <= 1, got {args.scale}")
51
+ if args.width <= 0:
52
+ die(f"--width must be > 0, got {args.width}")
53
+ if args.fps is not None and args.fps <= 0:
54
+ die(f"--fps must be > 0, got {args.fps}")
55
+
56
+ meta = probe(args.input)
57
+ if not meta.get("video"):
58
+ die("input has no video stream")
59
+ sw, sh = meta["video"]["width"], meta["video"]["height"]
60
+ if meta["video"].get("rotation") in (90, -90, 270, -270):
61
+ sw = sh
62
+ has_audio = bool(meta.get("audio")) and not args.no_audio
63
+
64
+ out_w = even(sw * args.scale) if args.scale is not None else even(args.width)
65
+ output = args.output or default_output(args.input, "proxy")
66
+
67
+ cmd = ffmpeg_base() + ["-i", args.input, "-vf", f"scale={out_w}:-2"]
68
+ cmd += video_args(meta, args.crf, "veryfast")
69
+ cmd += cfr_args(meta, args.fps)
70
+ cmd += ["-c:a", "aac", "-b:a", "96k"] if has_audio else ["-an"]
71
+ cmd.append(output)
72
+ run(cmd)
73
+
74
+ result = probe(output)
75
+ v = result["video"]
76
+ info(f"wrote {output} ({result['duration']:.3f}s, {v['width']}x{v['height']}, {v['codec']}, crf {args.crf})")
77
+ emit(output)
78
+ return 0
79
+
80
+
81
+ if __name__ == "__main__":
82
+ sys.exit(main())
@@ -0,0 +1,56 @@
1
+ #!/usr/bin/env python3
2
+ """Reverse a video (and its audio, unless dropped).
3
+
4
+ Uses ffmpeg's `reverse` (video) and `areverse` (audio) filters, which decode
5
+ and buffer the whole clip in memory -- long inputs cost real time and RAM,
6
+ which is why there is no length limit baked in here: it is the caller's job
7
+ to keep this to clips it makes sense to reverse (a few seconds to a couple of
8
+ minutes), not a workaround this tool applies for you.
9
+
10
+ Examples:
11
+ python3 reverse.py input.mp4
12
+ python3 reverse.py input.mp4 --no-audio -o backwards.mp4
13
+ """
14
+ import argparse
15
+ import sys
16
+
17
+ from _common import add_common, apply_common, aac_args, cfr_args, default_output, die, emit, ffmpeg_base, info, probe, run, video_args
18
+
19
+
20
+ def main() -> int:
21
+ ap = argparse.ArgumentParser(description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter)
22
+ ap.add_argument("input")
23
+ ap.add_argument("-o", "--output", help="output file (default: <name>_reverse.<ext>)")
24
+ ap.add_argument("--no-audio", action="store_true", help="drop audio instead of reversing it")
25
+ ap.add_argument("--crf", type=int, default=18, help="x264 CRF (default 18)")
26
+ ap.add_argument("--preset", default="medium", help="x264 preset")
27
+ add_common(ap)
28
+ args = ap.parse_args()
29
+ apply_common(args)
30
+
31
+ meta = probe(args.input)
32
+ if not meta.get("video"):
33
+ die("input has no video stream")
34
+ has_audio = bool(meta.get("audio")) and not args.no_audio
35
+
36
+ output = args.output or default_output(args.input, "reverse")
37
+ cmd = ffmpeg_base() + ["-i", args.input, "-vf", "reverse"]
38
+ if has_audio:
39
+ cmd += ["-af", "areverse"]
40
+ cmd += video_args(meta, args.crf, args.preset)
41
+ cmd += cfr_args(meta)
42
+ if has_audio:
43
+ cmd += aac_args()
44
+ else:
45
+ cmd += ["-an"]
46
+ cmd.append(output)
47
+ run(cmd)
48
+
49
+ result = probe(output, role="output")
50
+ info(f"wrote {output} ({result['duration']:.3f}s, {result['video']['width']}x{result['video']['height']})")
51
+ emit(output)
52
+ return 0
53
+
54
+
55
+ if __name__ == "__main__":
56
+ sys.exit(main())
@@ -0,0 +1,124 @@
1
+ #!/usr/bin/env python3
2
+ """Turn a numbered image sequence into a video.
3
+
4
+ --pattern accepts either a printf-style numbered pattern (`frame_%04d.png`,
5
+ resolved relative to --dir) or a glob (`*.png`, matched and sorted
6
+ alphabetically) -- detected by whether the pattern contains a `%`. Either
7
+ way, the actual frame list is resolved and checked on disk before ffmpeg
8
+ runs (an empty match or a missing first frame is refused here, not
9
+ discovered from an opaque ffmpeg error), then fed to ffmpeg as an explicit
10
+ concat list -- not `-pattern_type glob`, which several real ffmpeg builds
11
+ (the Windows Chocolatey package, for one) compile without.
12
+
13
+ Examples:
14
+ python3 sequence.py --dir frames --pattern "frame_%04d.png" --fps 24 -o out.mp4
15
+ python3 sequence.py --dir frames --pattern "*.png" --fps 30 --start-number 1
16
+ """
17
+ import argparse
18
+ import sys
19
+ import tempfile
20
+ from pathlib import Path
21
+
22
+ from _common import add_common, apply_common, default_output, die, emit, ffmpeg_base, info, probe, run, video_args
23
+
24
+
25
+ def even(n: float) -> int:
26
+ v = int(round(n))
27
+ return v if v % 2 == 0 else v + 1
28
+
29
+
30
+ def _concat_list_line(path: Path) -> str:
31
+ # concat demuxer file paths: backslash and single-quote need escaping inside the quoted form.
32
+ escaped = str(path).replace("\\", "/").replace("'", "'\\''")
33
+ return f"file '{escaped}'"
34
+
35
+
36
+ def main() -> int:
37
+ ap = argparse.ArgumentParser(description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter)
38
+ ap.add_argument("--dir", required=True, help="directory containing the frames")
39
+ ap.add_argument("--pattern", required=True, help="printf pattern (frame_%%04d.png) or glob (*.png)")
40
+ ap.add_argument("-o", "--output", help="output file (default: <dir>_sequence.mp4)")
41
+ ap.add_argument("--fps", type=float, default=30.0, help="output frame rate (default 30)")
42
+ ap.add_argument("--start-number", type=int, default=0, help="first frame index, for a printf pattern (default 0)")
43
+ ap.add_argument("--width", type=int, help="output width in px; with --height also given, both are used directly")
44
+ ap.add_argument("--height", type=int, help="output height in px; with --width also given, both are used directly")
45
+ ap.add_argument("--crf", type=int, default=18, help="x264 CRF (default 18)")
46
+ ap.add_argument("--preset", default="medium", help="x264 preset")
47
+ add_common(ap)
48
+ args = ap.parse_args()
49
+ apply_common(args)
50
+
51
+ if args.fps <= 0:
52
+ die("--fps must be > 0")
53
+ directory = Path(args.dir)
54
+ if not directory.is_dir():
55
+ die(f"--dir not found or not a directory: {args.dir}")
56
+
57
+ is_glob = "%" not in args.pattern
58
+ if is_glob:
59
+ frames = sorted(directory.glob(args.pattern))
60
+ if not frames:
61
+ die(f"no files in {args.dir} match glob '{args.pattern}'")
62
+ info(f"found {len(frames)} frames matching '{args.pattern}'")
63
+ else:
64
+ try:
65
+ args.pattern % args.start_number
66
+ except (TypeError, ValueError):
67
+ die(f"bad printf pattern '{args.pattern}'")
68
+ frames = []
69
+ i = args.start_number
70
+ while (directory / (args.pattern % i)).exists():
71
+ frames.append(directory / (args.pattern % i))
72
+ i += 1
73
+ if not frames:
74
+ die(f"first frame not found: {directory / (args.pattern % args.start_number)} (check --pattern / --start-number)")
75
+ info(f"found {len(frames)} consecutive frames from index {args.start_number}")
76
+
77
+ frame_meta = probe(str(frames[0]))
78
+ if not frame_meta.get("video"):
79
+ die(f"{frames[0]} is not a readable image")
80
+ sw, sh = frame_meta["video"]["width"], frame_meta["video"]["height"]
81
+
82
+ if args.width and args.height:
83
+ out_w, out_h = even(args.width), even(args.height)
84
+ elif args.width:
85
+ out_w = even(args.width)
86
+ out_h = even(out_w * sh / sw)
87
+ elif args.height:
88
+ out_h = even(args.height)
89
+ out_w = even(out_h * sw / sh)
90
+ else:
91
+ out_w, out_h = even(sw), even(sh)
92
+
93
+ output = args.output or default_output(str(directory).rstrip("/\\") or "sequence", "sequence", "mp4")
94
+ frame_duration = 1.0 / args.fps
95
+
96
+ with tempfile.TemporaryDirectory(prefix="ffmpeg-skill-sequence-") as tmp:
97
+ list_path = Path(tmp) / "frames.txt"
98
+ lines = []
99
+ for f in frames:
100
+ lines.append(_concat_list_line(f.resolve()))
101
+ lines.append(f"duration {frame_duration:.6f}")
102
+ lines.append(_concat_list_line(frames[-1].resolve())) # concat demuxer: last entry's duration is ignored, so repeat it
103
+ list_path.write_text("\n".join(lines), encoding="utf-8")
104
+
105
+ cmd = ffmpeg_base() + ["-f", "concat", "-safe", "0", "-i", str(list_path)]
106
+ vf = [f"scale={out_w}:{out_h}", "setsar=1", f"fps={args.fps:g}"]
107
+ cmd += ["-vf", ",".join(vf)]
108
+ cmd += video_args(None, args.crf, args.preset)
109
+ # The concat demuxer's trailing repeated-last-file trick (needed so the last real file's
110
+ # duration line takes effect) has been observed to produce an extra frame's worth of
111
+ # duration on some ffmpeg builds -- force the exact intended length rather than trust it.
112
+ total_duration = len(frames) * frame_duration
113
+ cmd += ["-t", f"{total_duration:.6f}", "-an", output]
114
+ run(cmd)
115
+
116
+ result = probe(output, role="output")
117
+ v = result["video"]
118
+ info(f"wrote {output} ({result['duration']:.3f}s, {v['width']}x{v['height']}, {v['fps']:g}fps)")
119
+ emit(output)
120
+ return 0
121
+
122
+
123
+ if __name__ == "__main__":
124
+ sys.exit(main())
@@ -27,7 +27,7 @@ def detect(path: str, threshold: float, min_silence: float) -> List[Tuple[float,
27
27
  f"silencedetect=noise={threshold}dB:d={min_silence}", "-f", "null", "-"]
28
28
  proc = run(cmd, quiet=True, check=False)
29
29
  if proc.returncode != 0:
30
- die(f"silencedetect failed:\n{proc.stderr.strip()[-800:]}")
30
+ die(f"silencedetect failed:\n{proc.stderr.strip()[-800:]}", kind="ffmpeg")
31
31
  silences: List[Tuple[float, float]] = []
32
32
  start = None
33
33
  for kind, val in SIL_RE.findall(proc.stderr):
@@ -113,7 +113,7 @@ def main() -> int:
113
113
  cmd += ["-vf", vf] + video_args(meta, args.crf, args.preset) + cfr_args(meta)
114
114
  cmd += ["-af", af] + aac_args() + [output]
115
115
  run(cmd)
116
- r = probe(output)
116
+ r = probe(output, role="output")
117
117
  info(f"wrote {output} ({r['duration']:.3f}s, expected ~{kept:.3f}s)")
118
118
  emit(output, **summary)
119
119
  return 0