ffmpeg-skill 3.1.5 → 3.1.7

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.
Files changed (42) hide show
  1. package/docs/contract.md +11 -7
  2. package/package.json +1 -1
  3. package/references/ci-platform-pitfalls.md +3 -2
  4. package/references/gotchas.md +5 -1
  5. package/references/scripts.md +12 -6
  6. package/scripts/_common/__init__.py +7 -5
  7. package/scripts/_common/asr.py +49 -10
  8. package/scripts/_common/color.py +40 -1
  9. package/scripts/_common/decision.py +177 -10
  10. package/scripts/_common/emit.py +2 -0
  11. package/scripts/_common/fonts.py +26 -0
  12. package/scripts/_common/probe.py +115 -5
  13. package/scripts/_common/runner.py +38 -1
  14. package/scripts/_common/timeline.py +123 -27
  15. package/scripts/_contract.py +34 -3
  16. package/scripts/broll.py +35 -14
  17. package/scripts/caption.py +105 -19
  18. package/scripts/color.py +37 -8
  19. package/scripts/crop.py +8 -3
  20. package/scripts/cropdetect.py +13 -9
  21. package/scripts/cut.py +4 -4
  22. package/scripts/export.py +16 -15
  23. package/scripts/fit.py +8 -4
  24. package/scripts/freeze.py +38 -10
  25. package/scripts/graphics.py +104 -67
  26. package/scripts/grid.py +49 -7
  27. package/scripts/insert.py +9 -1
  28. package/scripts/join.py +29 -19
  29. package/scripts/look.py +41 -12
  30. package/scripts/loop.py +45 -6
  31. package/scripts/multicam.py +16 -7
  32. package/scripts/overlay.py +68 -14
  33. package/scripts/proxy.py +8 -4
  34. package/scripts/redact.py +10 -5
  35. package/scripts/reverse.py +27 -2
  36. package/scripts/scenes.py +16 -5
  37. package/scripts/sequence.py +6 -2
  38. package/scripts/silence.py +91 -35
  39. package/scripts/speedramp.py +17 -5
  40. package/scripts/straighten.py +17 -5
  41. package/scripts/sync.py +8 -2
  42. package/scripts/waveform.py +8 -3
@@ -12,10 +12,12 @@ Examples:
12
12
  python3 reverse.py input.mp4 --no-audio -o backwards.mp4
