ffmpeg-skill 2.7.0 → 2.8.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.
package/README.md CHANGED
@@ -202,7 +202,7 @@ These are the rules the skill file gives the agent and the code enforces.
202
202
 
203
203
  | Tool | What it does |
204
204
  |---|---|
205
- | `cut.py` | In/out or multi-segment cuts, lossless `-c copy` first, re-encode fallback, `--accurate` for frame-exact video and sample-exact audio; reports `precision`; `--snap beats` moves the in/out points onto a measured beat, or refuses when there is no measurable pulse |
205
+ | `cut.py` | In/out or multi-segment cuts, lossless `-c copy` first, re-encode fallback, `--accurate` for frame-exact video and sample-exact audio, `--keep-hevc` to re-encode an HEVC source as HEVC, `--edit-list` to keep an `.mp4` copy's start exact, `--vfr-guard` to choose the VFR check; reports `precision`; `--snap beats` moves the in/out points onto a measured beat, or refuses when there is no measurable pulse |
206
206
  | `join.py` | Concatenate clips with xfade transitions, normalising size, fps, sample rate and channel layout (the widest clip's, or `--channels`); audio-only inputs are joined as audio; `--list parts.txt` reads generated segments (TTS lines) and checks every one first — missing, empty or unreadable parts are all named in one refusal, or skipped and reported with `--on-missing skip` |
207
207
  | `silence.py` | Detect and remove dead air (jump cuts) with a margin around speech; list or export the cut list; `--filler` also removes filler words, but only where a speech engine timed them; `--speech-aware` keeps a breath inside a sentence and cuts only at sentence-boundary pauses, composing with `--filler` through the same `keep_ranges()` |
208
208
  | `fit.py` | Fit to a duration (pitch-preserving speed change or trim, smooth slow-mo) and/or aspect ratio (pad, crop or `--fit blur`'s blurred, darkened fill, with `--crop-x`/`--crop-y` to keep an off-centre subject) and/or exact `--width`/`--height`; rotate 90/180/270, flip h/v; force constant fps |
@@ -362,8 +362,8 @@ npx ffmpeg-skill doctor --json # available / missing / missing_optional / unkn
362
362
 
363
363
  The short list for humans. The agent-facing version, with the reasoning, is the "Things that look right but are wrong" and "Gotchas" sections of [SKILL.md](SKILL.md).
364
364
 
365
- - **Variable frame rate (phone and screen recordings).** `probe.py` flags it; every re-encoding tool conforms to a constant rate automatically, and `cut.py` switches to frame-accurate mode on its own because copy-cuts on VFR land on the wrong frame. Choose the rate yourself with `fit.py input.mp4 --fps 30` when the measured average is odd.
366
- - **Lossless cuts snap to keyframes.** A stream-copy cut can start up to one GOP earlier than asked. `cut.py` re-encodes when the snap exceeds 0.5 s (`--tolerance` changes the limit). For a strictly lossless file pass `--tolerance -1`, and expect the cut to land on the nearest earlier keyframe; the JSON result lists them under `nearest_keyframes`.
365
+ - **Variable frame rate (phone and screen recordings).** `probe.py` flags it; every re-encoding tool conforms to a constant rate automatically. `cut.py` switches to frame-accurate mode on it, because copy-cuts on VFR land on the wrong frame, and samples the frame timing to say whether that was needed (`--vfr-guard sampled` decides on the sample instead; `--vfr-guard off` keeps the copy). Choose the rate yourself with `fit.py input.mp4 --fps 30` when the measured average is odd.
366
+ - **Lossless cuts snap to keyframes.** A stream-copy cut can start up to one GOP earlier than asked. `cut.py` re-encodes when the snap exceeds 0.5 s (`--tolerance` changes the limit). For a strictly lossless file pass `--tolerance -1` (plus `--vfr-guard off` on a variable-frame-rate source), and expect the cut to land on the nearest earlier keyframe; the JSON result lists them under `nearest_keyframes`. A `--segments` join can still be re-cut: open-GOP video, a later segment of B-frame `.mp4`/`.mov` video that starts between keyframes (start it on a keyframe, or write `.mkv`), or a join that fails its check. For a single `.mp4`/`.mov` cut, `--edit-list` keeps the copy and starts the picture where asked: the keyframe's pre-roll is stored but hidden by an MP4 edit list, which a player that ignores edit lists shows.
367
367
  - **HDR stays HDR.** When the probe reports HDR (HDR10, HLG, Dolby Vision, BT.2020), the tools keep it rather than flatten it. Convert deliberately with `color.py --to-sdr` before H.264 deliverables or LUT work. `export.py` platform presets are SDR and warn on HDR input.
368
368
  - **Loudness targets.** −14 LUFS / −1 dBTP for YouTube and social platforms (the `loudness.py` default), `-I -16 --tp -1.5` for podcasts, `-I -23` for broadcast. A clip measured at −40 LUFS or below is room tone, not content; raising it raises the noise. Check true peak as well as LUFS: `check.py file --platform podcast` measures both.
369
369
  - **Frame changes first, text second.** Captions and overlays burned before a crop or resize end up off-frame. Reframe, then caption.
package/SKILL.md CHANGED
@@ -7,7 +7,7 @@ description: 'Edit video and audio with local FFmpeg from natural-language reque
7
7
 
8
8
  Scripts live in `scripts/` next to this file; run them with `python3 <skill-dir>/scripts/<name>.py`, and delivery templates in `templates/`. This file is enough to do a job: the table below routes the request, and `--help` on the script about to run is the cheapest full flag list. A reference file costs as much as this one; open one only for a question you have: `references/scripts.md` (every flag of all 42 scripts), `references/devices.md` (iPhone HDR, GoPro, DJI, screen recordings, Zoom), `references/gotchas.md` (the long form of the one-line rules at the end). MCP: `tools/list` shows only the core 12 by default; the other 30 (per this table) stay callable by name via `tools/call`, described by `contract --json`; set `FFMPEG_SKILL_MCP_FULL=1` for all 42.
9
9
 
10
- Shared flags (every script): `--dry-run`; `--json` (output path, its probe, the commands run); `--json-brief` (status/output/verified + a `summary`; prefer on writing steps); `--fast` (preview quality); `--progress` (percent/ETA); `--timeout SECONDS` (`kind: timeout`, default 1800); `--overwrite` (step 7); `--plan FILE` (a dry run `render.py FILE` runs later; refuses if an input changed). Re-encoding tools also take `--hw` (Apple Silicon GPU, big files; `FFMPEG_SKILL_HW=1` = default, not export), `--codec h264|hevc|av1|prores` and `--quality N`: unset, SDR is x264, HDR is x265 Main10; `prores` needs `-o NAME.mov`, `h264` refuses HDR (`color.py --to-sdr` first).
10
+ Shared flags (every script): `--dry-run`; `--json` (output path, its probe, the commands run); `--json-brief` (status/output/verified + a `summary`; prefer on writing steps); `--fast` (preview quality); `--progress` (percent/ETA); `--timeout SECONDS` (`kind: timeout`, default 1800); `--overwrite` (step 7); `--plan FILE` (a dry run `render.py FILE` runs later; refuses if an input changed). Re-encoding tools also take `--hw` (Apple Silicon GPU, big files; `FFMPEG_SKILL_HW=1` = default, not export), `--codec h264|hevc|av1|prores` and `--quality N`: unset, SDR is x264 (`cut.py --keep-hevc`: HEVC source stays HEVC), HDR is x265 Main10; `prores` needs `-o NAME.mov`, `h264` refuses HDR (`color.py --to-sdr` first).
11
11
 
12
12
  Writing tools run nothing under `--dry-run`; the measuring tools (`probe`, `check`, `sync`, `multicam`, `scenes`, `cropdetect`, `report`, `silence`, `loudness`, `stabilize`) may still run ffmpeg/ffprobe, skipping artifacts/side files (`--edl`, `--sheet`, a generated `.ass`); `verify` ignores the flag. Per tool: `contract --json` `dry_run`.
13
13
 
@@ -52,7 +52,7 @@ This skill cuts, joins, measures, syncs, exports and checks files — it execute
52
52
 
53
53
  The line: same input + same explicit parameters always producing the same verifiable output belongs here; anything depending on taste, understanding or what looks or sounds good belongs to whoever makes that judgement.
54
54
 
55
- If a request needs an FFmpeg feature none of the 42 scripts expose, say so and name the closest built-in option — never guess a raw `ffmpeg`/`ffprobe` invocation outside `scripts/*.py`. It bypasses every guarantee this skill makes, so never a fallback when a script's flag doesn't cover something.
55
+ If a request needs an FFmpeg feature none of the 42 scripts expose, say so and name the closest built-in option — never guess a raw `ffmpeg`/`ffprobe` invocation outside `scripts/*.py`. It bypasses every guarantee this skill makes, so it is never a fallback.
56
56
 
57
57
  ## Request → script
58
58
 
@@ -65,7 +65,7 @@ Timestamp flags (`--start`, `--end`, `--at`, `--from`, `--duration`, `--offset`,
65
65
  | "cut from 1:20 to 2:05", "trim the first 10 s" | `cut.py input.mp4 --start 1:20 --end 2:05` |
66
66
  | "keep only these parts", "remove the middle" | `cut.py input.mp4 --segments 0-1:00,1:30-2:00` |
67
67
  | "make it exactly 60 seconds" | `fit.py input.mp4 --duration 60` (speed) or `--method trim` |
68
- | "cut this and make it HEVC / AV1 / ProRes" (output codec named) | `cut.py input.mp4 --start 0:10 --end 0:40 --codec hevc` (`--codec`/`--quality` on any re-encoding tool; ProRes needs `-o NAME.mov`) |
68
+ | "cut this and make it HEVC / AV1 / ProRes" (output codec named) | `cut.py input.mp4 --start 0:10 --end 0:40 --codec hevc` (ProRes needs `-o NAME.mov`) |
69
69
  | "cut out the pauses", "jump cuts" | `silence.py input.mp4 [--threshold -40 --min-silence 0.8]` |
70
70
  | "cut the ums and uhs", "remove the filler words" | `silence.py input.mp4 --filler --words words.json` (measured word timings; `--transcribe` makes them) |
71
71
  | "don't cut inside a sentence, just the real pauses" | `silence.py input.mp4 --speech-aware` — a breath under `--min-silence` inside a sentence is kept, only sentence-boundary pauses cut; composes with `--filler` into one list |
@@ -206,8 +206,8 @@ One line each; open the linked `references/gotchas.md` section when the job is i
206
206
 
207
207
  - HDR (iPhone, HDR10) re-encoded through an SDR path goes flat; the scripts keep HDR; `hdr: true` is a real PQ/HLG/DV signal, `bt2020_or_hdr: true` also BT.2020 SDR. -> [#hdr-and-colour](references/gotchas.md#hdr-and-colour)
208
208
  - Log footage (S-Log/V-Log/C-Log) is tagged SDR and looks grey: `probe.py --analyze`, then `color.py --lut` first. -> [#log-footage](references/gotchas.md#log-footage)
209
- - A `-c copy` cut can start on a wrong or frozen frame; `cut.py` re-encodes past a 0.5 s snap, respect it. -> [#keyframe-cuts](references/gotchas.md#keyframe-cuts)
210
- - VFR phone/screen recordings: re-encodes conform to CFR, `cut.py` switches to `--accurate`; pick the rate with `fit.py --fps` when odd. -> [#variable-frame-rate](references/gotchas.md#variable-frame-rate)
209
+ - A `-c copy` cut can start on a wrong/frozen frame; `cut.py` re-encodes past a 0.5 s snap, respect it; `--edit-list` hides an .mp4's pre-roll. -> [#keyframe-cuts](references/gotchas.md#keyframe-cuts)
210
+ - VFR phone/screen clips: re-encodes go CFR; `cut.py` re-encodes (`--vfr-guard sampled`: only measured VFR); odd rate: `fit.py --fps`. -> [#variable-frame-rate](references/gotchas.md#variable-frame-rate)
211
211
  - Sync/multicam `confidence` under 0.3 (or a huge offset) is suspect — check every camera; these align audio, never lip sync. -> [#sync-multicam-and-drift](references/gotchas.md#sync-multicam-and-drift)
212
212
  - "Normalised" audio can still clip (check true peak); ambience at -40 LUFS or below must never be raised to a speech target. -> [#loudness-and-ambience](references/gotchas.md#loudness-and-ambience)
213
213
  - Captions burned before a crop/resize land off-frame; burned small then upscaled by `export.py` come out soft. -> [#captions-fonts-and-text-order](references/gotchas.md#captions-fonts-and-text-order)
package/docs/contract.md CHANGED
@@ -21,7 +21,7 @@ The contract is derived from the code that runs, not maintained beside it:
21
21
  | Field | Meaning | Changes when |
22
22
  |---|---|---|
23
23
  | `contract_version` | shape of this document (`1.0`) | a key is renamed, removed or changes meaning |
24
- | `skill.version` | the npm / package.json version (`2.7.0`) | any release |
24
+ | `skill.version` | the npm / package.json version (`2.8.0`) | any release |
25
25
 
26
26
  A release that adds a tool or a flag keeps `contract_version`; a breaking change to the
27
27
  ToolSpec shape bumps it. Consumers pin on `contract_version` and read `skill.version`
@@ -119,7 +119,7 @@ JSON would have been rewritten for that. The flat keys, typed per tool in `outpu
119
119
  "contract_version": "1.0",
120
120
  "deprecated": [],
121
121
  "removed": [{"what": "...", "since": "1.10.0", "replacement": "...", "removed_in": "2.0.0", "where": "cli | json | mcp | behaviour"}],
122
- "skill": {"id": "ffmpeg-skill", "version": "2.7.0", "execution_mode": "local", "kind": "execution",
122
+ "skill": {"id": "ffmpeg-skill", "version": "2.8.0", "execution_mode": "local", "kind": "execution",
123
123
  "entrypoints": {"cli": "...", "mcp": "...", "contract": "...", "doctor": "..."},
124
124
  "not_provided": ["AI reasoning", "decisions", "production plans", "project IR", "approvals", "network access", "transcription engine"]},
125
125
  "requirements": {"python": ">=3.9 (standard library only)", "ffmpeg": ">=5.0", "ffprobe": ">=5.0"},
@@ -216,7 +216,9 @@ BT.709 for SDR; av1 is SVT-AV1 with libaom as the fallback; prores is 422 HQ and
216
216
  `.mov`/`.mkv` output; h264 refuses an HDR source (`kind: input`). `--quality` is the CRF scale;
217
217
  its default is the tool's own (18, `proxy.py` 30). 1.x also accepted `--crf` as an alias; 2.0
218
218
  removed it (`export.py` keeps its own `--crf`). Without `--codec` the encoder is what it always was (x264 for SDR, x265
219
- Main10 for HDR), so the flags add no behaviour to a caller that does not pass them. The
219
+ Main10 for HDR), so the flags add no behaviour to a caller that does not pass them.
220
+ `cut.py --keep-hevc` (opt-in) re-encodes an SDR HEVC source as x265 8-bit BT.709 instead of x264; an HDR or
221
+ BT.2020 source keeps the Main10 line with its own colour tags, with or without the flag. The
220
222
  encoder each value needs is listed under the tool's optional capabilities (`--codec hevc` and
221
223
  so on). `export.py` refuses `--codec`: its presets decide the codec.
222
224
 
@@ -573,6 +575,12 @@ Per-tool keys added after 2.2.2, all additive:
573
575
  | `encoder`, `hw`, `notes` | every re-encoding tool, `export.py`, `render.py`, `batch.py` (and its rows), `waveform.py` | `encoder`: the video encoder the last command ran (after any GPU→CPU fallback; under `--dry-run`, the planned one), `copy` for a stream copy; absent when no command encoded video. A tool that runs other tools reports its stages': `render.py` its stages and an executed plan's tool, `waveform.py` the title/caption stage that writes its file (its `commands` include that stage's). `hw` is present when VideoToolbox was asked for (`--hw`, `FFMPEG_SKILL_HW=1`, or a stage or `batch.py` item that reported it) and on `export.py` when `FFMPEG_SKILL_HW=1` is set but its delivery preset does not take it: `{requested, source: flag\|env, used, fallback, notes}`. `requested` says whether this run's encodes were asked onto VideoToolbox (`false` for that export), `used` whether the encoder that ran last is VideoToolbox (`null` under `--dry-run`, which ran nothing, and when the tool encoded nothing itself: `batch.py`, whose top-level `hw` gathers its items' `requested`/`source`/`fallback`/`notes` while each `results` row carries the item's own `encoder`, and `hw`/`notes` when VideoToolbox was asked for), `fallback` whether a job VideoToolbox refused was re-encoded on the CPU, and `notes` why. Only a VideoToolbox failure (the encoder could not open, or its compression session failed) is retried on the CPU; any other failure is reported as it happened, once. Every VideoToolbox encode adds a top-level `notes` line: its quality is not CRF-equivalent (`--quality` / a preset's CRF maps to `-q:v` approximately), and `verified` stays what it is for every tool, the measured properties of the output. A `kind: ffmpeg` failure under `--hw` / `FFMPEG_SKILL_HW` carries `encoder` and `hw` for the command that ran last (a stage's, when `render.py`/`waveform.py` re-raise it); when the command itself failed inside the run (not a tool's own check afterwards, such as `cut.py`'s), its message also names that encoder |
574
576
  | `transcription` | `caption.py --transcribe` (every `--mode`); inside `filler` for `silence.py --filler --transcribe` | `{routing, detected_language?, engine, model, language}`: the engine and model that made the transcript; `language` is the language the engine was told -- the one named (`--language`, `--filler-lang`), else the one auto's detector named -- and `null` when the engine identified it itself; `detected_language` is present when auto's whisper.cpp detector named one; `routing` says why `--engine auto` chose the engine (`"requested"` for a named engine), ending `, no Parakeet engine transcribed it` when auto chose Parakeet and a Whisper engine made the transcript after every Parakeet engine failed |
575
577
  | `notes` | `silence.py --filler --transcribe` (new for `silence.py`; `caption.py` already had it) | present when `--engine auto` ran the English-only Parakeet model on English *assumed* (no language given or detectable, and no Whisper engine installed or none that succeeded): the transcript is wrong if the speech is not English. `caption.py --transcribe` puts the same line in its `notes` |
578
+ | `reencode_reason`, `segment_precision`, `least_exact_precision`, `start_snapped` | `cut.py` (every run) | `reencode_reason`: why anything was re-encoded, distinct values in first-seen order, `[]` for a clean copy: `requested` (`--accurate`), `codec` (`--codec`), `vfr`, `vfr_inconclusive`, `pcm_container`, `copy_failed`, `tolerance`, `concat_fallback` (a `--segments` join that could not stay a stream copy, which now also sets `reencoded: true` and `mode: hybrid`; it reported `copy` before). `segment_precision`: each `--segments` segment's precision (`null` for one segment); `least_exact_precision`: the least exact of them, on every run. `precision` and `keyframe_snapped` keep their meaning (`keyframe_snapped` is `precision == "packet"`, a stream copy end to end); `least_exact_precision` differs from `precision` only for an audio-only join of copied and re-encoded parts. `start_snapped`: measured, `true` when a copied segment's picture starts more than one frame from its requested start (or its start could not be measured: an unknown start is never claimed exact), `false` when every start is where asked or everything re-encoded, `null` under `--dry-run`. `requested_segments`/`requested_duration` keep their meaning, the request clamped to the media's duration: a segment ended with the video (less than a frame of sound trimmed) shows in `duration_delta_seconds` and a `notes` line |
579
+ | `edit_list`, `stored_preroll_seconds`, `av_start_skew_seconds`, `notes` | `cut.py` (every run) | `edit_list`: `true` only for a single-segment `.mp4`/`.m4v`/`.mov` stream copy run with `--edit-list`, which keeps the MP4 edit list the copy writes instead of `-avoid_negative_ts make_zero` (the default, unchanged): the keyframe's pre-roll is stored but hidden, so the picture starts at `--start`. `stored_preroll_seconds`: with `edit_list`, the seconds decoded from the keyframe the copy's seek lands on (measured with the seek ffmpeg makes) up to the first presented frame; `null` otherwise. Under `--edit-list` a copy is judged by its video's length (a `notes` line says so when the container is longer), and one that starts where asked but ends past `--tolerance` re-encodes (`tolerance`) without a `lossless_alternative`. `av_start_skew_seconds`: the output's audio start minus its video start (`null` when either is missing or under `--dry-run`); on a stream copy, when it is more than max(2 frames, 0.1 s) away from the source's own audio/video offset where the copy's picture starts (the keyframe it began from; `--start` under `--edit-list`), a `notes` line names `--accurate`, and `--edit-list` for a single `.mp4`/`.mov` copy whose start snapped. A re-encode carries no such note. The skew is reported, never repaired. `notes`: what the run did that a caller should know, in plain sentences (`[]` when nothing) |
580
+ | `video.start_time`, `audio.start_time` | `probe.py` (and every `probe` block a tool returns) | where each stream starts, in seconds on the file's own clock (ffprobe's stream `start_time`; `null` when unknown). A video stream that starts after its audio, or the reverse, is how a lossless cut's A/V skew shows; `cut.py` reads them for `av_start_skew_seconds` |
581
+ | `cut_reencode_reasons` | `batch.py` (and its `results` rows) | a row whose recipe ran `cut.py` lists each `reencode_reason` its `cut.py` calls named, once per call, beside `cut_reencoded`; the top level counts them across the folder (`{}` when no call re-encoded, `null` when no `cut.py` step ran) on a completed batch, as `cut_stream_copy` does: a failure document carries `results`, whose rows hold each row's reasons; a re-encoded call in a row served from a cache an earlier 2.x batch wrote, which recorded no reasons, counts as `unknown`. `cut_stream_copy` is unchanged |
582
+ | `vfr_check` | `cut.py` (a video input that could still stream-copy) | the sampled frame timing, `{heuristic: true, measured, method: "sampled", guard, windows, deltas}`; `measured` is `sampled_cfr`, `vfr` or `inconclusive`. `--vfr-guard average` (the default) keeps the 2.x guard: a source whose nominal and average frame rates differ (`variable_frame_rate_suspected`) re-encodes as `--accurate` (reason `vfr`, stderr says "variable-frame-rate"), and only then is the timing sampled and reported, with a `notes` line naming `--vfr-guard sampled` when it measured `sampled_cfr`; the key is absent otherwise. `--vfr-guard sampled` samples before every copy and re-encodes on `vfr` (reason `vfr`) or `inconclusive` (fewer than 20 intervals; `vfr_inconclusive`); `--vfr-guard off` samples and keeps the copy, with a `notes` line. Absent for audio-only input or output, `--accurate`, `--codec` and a pending `--dry-run` input |
583
+ | `join_check`, `segment_end_snap_seconds` | `cut.py --segments` | `join_check`: a video stream-copy join measured by demuxing, `{packets, expected_packets, max_step_seconds, audio_offset_ms, audio_parts_checked, ok}`. `expected_packets` counts the source's video packets from each part's start keyframe to its end (or the end of the video); `max_step_seconds` is the largest presentation step, `null` when `vfr_check` measured anything but `sampled_cfr` (a `--vfr-guard off` join of variable timing: count only), and each step at a join must be the step its parts predict within 1 ms. `audio_offset_ms`: per part, its sound's shift minus its picture's in ms (+ = sound late), from audio packets matched by CRC32 to the source stream shaped like the joined audio (FFmpeg's default selection may copy one other than `a:0`; an MPEG-TS source's AAC is hashed without its ADTS headers); `null` for a part with no unique match (digital silence, a periodic tone), and as a whole when the source has no audio or the video check failed. `audio_parts_checked` counts the measured parts. `ok` is the conjunction (count, steps, every measured part within 5 ms); `ok: false` means the join was re-cut (`concat_fallback`) and this is the failed measurement. `null` for a single segment, audio, `--dry-run`, any re-encode forced for every segment, a segment that re-encoded, or a join re-cut before a copy was tried (open GOPs, a later B-frame segment starting between keyframes, parts that differ, a concat error). `segment_end_snap_seconds`: per segment, the signed seconds its end moved to the keyframe the copied part stops at, the first at or after the requested end (`null` for a part that reaches the end of the video, a part of a source with no B-frames, cut to its requested end as in 2.5.1, or a Matroska/MPEG-TS part, which is not snapped); `null` as a whole unless the output is those copied parts |
576
584
 
577
585
  `caption.py --transcribe` and `silence.py --filler --transcribe`: when a local speech engine ran
578
586
  and produced no cue, the refusal is `"<engine> found no speech in <input>"` with `kind: input`,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ffmpeg-skill",
3
- "version": "2.7.0",
3
+ "version": "2.8.0",
4
4
  "description": "Agent Skill that gives coding agents (Claude Code, Cursor, Codex) a local video editor: 42 FFmpeg tools with a machine-readable contract, contract-derived MCP server, FFmpeg capability detection, probe-first / verify-last workflow. Cut, join, silence removal, fit, captions and karaoke, overlays, motion graphics, HDR to SDR, LUTs, audio clean-up and typed dynamics, sync with drift correction, multicam, loudness, delivery checks, project rendering, batch. No API keys, no cloud, no dependencies.",
5
5
  "keywords": [
6
6
  "ffmpeg",
@@ -37,17 +37,39 @@ S-Log, V-Log and C-Log look grey and low-contrast but are tagged SDR. Run
37
37
 
38
38
  ### Keyframe cuts
39
39
  A lossless `cut.py` result may start up to one GOP (often 1–10 s) earlier than
40
- requested; the script re-encodes automatically when the deviation exceeds 0.5 s.
41
- If the user insists on lossless output, pass `--tolerance -1` and tell them the
42
- cut lands on the nearest earlier keyframe. A `-c copy` cut on VFR or a
40
+ requested (`start_snapped: true`); the script re-encodes automatically when the
41
+ deviation exceeds 0.5 s. With `--edit-list`, a single-segment `.mp4`/`.m4v`/`.mov` copy
42
+ keeps the MP4 edit list instead: the pre-roll back to the keyframe is stored but
43
+ hidden, so the picture starts at the requested time (`edit_list`,
44
+ `stored_preroll_seconds`). A player or tool that ignores edit lists shows that
45
+ pre-roll. The **end** still lands on a packet boundary, a few frames long, and
46
+ those extra frames can skip. `.mkv` output has no edit list, and a `--segments`
47
+ join shows each part from its keyframe. On B-frame video an `.mp4`/`.mov`
48
+ `--segments` part's end moves to the first keyframe at or after it, within the
49
+ same tolerance (`segment_end_snap_seconds`); no-B-frame and `.mkv`/`.ts` parts
50
+ are not snapped. Every copy join is checked (frames, steps, each part's sound
51
+ against its picture) and re-cut when wrong. Open-GOP HEVC (iPhone "High Efficiency")
52
+ always re-encodes a `--segments` join, even with `--tolerance -1`, and so does a B-frame `.mp4`/`.mov`
53
+ join whose later segment starts between keyframes (start it on a keyframe, or write `.mkv`). If the user insists on lossless output, pass
54
+ `--tolerance -1` and tell them the cut lands on the nearest earlier keyframe.
55
+ Where a copy begins is measured with the seek ffmpeg makes: on B-frame `.mp4`/`.mov`
56
+ a start a few frames before a keyframe can begin at that keyframe or one GOP
57
+ earlier (`start_snapped: true` either way).
58
+ `av_start_skew_seconds` warns when a copy's sound and picture start apart
59
+ (Core Media HEVC once gave 3.7 s of sound with no picture); it is reported, not
60
+ repaired: `--accurate` is the fix, or `--edit-list` for a lossless `.mp4`/`.m4v`/`.mov`. A `-c copy` cut on VFR or a
43
61
  non-keyframe boundary produces a file that "works" but starts on a frozen or
44
62
  wrong frame — respect the automatic re-encode rather than forcing the copy.
45
63
 
46
64
  ### Variable frame rate
47
65
  `probe.py` sets `variable_frame_rate_suspected` when `r_frame_rate` and
48
66
  `avg_frame_rate` disagree (phone and screen recordings). Every re-encoding
49
- script then adds `-fps_mode cfr` at the source's average rate, and `cut.py`
50
- switches itself to `--accurate` (copy-cuts on VFR are unreliable). Pick the rate
67
+ script then adds `-fps_mode cfr` at the source's average rate. That flag is a
68
+ whole-file average, and a phone clip at 29.98 against a nominal 30 trips it, so
69
+ `cut.py` re-encodes on it by default and reports packet timestamps sampled in up
70
+ to five windows (`vfr_check`), with a note when they are constant;
71
+ `--vfr-guard sampled` switches to `--accurate` only when they are irregular or too
72
+ few to judge, and `--vfr-guard off` keeps the copy anyway. Pick the rate
51
73
  explicitly with `fit.py --fps 30|60` when the average is odd (e.g. 23.4 fps from
52
74
  dropped frames).
53
75
 
@@ -56,24 +56,91 @@ between tools.
56
56
  ```
57
57
  probe.py INPUT... [--compact] [--field duration|video.fps|...]
58
58
  ```
59
- JSON with `duration`, `video{codec,width,height,fps,pix_fmt,color_space,rotation,variable_frame_rate_suspected,hdr,hdr_signal,bt2020_or_hdr,hdr_format}`
59
+ JSON with `duration`, `video{codec,width,height,fps,pix_fmt,color_space,rotation,variable_frame_rate_suspected,hdr,hdr_signal,bt2020_or_hdr,hdr_format,start_time}`
60
60
  (`hdr`, and `hdr_signal` equal to it, is true only for a PQ / HLG transfer or Dolby Vision -- since 2.0;
61
61
  `bt2020_or_hdr` also counts BT.2020 primaries on an SDR transfer, which `hdr_format` names "BT.2020 SDR"),
62
- `audio{codec,channels,sample_rate}`. `--compact` gives one line per file.
62
+ `audio{codec,channels,sample_rate,start_time}` (`start_time`: where the stream starts on the file's
63
+ own clock, `null` when unknown). `--compact` gives one line per file.
63
64
 
64
65
  ### cut.py — cut / join segments
65
66
  ```
66
- cut.py INPUT [--start T] [--end T | --duration T] [--segments A-B,C-D,...] [--accurate] [-o OUT]
67
+ cut.py INPUT [--start T] [--end T | --duration T] [--segments A-B,C-D,...] [--accurate] [--edit-list] [--keep-hevc] [--vfr-guard average|sampled|off] [-o OUT]
67
68
  cut.py INPUT --start T --end T --snap beats [--snap-tolerance 0.12] [--snap-source FILE] [--min-confidence 0.5]
68
69
  ```
69
70
  Times accept `12.5`, `1:30`, `00:01:30.250`. Default is `-c copy` (snaps to
70
71
  keyframes, instant, lossless); if the snapped result deviates more than
71
72
  `--tolerance` (0.5 s) from the request, that segment is re-encoded automatically
72
- (x264 CRF 18). `--accurate` always re-encodes; `--tolerance -1` never does.
73
+ (CRF 18: x264, or x265 Main10 with the source's own tags for an HDR or BT.2020 source;
74
+ `--keep-hevc` re-encodes an SDR HEVC source (not BT.2020: that keeps its Main10 line and colour tags) as x265 8-bit BT.709, on VideoToolbox under `--hw`;
75
+ `--codec` overrides all of them). `--accurate` always re-encodes; `--tolerance -1` never does for a keyframe snap
76
+ (a `--segments` join of open-GOP video, one whose later B-frame `.mp4`/`.mov` segment starts
77
+ between keyframes, or one that fails its check, still re-cuts).
73
78
  Multiple segments are concatenated in the order given. stderr reports whether
74
79
  the result was "lossless stream copy" or "re-encoded"; when the snap forced a
75
80
  re-encode, the result's `lossless_alternative` names the nearest keyframe
76
81
  `--start` that would stream-copy instead, so the trade can be offered.
82
+ A single `.mp4`/`.mov` stream copy shifts its timestamps to zero
83
+ (`-avoid_negative_ts make_zero`), so the picture starts at the keyframe.
84
+ `--edit-list` keeps its MP4 edit list instead, so the picture starts at
85
+ `--start`: `edit_list` and `stored_preroll_seconds` say so, `notes` warns that a
86
+ player which ignores edit lists shows the pre-roll, and the copy's length is
87
+ judged by its video (a copy that starts where asked but ends past `--tolerance`
88
+ re-encodes without offering another `--start`). `av_start_skew_seconds` is
89
+ audio start minus video start; a stream copy gets a note when it is more than
90
+ max(2 frames, 0.1 s) away from the source's own offset where the picture starts
91
+ (the keyframe the copy began from; `--start` under `--edit-list`). It is
92
+ reported, not repaired. `-ss`/`-t` are passed to the microsecond; `--accurate`
93
+ seeks a second early and trims on the output side, so frames just before a
94
+ keyframe are kept. `keyframe_snapped` is true for every stream copy (`precision`
95
+ `packet`); `start_snapped` is measured: true when a copied picture starts more
96
+ than a frame from `--start`, or when its start could not be measured (an unknown start is
97
+ never claimed exact); `null` under `--dry-run`.
98
+ `reencode_reason` lists every cause of a re-encode (`requested`, `codec`, `vfr`,
99
+ `vfr_inconclusive`, `pcm_container`, `copy_failed`, `tolerance`, `concat_fallback`);
100
+ `--segments` adds `segment_precision`, and `least_exact_precision` is the least exact one.
101
+ A `--segments` video copy join adds `join_check` (`packets`, `expected_packets`,
102
+ `max_step_seconds`, `audio_offset_ms` per part, `audio_parts_checked`, `ok`: the
103
+ written file measured against the source) and `segment_end_snap_seconds` (where
104
+ each part's end moved to its keyframe).
105
+
106
+ **VFR guard (`--vfr-guard`).** `average` (default): a source whose nominal and
107
+ average frame rates differ (`variable_frame_rate_suspected`) re-encodes as
108
+ `--accurate` (reason `vfr`), and `cut.py` then samples its packet timestamps (up
109
+ to five 6 s windows; no decoding) into `vfr_check` (`measured`: `sampled_cfr`,
110
+ `vfr` or `inconclusive`, plus `guard`); a `sampled_cfr` there adds a note naming
111
+ `--vfr-guard sampled`. `sampled` samples before every copy and re-encodes only on
112
+ `vfr` / `inconclusive` (reasons `vfr` / `vfr_inconclusive`), so a phone clip at
113
+ 29.98 against a nominal 30 copies; `off` samples and keeps the copy with a note.
114
+ Irregular timing between the windows is not seen; `--accurate` is always available.
115
+
116
+ A source with no B-frames (iPhone "Most Compatible" H.264) cuts its parts as
117
+ every 2.x release did: from the keyframe at (or before) each start to the end
118
+ asked for, each part within `--tolerance`. On B-frame video a copied
119
+ `.mp4`/`.mov` part runs from the keyframe at (or before) its start to the first
120
+ keyframe at or after its end (never an earlier one), each end within
121
+ `--tolerance`, so the join is the source's own frames with no gaps; a later
122
+ segment that starts between keyframes there is re-cut (its hidden pre-roll
123
+ cannot cross a join). A source with **open GOPs** (x265's default, iPhone "High
124
+ Efficiency" HEVC) cannot be copied exactly across a join and is re-cut
125
+ (`concat_fallback`, named in `notes`). Every copy join is measured after it is
126
+ written (`join_check`): the frame count, each step at a join against the step
127
+ its parts predict, and each part's sound against its picture (audio packets
128
+ matched to the source; more than 5 ms off fails, a part with no unique match
129
+ is unmeasured). One that fails is re-cut. Only windows around the segments
130
+ (about 10 s each side, widened as needed) are read from the source, so a long
131
+ source costs what its segments do.
132
+ The parts are joined by stream copy only when they all copied and match: the
133
+ same streams with the same codec parameters, rotation, colour tags and
134
+ extradata. Otherwise every segment is **re-cut from
135
+ the source** into one re-encode (`concat_fallback`): frame-exact boundaries, the
136
+ audio's offset from the video kept, subtitles dropped and reported, and at most
137
+ 32 segments per ffmpeg call. A segment shorter than one video frame is refused.
138
+ With `--accurate` the segments are always cut that way, in one encode (a part
139
+ encoded on its own ran an AAC frame past its picture, leaving a hole at every
140
+ join). A segment other than the last that runs past the end of the video is
141
+ ended with the video when it runs less than a frame past it, and otherwise keeps
142
+ its sound with the last frame held -- which forces the re-cut (`concat_fallback`,
143
+ named in `notes`). The last segment keeps the source's own sound-only tail.
77
144
 
78
145
  **`--snap beats` (1.17)** moves each in/out point to the nearest *measured*
79
146
  beat within `--snap-tolerance` seconds (default 0.12, about a quarter of a beat
@@ -905,6 +972,13 @@ batch.py FOLDER --recipe batch.json [--force] [--watch SECONDS] [--jobs N|auto]
905
972
  placeholders, chained) or `project` (a render project applied per file).
906
973
  Outputs land in `output_dir` with `suffix`; a content-hash cache skips files
907
974
  already done with the same recipe. Use `--dry-run` to preview the plan.
975
+ A recipe that runs `cut.py` rolls its calls up: each of those `results` rows carries
976
+ `cut_reencoded` (each call's `reencoded`) and `cut_reencode_reasons` (each
977
+ `reencode_reason` its calls named, once per call),
978
+ and the top level `cut_stream_copy` (`{calls, stream_copy, reencoded,
979
+ stream_copy_rate}`) and `cut_reencode_reasons` (counts per reason; `unknown` for a
980
+ re-encoded call cached by an earlier 2.x batch); both are `null`
981
+ when no `cut.py` step ran.
908
982
 
909
983
  **`--jobs N` (1.17)** processes N files at once (threads: the work is
910
984
  subprocess waiting). Capped at `min(N, cpu_count, 8)` — every item is itself an
@@ -59,7 +59,7 @@ from _common.runner import (
59
59
  ffmpeg_version, flush_drawtext_textfiles, INSTALL_HINTS, install_signal_handlers, _is_ffmpeg, _limit_for,
60
60
  _odd_dimension_retry, _on_signal, _OutputLock, _pid_dead, place_output, PROBE_TIMEOUT, _progress_line,
61
61
  read_text_or_die, refuse_output_is_input, _remember_output, require_tool, run, run_analysis, _run_captured,
62
- run_keeping_subtitles, run_tool, _run_with_progress, shell_quote, _SIGNALS_INSTALLED, _stage_existing_output,
62
+ run_keeping_subtitles, run_tool, _run_with_progress, shell_quote, sibling_temp, _SIGNALS_INSTALLED, _stage_existing_output,
63
63
  STATE, _timed_out, _unwatch, _watch, X264_PRESETS
64
64
  )
65
65
  from _common.emit import (
@@ -68,7 +68,7 @@ from _common.emit import (
68
68
  )
69
69
  from _common.probe import (
70
70
  analyze_levels, _aspect_string, _bit_depth, decode_pcm_mono, detect_scenes, detect_silences, fingerprint,
71
- _fraction, keyframes_near, SCORE_RE, SIL_RE, decode_gray_frames,
71
+ _fraction, keyframes_near, classify_frame_timing, measure_frame_timing, SCORE_RE, SIL_RE, decode_gray_frames,
72
72
  measured_level_dbfs, AUDIO_MEDIA_EXT, MEDIA_EXT, _output_failed, probe, rms_envelope, _to_float, _to_int, verify_output
73
73
  )
74
74
  from _common.decision import (
@@ -83,7 +83,7 @@ from _common.decision import (
83
83
  _onset_strength, _pick_onsets, _autocorrelation_peak, _grid_score,
84
84
  brand_states_font, cfr_args, concat_list_line, db_to_linear, default_output, encoder_args, escape_filter_path,
85
85
  fmt_secs, fmt_smpte_time, fmt_srt_time, frame_size, is_audio_output, load_brand, MissingFpsError, pad_filters, parse_time,
86
- signed_time_arg, SVT_PRESET, time_arg, video_args, x264_args, _x264_raw
86
+ signed_time_arg, source_codec_video_args, SVT_PRESET, time_arg, video_args, x264_args, _x264_raw
87
87
  )
88
88
  from _common.asr import (
89
89
  ASR_ENGINES, ASR_INSTALL_HINT, _asr_run, die_no_engine, parse_srt, transcribe, _transcribe_in,
@@ -200,7 +200,7 @@ __all__ = [
200
200
  "font_family_for_script", "font_family_of_file", "FONT_FLAG_HINT", "font_for_script", "FONT_INSTALL_HINT",
201
201
  "fonts_dir_covers_script", "_fraction", "has_emoji", "info", "INSTALL_HINTS", "install_signal_handlers",
202
202
  "is_audio_output", "_is_emoji_base", "_is_emoji_char", "_is_ffmpeg", "_is_mark", "_KEYCAP", "_KEYCAP_BASES",
203
- "keyframes_near", "LANGUAGE_NAMES", "LATIN_EM", "LEADING_VOWELS", "_libass_color_probe", "_limit_for",
203
+ "keyframes_near", "classify_frame_timing", "measure_frame_timing", "LANGUAGE_NAMES", "LATIN_EM", "LEADING_VOWELS", "_libass_color_probe", "_limit_for",
204
204
  "load_brand", "measured_level_dbfs", "AUDIO_MEDIA_EXT", "MEDIA_EXT", "MissingFpsError", "needs_shaping", "NO_SPACE_SCRIPTS",
205
205
  "_odd_dimension_retry", "_on_signal", "_output_failed", "_OutputLock", "pad_filters", "parse_time", "_pid_dead",
206
206
  "place_output", "_plan_at_exit", "_plan_inputs", "_PLAN_STRIP", "PLAN_VERSION", "PREFERRED_FAMILIES",
@@ -209,10 +209,10 @@ __all__ = [
209
209
  "decode_gray_frames", "frame_flow", "label_shot_flow", "MOTION_GRID", "MOTION_SEARCH", "MOTION_STATIC_PX", "MOTION_PAN_SPREAD",
210
210
  "_run_captured", "run_keeping_subtitles", "run_tool", "_run_with_progress", "_SCRIPT_FONT_CACHE",
211
211
  "_script_font_entry", "script_font_for_text", "script_font_status", "_script_font_uncached", "_SCRIPT_RANGES",
212
- "SCRIPTS", "_sdr_bt709", "_set_current_ctx", "_SHAPING_BUILD_CACHE", "SHAPING_SCRIPTS", "shell_quote",
212
+ "SCRIPTS", "_sdr_bt709", "_set_current_ctx", "_SHAPING_BUILD_CACHE", "SHAPING_SCRIPTS", "shell_quote", "sibling_temp",
213
213
  "_SIGNALS_INSTALLED", "signed_time_arg", "_stage_existing_output", "STATE", "SVT_PRESET", "text_width_em",
214
214
  "time_arg", "_timed_out", "_to_float", "_to_int", "_unwatch", "validate_color", "verify_output",
215
- "video_args", "_VS15", "_VS16", "_watch", "WINDOWS_FONTS", "write_plan", "x264_args", "X264_PRESETS",
215
+ "source_codec_video_args", "video_args", "_VS15", "_VS16", "_watch", "WINDOWS_FONTS", "write_plan", "x264_args", "X264_PRESETS",
216
216
  "_x264_raw", "_ZWJ",
217
217
  "ASR_ENGINES", "ASR_INSTALL_HINT", "_asr_run", "die_no_engine", "parse_srt", "transcribe",
218
218
  "_transcribe_in", "transcribe_words", "_words_from_openai_whisper_json",
@@ -478,6 +478,19 @@ def video_args(meta: Optional[Dict[str, Any]], crf: int = 18, preset: str = "med
478
478
  return _maybe_hw("hevc", crf, meta, True, _x265_hdr_default(v, crf, preset))
479
479
 
480
480
 
481
+ def source_codec_video_args(meta: Optional[Dict[str, Any]], crf: int = 18, preset: str = "medium",
482
+ keep_hevc: bool = False) -> List[str]:
483
+ """video_args(), except that with `keep_hevc` (cut.py --keep-hevc) an SDR HEVC source re-encodes
484
+ to HEVC (8-bit, BT.709-tagged, on VideoToolbox when --hw asks) instead of H.264: a cut is a trim,
485
+ so a re-encoded segment can come out in the codec the source (and any copied segment beside it)
486
+ is in. Opt-in within 2.x, where an SDR re-encode stays x264 unless asked. An HDR or BT.2020
487
+ source always takes video_args()'s Main10 line, and --codec always wins."""
488
+ v = (meta or {}).get("video") or {}
489
+ if keep_hevc and not STATE.codec and v.get("codec") == "hevc" and not v.get("bt2020_or_hdr"):
490
+ return encoder_args("hevc", crf, preset, meta)
491
+ return video_args(meta, crf, preset)
492
+
493
+
481
494
  def _x265_hdr_default(v: Dict[str, Any], crf: int, preset: str) -> List[str]:
482
495
  cs = v.get("color_space") or "bt2020nc"
483
496
  prim = v.get("color_primaries") or "bt2020"
@@ -9,6 +9,7 @@ import json
9
9
  import math
10
10
  import os
11
11
  import re
12
+ import statistics
12
13
  from fractions import Fraction
13
14
  from typing import Any, Dict, List, Optional, Sequence, Tuple
14
15
  from _common.emit import die
@@ -285,6 +286,9 @@ def probe(path: str, role: str = "input") -> Dict[str, Any]:
285
286
  "nb_frames": _to_int(video.get("nb_frames")),
286
287
  # the stream's own length (a container's also counts audio priming and a longer track)
287
288
  "duration": _to_float(video.get("duration")),
289
+ # where the stream starts; a video stream that starts after its audio (or the reverse)
290
+ # is how a lossless cut's A/V skew shows up
291
+ "start_time": _to_float(video.get("start_time")),
288
292
  "bitrate": _to_int(video.get("bit_rate")),
289
293
  }
290
294
  if audio:
@@ -295,6 +299,7 @@ def probe(path: str, role: str = "input") -> Dict[str, Any]:
295
299
  "sample_rate": _to_int(audio.get("sample_rate")),
296
300
  "bitrate": _to_int(audio.get("bit_rate")),
297
301
  "duration": _to_float(audio.get("duration")),
302
+ "start_time": _to_float(audio.get("start_time")),
298
303
  }
299
304
  # every audio stream in file order: index n here is `-map 0:a:n` (audio.py --audio-stream n)
300
305
  out["audio_streams"] = [{
@@ -343,6 +348,93 @@ def keyframes_near(path: str, t: float, window: float = 5.0) -> List[float]:
343
348
  return sorted(set(out))
344
349
 
345
350
 
351
+ # frame timing: is a stream's frame rate really variable? (cut.py's VFR guard)
352
+ TIMING_WINDOWS = 5 # sampled windows per file, fewer on a short one
353
+ TIMING_WINDOW_SECONDS = 6
354
+ TIMING_MIN_DELTAS = 20 # fewer measured intervals than this is no evidence either way
355
+
356
+
357
+ def complete_pts(packets: Sequence[Dict[str, Any]]) -> List[Any]:
358
+ """The pts of `packets` (as read, in decode order) whose frames are all there: those at or
359
+ before the last dts read. A read that stops just after a frame stored ahead of the B-frames
360
+ shown before it has missed those B-frames, and their gap would read as a dropped frame; every
361
+ frame shown by the last dts was decoded by then, so it was read. (FFmpeg 9.0's interval reads
362
+ measured whole on MP4; this keeps a demuxer that stops elsewhere from faking VFR.)"""
363
+ dts = [p["dts"] for p in packets if isinstance(p.get("dts"), int)]
364
+ pts = [p.get("pts") for p in packets]
365
+ return pts if not dts else [t for t in pts if isinstance(t, int) and t <= max(dts)]
366
+
367
+
368
+ def classify_frame_timing(windows: Sequence[Sequence[Any]], time_base: Any) -> Dict[str, Any]:
369
+ """Whether the frame timing in sampled windows is constant. `windows` are lists of pts in
370
+ `time_base` ticks, the first starting at the file's first packet.
371
+
372
+ Each window's pts are sorted (decode order stores B-frames out of sequence) and differenced,
373
+ with repeats and missing values dropped. The file-start window also drops its first interval:
374
+ an edit list or encoder priming can stretch it without the frame rate being variable.
375
+ `sampled_cfr` needs every interval within one tick (at least 1 ms) of its window's median and
376
+ the window medians to agree, so a single dropped or irregular frame is `vfr`. The tolerance
377
+ never exceeds half a frame: a tick that coarse (AVI's 1/fps) cannot round an interval, and one
378
+ tick of slack there would pass a dropped frame. Fewer than
379
+ TIMING_MIN_DELTAS intervals is `inconclusive`. What happens between the windows is not seen."""
380
+ result: Dict[str, Any] = {"measured": "inconclusive", "windows": len(windows), "deltas": 0}
381
+ tick = _fraction(str(time_base)) if time_base else None
382
+ if not tick or tick <= 0:
383
+ return result
384
+ per_window: List[List[float]] = []
385
+ for i, pts in enumerate(windows):
386
+ vals = sorted(float(p) for p in pts if isinstance(p, (int, float)) and math.isfinite(p))
387
+ deltas = [(b - a) * float(tick) for a, b in zip(vals, vals[1:]) if b != a]
388
+ per_window.append(deltas[1:] if i == 0 else deltas)
389
+ per_window = [d for d in per_window if d]
390
+ result["deltas"] = sum(len(d) for d in per_window)
391
+ if result["deltas"] < TIMING_MIN_DELTAS:
392
+ return result
393
+ medians = [statistics.median(d) for d in per_window]
394
+ tol = min(max(0.001, float(tick) + 1e-6), min(medians) / 2)
395
+ steady = all(abs(x - m) <= tol for d, m in zip(per_window, medians) for x in d)
396
+ result["measured"] = "sampled_cfr" if steady and max(medians) - min(medians) <= tol else "vfr"
397
+ return result
398
+
399
+
400
+ def timing_window_starts(duration: float, start: float = 0.0) -> List[float]:
401
+ """Up to TIMING_WINDOWS non-overlapping window starts spread over the file, the first on its
402
+ first packet. -read_intervals takes timestamps, so a file that starts at 10 s is read from 10."""
403
+ n = max(1, min(TIMING_WINDOWS, int((duration or 0.0) // TIMING_WINDOW_SECONDS)))
404
+ span = (duration or 0.0) - TIMING_WINDOW_SECONDS
405
+ return [round(start + (span * i / (n - 1) if n > 1 else 0.0), 3) for i in range(n)]
406
+
407
+
408
+ def measure_frame_timing(path: str, duration: Optional[float], start: float = 0.0) -> Dict[str, Any]:
409
+ """classify_frame_timing over sampled windows of `path`'s first video stream that is not cover
410
+ art (probe()'s choice): one packet-only ffprobe per window, no decoding. An interval read seeks
411
+ back to the keyframe before its start and, given a relative end, ends that far past the
412
+ keyframe -- on a long GOP every window read the file's first seconds -- so each window names
413
+ its absolute end and keeps only the frames from its start. The first window does not seek at
414
+ all: a seek to the start of an edit-listed MP4 skips its keyframe, stored before the start
415
+ with a negative pts, and loses the frames that depend on it (seeks near the start behave
416
+ erratically, so no margin is safe). The cost: a file whose video begins long after its first
417
+ packet is demuxed, not decoded, up to the video start. An ffprobe failure is `inconclusive`."""
418
+ ffprobe = require_tool("ffprobe")
419
+ windows: List[List[Any]] = []
420
+ time_base = None
421
+ for i, t in enumerate(timing_window_starts(duration or 0.0, start)):
422
+ proc = run([ffprobe, "-v", "error", "-select_streams", "V:0", "-show_packets",
423
+ "-show_entries", "packet=pts,dts:stream=time_base", "-of", "json",
424
+ "-read_intervals", f"{t if i else ''}%{t + TIMING_WINDOW_SECONDS:.3f}", path], quiet=True, check=False)
425
+ try:
426
+ doc = json.loads(proc.stdout) if proc.returncode == 0 else None
427
+ except ValueError:
428
+ doc = None
429
+ if not isinstance(doc, dict):
430
+ return {"measured": "inconclusive", "windows": len(windows), "deltas": 0}
431
+ time_base = time_base or ((doc.get("streams") or [{}])[0] or {}).get("time_base")
432
+ tick = float(_fraction(str(time_base)) or 0) if time_base else 0.0
433
+ windows.append([p for p in complete_pts(doc.get("packets") or [])
434
+ if isinstance(p, int) and p * tick >= t - 1e-6])
435
+ return classify_frame_timing(windows, time_base)
436
+
437
+
346
438
  def measured_level_dbfs(path: str, seconds: Optional[float] = 120.0,
347
439
  audio_stream: Optional[int] = None) -> Optional[Dict[str, float]]:
348
440
  """Mean and peak level of the first `seconds` of audio (volumedetect), in dBFS; None if unmeasurable.
@@ -1158,11 +1158,17 @@ def ffmpeg_base(overwrite: bool = True) -> List[str]:
1158
1158
  return cmd
1159
1159
 
1160
1160
 
1161
- def place_output(src: str, dst: str) -> None:
1161
+ def place_output(src: str, dst: str, move: bool = False) -> None:
1162
1162
  """Deliver an already-rendered file to `dst` under the same rules as an ffmpeg output:
1163
- the path is checked, an existing file is only replaced through a sibling temp so a
1164
- failed copy never costs the caller what was there, and the result is remembered as ours.
1165
- render.py's final `copyfile()` used to bypass all three."""
1163
+ the path is checked, the output's lock is held (as run() holds it for an ffmpeg write), an
1164
+ existing file is only replaced through a sibling temp so a failed copy never costs the
1165
+ caller what was there, and the result is remembered as ours. render.py's final
1166
+ `copyfile()` used to bypass all of them.
1167
+
1168
+ `move`: `src` is a temp file the caller wrote next to `dst` (sibling_temp), renamed into place
1169
+ with os.replace() under the lock instead of copied -- a checked `cut.py --segments` join was
1170
+ copied whole, about 2 s for 240 MB. A rename that fails (another filesystem) falls back to
1171
+ the copy."""
1166
1172
  import shutil
1167
1173
  cmd = ["ffmpeg", dst]
1168
1174
  _check_output_path(cmd)
@@ -1170,18 +1176,35 @@ def place_output(src: str, dst: str) -> None:
1170
1176
  d, base = os.path.split(dst)
1171
1177
  stem, ext = os.path.splitext(base)
1172
1178
  tmp = os.path.join(d, f".{stem}.ffskill-{os.getpid()}{ext}")
1173
- try:
1174
- shutil.copyfile(src, tmp)
1175
- os.replace(tmp, dst)
1176
- except OSError as e:
1179
+ with _OutputLock(dst):
1180
+ if move:
1181
+ try:
1182
+ os.replace(src, dst)
1183
+ _remember_output(cmd)
1184
+ return
1185
+ except OSError:
1186
+ pass
1177
1187
  try:
1178
- os.remove(tmp)
1179
- except OSError:
1180
- pass
1181
- die(f"could not place {dst}: {e}", kind="output")
1188
+ shutil.copyfile(src, tmp)
1189
+ os.replace(tmp, dst)
1190
+ except OSError as e:
1191
+ try:
1192
+ os.remove(tmp)
1193
+ except OSError:
1194
+ pass
1195
+ die(f"could not place {dst}: {e}", kind="output")
1182
1196
  _remember_output(cmd)
1183
1197
 
1184
1198
 
1199
+ def sibling_temp(dst: str, tag: str) -> str:
1200
+ """A hidden path next to `dst`, with its extension (the muxer is chosen by it), for a file
1201
+ place_output(..., move=True) renames into place: same directory, so the rename never
1202
+ crosses a filesystem."""
1203
+ d, base = os.path.split(dst)
1204
+ stem, ext = os.path.splitext(base)
1205
+ return os.path.join(d, f".{stem}.ffskill-{tag}-{os.getpid()}{ext}")
1206
+
1207
+
1185
1208
  _DRAWTEXT_TMPDIR: "Optional[str]" = None
1186
1209
 
1187
1210