13
13
  """
14
14
  import argparse
15
+ import math
15
16
  import sys
16
17
 
17
18
  from _common import add_common, apply_common, aac_args, cfr_args, default_output, die, emit, ffmpeg_base, info, probe, run, video_args, X264_PRESETS, fmt_secs
18
19
  from _common.emit import warn
20
+ from _common.decision import av_clip_length, av_trim, frame_durations, frame_rate, stream_starts
19
21
 
20
22
 
21
23
  def main() -> int:
@@ -39,9 +41,32 @@ def main() -> int:
39
41
  warn(f"reverse buffers every decoded frame in memory; {meta['duration']:.0f}s of "
40
42
  f"{meta['video']['width']}x{meta['video']['height']} can exhaust RAM -- cut the part to reverse first (cut.py)",
41
43
  "memory_heavy", duration=meta["duration"])
42
- cmd = ffmpeg_base() + ["-i", args.input, "-vf", "reverse"]
44
+ cmd = ffmpeg_base() + ["-i", args.input]
43
45
  if has_audio:
44
- cmd += ["-af", "areverse"]
46
+ # Both streams are reversed about one end (#409); each reversed about its own end put a sound
47
+ # 0.6 s longer than its picture 0.396 s off. As in a join, both start at the first frame (the
48
+ # sound shifted by the same constant, padded when it starts late) and end together, on the
49
+ # first frame time at or after the clip's one length (av_clip_length: the picture's, or the
50
+ # sound's when it runs more than a frame past it). The last frame is held and the sound
51
+ # padded with silence to that end; the end is a frame time because a copied cut's stated
52
+ # video length can run past its last frame (7.517 s for frames ending at 7.5). One graph for
53
+ # both streams, as pad.py's padding needs. areverse keeps its input's timestamps in order,
54
+ # so the reversed sound is retimed by its samples. The hold (tpad) runs before av_trim's
55
+ # setpts: from FFmpeg 7.0 setpts leaves its output's frame rate unknown and its frames'
56
+ # durations 0, so a tpad after it clones nothing (the picture stayed 0.6 s short on 7.1);
57
+ # the closing fps gives the frames their durations back (frame_durations).
58
+ rate = frame_rate(meta) or meta["video"].get("fps") or 30.0
59
+ length = av_clip_length(meta, rate)
60
+ vlen = meta["video"].get("duration") or length
61
+ end = math.ceil(length * rate - 1e-6) / rate
62
+ vtrim, atrim = av_trim(stream_starts(meta)[0], None)
63
+ cmd += ["-filter_complex",
64
+ f"[0:v:0]tpad=stop_mode=clone:stop_duration={max(0.0, end - vlen) + 2 / rate:.6f},{vtrim},"
65
+ f"trim=end={end - 0.5 / rate:.6f},reverse,{frame_durations(meta, rate)}[v];"
66
+ f"[0:a:0]{atrim},apad=whole_dur={end:.6f},atrim=end={end:.6f},areverse,asetpts=N/SR/TB[a]",
67
+ "-map", "[v]", "-map", "[a]"]
68
+ else:
69
+ cmd += ["-vf", "reverse"]
45
70
  cmd += video_args(meta, args.crf, args.preset)
46
71
  cmd += cfr_args(meta)
47
72
  if has_audio:
package/scripts/scenes.py CHANGED
@@ -29,6 +29,8 @@ from _common import (guard_sidecar, detect_scenes, STATE, add_common, apply_comm
29
29
  from _common import drawtext_fontfile # noqa: E402
30
30
  from _common import ffmpeg_filters, filter_missing_hint # noqa: E402
31
31
  from _common.emit import note, record_fallback # noqa: E402
32
+ from _common.color import bt709_label_filter # noqa: E402
33
+ from _common.probe import square_pixels # noqa: E402
32
34
 
33
35
  SHOT_FPS = 4.0 # frames/second sampled for --shots' flow estimate
34
36
  SHOT_W, SHOT_H = 48, 27 # decode size for --shots (16:9-ish; enough blocks, still a few KB/shot)
@@ -285,9 +287,13 @@ def main() -> int:
285
287
  cols = min(4, max(1, n))
286
288
  rows = max(1, math.ceil(n / cols))
287
289
  tile_w = 1280 // cols // 2 * 2
288
- # exactly one frame per scene: the frame index at the scene start
289
- fps = meta["video"].get("fps") or 30.0
290
- expr = "+".join(f"eq(n\\,{int(round(sc['start'] * fps))})" for sc in scenes)
290
+ # exactly one frame per scene: the first frame at or after the scene start. The starts are
291
+ # presentation times, so the frame is chosen by its time, not by start x fps as a frame
292
+ # number -- on a variable-frame-rate source that number is another frame (#411)
293
+ def first_at(t: float) -> str:
294
+ a = f"{max(0.0, t - 0.0006):.4f}"
295
+ return f"gte(t\\,{a})*(isnan(prev_t)+lt(prev_t\\,{a}))"
296
+ expr = "+".join(first_at(sc["start"]) for sc in scenes)
291
297
  stamp = ""
292
298
  if not args.no_timecode and ffmpeg_filters() and "drawtext" not in ffmpeg_filters():
293
299
  # as look.py (#394): the sheet needs drawtext only for its stamps
@@ -300,9 +306,14 @@ def main() -> int:
300
306
  default_font = default_font_file("DejaVu Sans")
301
307
  font_prefix = f"{drawtext_fontfile(default_font)}:" if default_font else ""
302
308
  stamp = f",drawtext=text='%{{pts\\:hms}}':{font_prefix}fontcolor=white:fontsize=h/14:box=1:boxcolor=black@0.55:boxborderw=4:x=6:y=6"
303
- vf = (f"select='{expr}',scale={tile_w}:-2{stamp},"
309
+ # an untagged source is labelled BT.709 (tags only), or the sheet's RGB read it as BT.601 (#410);
310
+ # a non-square-pixel source is squared at its display aspect (#411)
311
+ lab = bt709_label_filter(meta)
312
+ square = square_pixels(meta["video"])
313
+ vf = (f"select='{expr}'," + (lab + "," if lab else "") + (square + "," if square else "")
314
+ + f"scale={tile_w}:-2{stamp},"
304
315
  f"tile={cols}x{rows}:padding=2:margin=2:color=0x202020")
305
- run(ffmpeg_base() + ["-i", args.input, "-vf", vf, "-frames:v", "1", "-fps_mode", "vfr", "-update", "1", args.sheet])
316
+ run(ffmpeg_base() + ["-i", args.input, "-an", "-vf", vf, "-frames:v", "1", "-fps_mode", "vfr", "-update", "1", args.sheet])
306
317
  info(f"wrote {args.sheet}")
307
318
  result["sheet"] = args.sheet
308
319
 
@@ -21,7 +21,8 @@ import tempfile
21
21
  from pathlib import Path
22
22
 
23
23
  from _common import add_common, apply_common, default_output, die, emit, ffmpeg_base, info, probe, run, video_args, X264_PRESETS, concat_list_line, fmt_secs
24
- from _common.emit import warn
24
+ from _common.emit import note, warn
25
+ from _common.probe import display_size, square_pixels_note
25
26
 
26
27
 
27
28
  def even(n: float) -> int:
@@ -105,7 +106,10 @@ def main() -> int:
105
106
  frame_meta = probe(str(frames[0]))
106
107
  if not frame_meta.get("video"):
107
108
  die(f"{frames[0]} is not a readable image")
108
- sw, sh = frame_meta["video"]["width"], frame_meta["video"]["height"]
109
+ # the displayed frame: an EXIF rotation applied, non-square pixels squared (#411)
110
+ sw, sh = display_size(frame_meta["video"])
111
+ if square_pixels_note(frame_meta["video"], "the first frame"):
112
+ note(square_pixels_note(frame_meta["video"], "the first frame"))
109
113
 
110
114
  if args.width and args.height:
111
115
  out_w, out_h = even(args.width), even(args.height)
@@ -3,7 +3,8 @@
3
3
 
4
4
  Detects quiet stretches with ffmpeg's silencedetect, keeps a margin on each
5
5
  side so words are not clipped, drops gaps shorter than --min-silence, and
6
- writes a frame-accurate re-encode in one pass (select, and ametadata for the sound).
6
+ writes a frame-accurate re-encode: the kept ranges are re-cut from the source the way
7
+ cut.py --segments --accurate cuts them (both streams on one set of frame-grid bounds).
7
8
 
8
9
  Examples:
9
10
  python3 silence.py talk.mp4 # -35 dB, gaps >= 0.6 s, 0.2 s margin
@@ -13,6 +14,7 @@ Examples:
13
14
  """
14
15
  import argparse
15
16
  import json
17
+ import math
16
18
  import os
17
19
  import sys
18
20
  from typing import Any, Dict, List, Optional, Tuple
@@ -25,7 +27,8 @@ from _common import asr as _asr
25
27
  from _common.emit import warn
26
28
  from _common import guard_sidecar, refuse_existing_outputs, refuse_output_is_input, _check_output_path
27
29
  from _common.emit import refuse
28
- from _common import detect_silences as detect, STATE, video_args, add_common, apply_common, audio_codec_for, cfr_args, default_output, die, emit, ffmpeg_base, info, is_audio_output, print_json, probe, run, X264_PRESETS, measured_level_dbfs, fmt_secs
30
+ from _common import detect_silences as detect, STATE, add_common, apply_common, default_output, die, emit, info, is_audio_output, print_json, probe, X264_PRESETS, measured_level_dbfs, fmt_secs
31
+ from _common.decision import frame_rate, stream_starts
29
32
 
30
33
 
31
34
 
@@ -80,10 +83,29 @@ def speech_aware_silences(path: str, threshold: float, min_silence: float) -> "T
80
83
  return boundaries, breaths
81
84
 
82
85
 
86
+ def inside_margin(silences: List[Tuple[float, float]], duration: float, margin: float) -> List[Tuple[float, float]]:
87
+ """Each silence with --margin kept on both sides: the stretch actually removed. A silence too
88
+ short to hold two margins removes nothing (it is dropped, never turned inside out).
89
+
90
+ The margin is applied to the silences alone, before they are merged with filler spans (#414):
91
+ a filler word's span is the word itself plus --filler-pad, and taking a 0.2 s margin off each
92
+ side of it turned every word narrower than 2 x --margin inside out -- kept, and still counted
93
+ as removed."""
94
+ out: List[Tuple[float, float]] = []
95
+ for s, e in silences:
96
+ a = s + margin
97
+ b = duration if e == float("inf") else min(duration, e - margin)
98
+ if b > a:
99
+ out.append((a, b))
100
+ return out
101
+
102
+
83
103
  def keep_ranges(silences: List[Tuple[float, float]], duration: float, margin: float, min_keep: float) -> List[Tuple[float, float]]:
84
104
  keeps: List[Tuple[float, float]] = []
85
105
  cursor = 0.0
86
106
  for s, e in sorted(silences):
107
+ if e != float("inf") and e - margin <= s + margin:
108
+ continue # narrower than two margins: nothing of it is removed (#414)
87
109
  s_adj = max(cursor, s + margin)
88
110
  if s_adj - cursor >= min_keep:
89
111
  keeps.append((cursor, s_adj))
@@ -97,16 +119,38 @@ def keep_ranges(silences: List[Tuple[float, float]], duration: float, margin: fl
97
119
  return keeps
98
120
 
99
121
 
122
+ def rendered_ranges(keeps: List[Tuple[float, float]], meta: Dict[str, Any], duration: float,
123
+ video_out: bool) -> List[Tuple[float, float]]:
124
+ """The source ranges the render holds, and is given: with a picture, each bound moves to the
125
+ nearest frame time, for the sound too, so cut.py's re-cut (which cuts at the first frame at or
126
+ after a bound) cuts exactly there. A measured bound sits a few microseconds off a frame
127
+ (silencedetect's 4.00002), which "at or after" would have turned into a whole frame dropped.
128
+ Sound alone is cut where asked. --timemap is built from these, so its times are the output's."""
129
+ rate = frame_rate(meta) if video_out else 0.0
130
+ if not rate:
131
+ return list(keeps)
132
+ v0 = stream_starts(meta)[0]
133
+
134
+ def snap(t: float) -> float:
135
+ return min(v0 + round((t - v0) * rate) / rate, duration)
136
+ return [(a, b) for a, b in ((snap(s), snap(e)) for s, e in keeps) if b > a]
137
+
138
+
139
+ def _edl_time(t: float, on_frames: bool) -> str:
140
+ """An EDL bound: a frame time truncated to the millisecond (below 1000 fps the frame at or
141
+ after it is still that frame), any other time rounded as before."""
142
+ return f"{math.floor(t * 1000 + 1e-6) / 1000:.3f}" if on_frames else f"{t:.3f}"
143
+
144
+
100
145
  def time_map(keeps: List[Tuple[float, float]], duration: float) -> Dict[str, Any]:
101
146
  """Each kept range with its time in the source and in the output, and each removed range with
102
- the output time it was cut at: re-timing subtitles, or finding a removed sentence again."""
147
+ the output time it was cut at: re-timing subtitles, or finding a removed sentence again.
148
+ `keeps` are the ranges the render holds (rendered_ranges): the output advances by each one's
149
+ exact length, rounded only for display."""
103
150
  kept: List[Dict[str, float]] = []
104
151
  removed: List[Dict[str, float]] = []
105
152
  out, prev = 0.0, 0.0
106
153
  for s, e in keeps:
107
- # the render cuts on bounds rounded to the millisecond (between(t,s,e) below): advance by
108
- # that rounded length, or every off-grid range adds its own error to every later time
109
- s, e = round(s, 3), round(e, 3)
110
154
  if s - prev > 0.0005:
111
155
  removed.append({"source_start": round(prev, 3), "source_end": round(s, 3), "output_time": round(out, 3)})
112
156
  kept.append({"source_start": round(s, 3), "source_end": round(e, 3),
@@ -478,6 +522,9 @@ def main() -> int:
478
522
  media_out = None if (args.list or args.filler_list) else (args.output or default_output(args.input, "tight"))
479
523
  guard_sidecar(args.edl, args.input, args.words, args.filler_words, siblings=(media_out,))
480
524
 
525
+ if not math.isfinite(args.margin) or args.margin < 0:
526
+ # nan made inside_margin() drop every silence and re-encode the whole file unchanged
527
+ die(f"--margin must be a finite number of seconds, 0 or more (got {args.margin:g})", kind="input")
481
528
  if args.filler_pad < 0:
482
529
  die("--filler-pad cannot be negative: a negative pad turns each word's span inside out "
483
530
  "(end before start) and the span is then silently dropped, so nothing is removed",
@@ -500,9 +547,15 @@ def main() -> int:
500
547
  filler_info, filler_ranges = resolve_filler(args, meta) if args.filler else (None, [])
501
548
  # One sorted, merged removal list through the graph the tool already has: filler removal IS
502
549
  # time-range removal, so it reuses keep_ranges() and the same select chain.
503
- removals = merge_spans(list(silences) + list(filler_ranges))
504
- keeps = keep_ranges(removals, duration, args.margin, args.min_keep)
505
- kept = sum(e - s for s, e in keeps)
550
+ # --margin is kept around the silences only (inside_margin), not around filler spans, whose
551
+ # own trim is --filler-pad (#414); the merged list then goes through with no further margin.
552
+ removals = merge_spans(inside_margin(silences, duration, args.margin) + list(filler_ranges))
553
+ keeps = keep_ranges(removals, duration, 0.0, args.min_keep)
554
+ # what the render holds (on a picture, each bound on its nearest frame): the report, the EDL
555
+ # and the time map all describe these, so a re-cut or a sum of `keep` is the rendered edit
556
+ video_out = bool(meta.get("video")) and not (media_out and is_audio_output(media_out))
557
+ held = rendered_ranges(keeps, meta, duration, video_out)
558
+ kept = sum(e - s for s, e in held)
506
559
  removed = max(0.0, duration - kept)
507
560
  # `removed_seconds` keeps the meaning it has had since this tool existed: the seconds of
508
561
  # SILENCE this run removes. It must not quietly start counting filler time as well, because a
@@ -511,12 +564,12 @@ def main() -> int:
511
564
  # the silences alone; everything removed is `removed_seconds_total`.
512
565
  silence_only = removed
513
566
  if args.filler:
514
- silence_keeps = keep_ranges(merge_spans(list(silences)), duration, args.margin, args.min_keep)
515
- silence_only = max(0.0, duration - sum(e - s for s, e in silence_keeps))
567
+ silence_keeps = keep_ranges(merge_spans(inside_margin(silences, duration, args.margin)), duration, 0.0, args.min_keep)
568
+ silence_only = max(0.0, duration - sum(e - s for s, e in rendered_ranges(silence_keeps, meta, duration, video_out)))
516
569
  kinds = silence_kinds(args.input, silences, duration, args.threshold)
517
570
  summary = {
518
571
  "silences": [[round(s, 3), None if e == float("inf") else round(e, 3)] for s, e in silences],
519
- "keep": [[round(s, 3), round(e, 3)] for s, e in keeps],
572
+ "keep": [[round(s, 3), round(e, 3)] for s, e in held],
520
573
  "input_duration": round(duration, 3),
521
574
  "kept_duration": round(kept, 3),
522
575
  "removed_seconds": round(silence_only, 3),
@@ -572,7 +625,7 @@ def main() -> int:
572
625
  summary["hint"] = f"no passage sits below {args.threshold:g} dBFS for {args.min_silence:g}s; try a higher --threshold (e.g. -25) or a shorter --min-silence"
573
626
  info("hint: " + summary["hint"])
574
627
 
575
- tm = time_map(keeps, duration)
628
+ tm = time_map(held, duration)
576
629
  points: Any = False # False: not measured (a dry run over a pending input); None: the decode failed
577
630
  if not (STATE.dry_run and not os.path.exists(args.input)):
578
631
  points = cut_levels(args.input, tm, duration, list(filler_ranges))
@@ -604,8 +657,11 @@ def main() -> int:
604
657
  if args.edl:
605
658
  if not STATE.dry_run: # the EDL is an artifact like the cut itself: a plan writes nothing
606
659
  with open(args.edl, "w", encoding="utf-8") as fh:
607
- for s, e in keeps:
608
- fh.write(f"{s:.3f}-{e:.3f}\n")
660
+ on_frames = bool(video_out and frame_rate(meta))
661
+ for s, e in held:
662
+ # a frame-time bound is written down to the millisecond, not rounded up past
663
+ # its frame: cut.py --segments cuts at the first frame at or after it (#416 review)
664
+ fh.write(f"{_edl_time(s, on_frames)}-{_edl_time(e, on_frames)}\n")
609
665
  info(f"wrote {args.edl}")
610
666
 
611
667
  if args.list or args.filler_list:
@@ -615,29 +671,29 @@ def main() -> int:
615
671
  else:
616
672
  print_json(summary)
617
673
  return 0
618
- if not keeps:
674
+ if not keeps or not held:
619
675
  die("nothing would be kept; raise --threshold (e.g. -45) or check the audio")
620
- if not silences or removed < 0.05:
621
- info("no removable silence found; output would equal the input")
676
+ if removed < 0.05:
677
+ # what the render removes, filler spans included: --filler alone has no silences at all
678
+ info(f"no removable {'filler' if filler_only else 'silence or filler' if args.filler else 'silence'} found; output would equal the input")
622
679
 
623
680
  output = args.output or default_output(args.input, "tight")
624
- expr = "+".join(f"between(t,{s:.3f},{e:.3f})" for s, e in keeps)
625
- vf = f"select='{expr}',setpts=N/FRAME_RATE/TB"
626
- # Not `aselect='{expr}'`: FFmpeg 8.0 and 8.0.1 (Ubuntu 26.04's apt build) ship an aselect that
627
- # passes every frame through (its activate callback was lost when the filter was converted;
628
- # fixed in 8.0.2/8.1), which cut the picture and left the sound whole. Tagging the frames the
629
- # expression keeps (timeline `enable`, the same per-frame t) and selecting the tagged ones keeps
630
- # exactly the frames aselect keeps, on every release, in a command no longer than before.
631
- af = (f"ametadata=mode=add:key=ffskill_keep:value=1:enable='{expr}',"
632
- "ametadata=mode=select:key=ffskill_keep,asetpts=N/SR/TB")
633
- cmd = ffmpeg_base() + ["-i", args.input]
634
- audio_only = is_audio_output(output) or not meta.get("video")
635
- if audio_only:
636
- cmd += ["-vn"]
637
- else:
638
- cmd += ["-vf", vf] + video_args(meta, args.crf, args.preset) + cfr_args(meta)
639
- cmd += ["-af", af] + audio_codec_for(output) + [output]
640
- run(cmd)
681
+ # the join of more than 32 ranges concats a list of chunks, so its last command never names the
682
+ # source and run()'s own check cannot see -o replace it (#416 review)
683
+ refuse_output_is_input(output, args.input)
684
+ # The kept ranges are re-cut from the source as cut.py --segments --accurate cuts them (#409):
685
+ # each range is its own seeked input, both streams cut on the same frame-grid bounds and shifted
686
+ # by one constant, the sound padded to the range's origin, at most 32 ranges per ffmpeg call.
687
+ # The one-pass select/ametadata graph this replaced rounded every range twice, the picture to
688
+ # whole frames and the sound to whole AAC frames, and the errors added up (150 ms over 21 ranges
689
+ # on a 61 s source); its asetpts=N/SR also moved a sound that starts late by its whole offset.
690
+ # No declick fade: the joins sit in the --margin the tool keeps, as they always have.
691
+ # imported here, not at the top: the contract and MCP load every tool's parser on its own
692
+ import cut as _cut
693
+ _cut.DECLICK_S = 0.0
694
+ import tempfile
695
+ with tempfile.TemporaryDirectory(prefix="ffskill_silence_") as tmp:
696
+ _cut.join_from_source(args.input, held, output, meta, args.crf, args.preset, tmp)
641
697
  r = probe(output, role="output")
642
698
  info(f"wrote {output} ({fmt_secs(r['duration'])}, expected ~{kept:.3f}s)")
643
699
  finish_time_map()
@@ -19,6 +19,7 @@ import argparse
19
19
  import sys
20
20
  from typing import List, Tuple
21
21
 
22
+ from _common.decision import av_trim, frame_rate, grid_at_or_after, stream_starts
22
23
  from _common import add_common, apply_common, aac_args, default_output, die, emit, ffmpeg_base, info, probe, run, video_args, X264_PRESETS, fmt_secs, time_arg
23
24
 
24
25
  MAX_SPEED = 20.0
@@ -89,15 +90,26 @@ def main() -> int:
89
90
  # the last factor. The end bound is omitted only when the stated end is the file's end
90
91
  # (within 50 ms): a trim a few milliseconds short of the last packet drops that packet.
91
92
  last_runs_to_eof = abs(segments[-1][1] - dur) <= 0.05
93
+ # Every boundary is a frame time and both streams of a segment are cut on it and shifted by the
94
+ # same constant before the speed change (#409), as cut.py's re-cut does. Rebasing each stream
95
+ # of each segment to its own first sample moved a sound that starts 0.3 s late 0.3 s early, and
96
+ # the factor scaled that (+570 ms at 0.5x). The first segment starts at the first frame.
97
+ v0 = stream_starts(meta)[0]
98
+ rate = frame_rate(meta)
99
+ bounds = [v0] + [grid_at_or_after(end, v0, rate) for _, end, _ in segments[:-1]]
92
100
  for i, (start, end, factor) in enumerate(segments):
93
101
  vlabel, alabel = f"v{i}", f"a{i}"
94
- end_expr = f"{end:.3f}" if not (i == len(segments) - 1 and last_runs_to_eof) else None
95
- trim = f"trim=start={start:.3f}" + (f":end={end_expr}" if end_expr else "")
96
- vparts.append(f"[0:v]{trim},setpts=(PTS-STARTPTS)/{factor:.6f}[{vlabel}]")
102
+ to_eof = i == len(segments) - 1 and last_runs_to_eof
103
+ seg_end = None if to_eof else (bounds[i + 1] if i + 1 < len(bounds) else grid_at_or_after(end, v0, rate))
104
+ if seg_end is not None and seg_end <= bounds[i] + 1e-6:
105
+ # both ends on the same frame: the trims would be empty and the factor reach no frame
106
+ die(f"--segment {start:g}-{end:g}:{factor:g} holds no frame: both ends fall on the frame at "
107
+ f"{bounds[i]:.3f}s; widen it or merge it into a neighbour")
108
+ vtrim, atrim = av_trim(bounds[i], seg_end, rate)
109
+ vparts.append(f"[0:v]{vtrim},setpts=PTS/{factor:.6f}[{vlabel}]")
97
110
  labels.append(f"[{vlabel}]")
98
111
  if has_audio:
99
- atrim = f"atrim=start={start:.3f}" + (f":end={end_expr}" if end_expr else "")
100
- aparts.append(f"[0:a]{atrim},asetpts=PTS-STARTPTS,{atempo_chain(factor)}[{alabel}]")
112
+ aparts.append(f"[0:a]{atrim},{atempo_chain(factor)}[{alabel}]")
101
113
 
102
114
  if has_audio:
103
115
  concat_inputs = "".join(f"[v{i}][a{i}]" for i in range(len(segments)))
@@ -23,6 +23,8 @@ import math
23
23
  import sys
24
24
 
25
25
  from _common import add_common, apply_common, aac_args, cfr_args, default_output, die, drawn_chain, emit, ffmpeg_base, info, probe, run_keeping_subtitles, validate_color, video_args, X264_PRESETS, fmt_secs
26
+ from _common.emit import note
27
+ from _common.probe import display_size, square_pixels, square_pixels_note
26
28
 
27
29
 
28
30
  def main() -> int:
@@ -68,15 +70,25 @@ def main() -> int:
68
70
  rotate = drawn_chain(f"rotate={radians:.8f}:fillcolor={args.fill_color}:{geometry}", meta, [args.fill_color],
69
71
  f"rotate={radians:.8f}:fillcolor=black:{geometry}")
70
72
  else:
71
- # Pre-scale the frame up by a safe, conservative factor (|cos|+|sin|, the exact growth
72
- # factor for a square, over-generous for a rectangle) so the rotated content fully
73
- # covers the original W:H window with no black corner, then rotate in place (canvas
74
- # stays at the scaled size) and crop back down to the original W:H, centred.
75
- s = abs(math.cos(radians)) + abs(math.sin(radians))
73
+ # Pre-scale the frame up so the rotated content fully covers the original W:H window with
74
+ # no black corner, then rotate in place (canvas stays at the scaled size) and crop back
75
+ # down to the original W:H, centred. The window turned back by the angle must fit inside
76
+ # the scaled frame: W*cos + H*sin <= s*W and W*sin + H*cos <= s*H, so
77
+ # s = |cos| + max(W/H, H/W) * |sin| for the displayed (squared, upright) frame. Through
78
+ # 3.1.5 it was |cos| + |sin|, a square's factor, which left two corners black on 16:9
79
+ # (#416 review).
80
+ dw, dh = display_size(meta["video"])
81
+ aspect = max(dw / dh, dh / dw) if dw and dh else 1.0
82
+ s = abs(math.cos(radians)) + aspect * abs(math.sin(radians))
76
83
  # crop dimensions must round down to even (4:2:0 chroma); trunc(.../2)*2 floors to the
77
84
  # nearest even value instead of leaving an odd width/height that the encoder would refuse.
78
85
  rotate = f"scale=iw*{s:.8f}:ih*{s:.8f},rotate={radians:.8f},crop=trunc(iw/{s:.8f}/2)*2:trunc(ih/{s:.8f}/2)*2"
79
86
 
87
+ # a rotation of non-square pixels is a shear on screen: the picture is squared at its
88
+ # display aspect first (#411)
89
+ if square_pixels(meta["video"]):
90
+ rotate = square_pixels(meta["video"]) + "," + rotate
91
+ note(square_pixels_note(meta["video"]))
80
92
  cmd = ffmpeg_base() + ["-i", args.input, "-vf", rotate, "-map", "0:v:0"]
81
93
  if has_audio:
82
94
  cmd += ["-map", f"0:a:{args.audio_stream}?"]
package/scripts/sync.py CHANGED
@@ -34,6 +34,7 @@ from typing import Dict, List
34
34
 
35
35
  from _common import video_args, add_common, apply_common, emit, aac_args, audio_codec_for, default_output, die, ffmpeg_base, info, probe, require_tool, run, run_analysis, x264_args, decode_pcm_mono, rms_envelope
36
36
  from _common.emit import record_skip, warn
37
+ from _common.decision import stream_starts
37
38
 
38
39
  SR = 8000 # decode sample rate
39
40
 
@@ -355,6 +356,11 @@ def main() -> int:
355
356
  if abs(drift_ratio - 1.0) > 1e-7:
356
357
  # the second file runs long by drift_ratio -> play it faster by that ratio (pitch shift is ~ppm, inaudible)
357
358
  drift_af = [f"asetrate={sec_sr * drift_ratio:.6f}", f"aresample={sec_sr}"]
359
+ # the offset is measured between the files' clocks (decode_pcm_mono pads a late sound), so
360
+ # the second's sound is placed on its own clock first: one that starts 0.3 s after its
361
+ # file's start keeps that 0.3 s instead of being delayed from its first sample (#409).
362
+ # Only then, so the command for an ordinary file is the one it always was.
363
+ on_clock = ["aresample=async=1:first_pts=0"] if stream_starts(second_meta)[1] > 0.0005 else []
358
364
 
359
365
  if args.replace_audio:
360
366
  cmd = ffmpeg_base() + ["-i", args.reference]
@@ -367,7 +373,7 @@ def main() -> int:
367
373
  # its length instead of -shortest, which cut the reference's tail whenever the second
368
374
  # file started earlier (head_trim > 0) or was simply shorter
369
375
  ref_dur = probe(args.reference).get("duration")
370
- af_parts = drift_af + ([f"adelay={delay_ms}:all=1"] if delay_ms > 0 else []) + ["apad"]
376
+ af_parts = on_clock + drift_af + ([f"adelay={delay_ms}:all=1"] if delay_ms > 0 else []) + ["apad"]
371
377
  cmd += ["-af", ",".join(af_parts)]
372
378
  cmd += aac_args() + (["-t", f"{ref_dur:.3f}"] if ref_dur else ["-shortest"]) + [output]
373
379
  proc = run(cmd, check=False)
@@ -391,7 +397,7 @@ def main() -> int:
391
397
  if head_trim > 0:
392
398
  cmd += ["-ss", f"{head_trim:.4f}"]
393
399
  cmd += ["-i", args.second]
394
- af_parts = list(drift_af)
400
+ af_parts = on_clock + drift_af
395
401
  if delay_ms > 0:
396
402
  af_parts.append(f"adelay={delay_ms}:all=1")
397
403
  if has_video:
@@ -35,12 +35,12 @@ import os
35
35
  import sys
36
36
 
37
37
  from _platforms import PLATFORMS, PLATFORM_CHOICES, resolve as resolve_platform
38
- from _common.probe import SILENT_PEAK_DBFS
38
+ from _common.probe import SILENT_PEAK_DBFS, square_pixels, square_pixels_note
39
39
  from _common import still_input_args # noqa: E402
40
40
  from _common import (add_common, apply_common, aac_args, default_output, die, draws_chroma, emit, ffmpeg_base,
41
41
  info, load_brand, pad_filters, probe, rgb_chain_to_bt709, run, STATE, validate_color, video_args, X264_PRESETS,
42
42
  fmt_secs, child_args, run_tool, measured_level_dbfs, sibling_temp)
43
- from _common.emit import absorb_stage_hw, warn
43
+ from _common.emit import absorb_stage_hw, note, warn
44
44
 
45
45
  WAVEFORM_MODES = ["point", "line", "p2p", "cline"]
46
46
 
@@ -184,7 +184,12 @@ def main() -> int:
184
184
  plate = pad_filters(args.width, args.height,
185
185
  "blur" if args.image_fit == "blur" else "color",
186
186
  args.background, 20)
187
- graph = f"[1:v]{plate},format=rgba[plate];[0:a:{args.audio_stream}]{vf},format=rgba"
187
+ # a non-square-pixel still is squared at its display aspect before it is fitted (#411)
188
+ plate_v = probe(args.image, role="input").get("video") if not STATE.dry_run else None
189
+ plate_sq = square_pixels(plate_v)
190
+ if plate_sq:
191
+ note(square_pixels_note(plate_v, "--image"))
192
+ graph = f"[1:v]{plate_sq + ',' if plate_sq else ''}{plate},format=rgba[plate];[0:a:{args.audio_stream}]{vf},format=rgba"
188
193
  if args.opacity < 1.0:
189
194
  graph += f",colorchannelmixer=aa={args.opacity:g}"
190
195
  # the composite is RGBA (the plate is): converted to the BT.709 the output is tagged with,