Local video editor for coding agents: 42 FFmpeg tools with a machine-readable contract and an MCP server, for Claude Code, Cursor and Codex. Cut, captions, reframe, loudness, platform exports. No cloud, no API keys.
Python agent-skillsai-agentsaudio-processingcaptionsclaude-codecodexcoding-agentcursorffmpegloudness-normalizationmcpmcp-servermodel-context-protocolpythonsubtitlestranscodingvideovideo-editingvideo-processingwhisper
fix(color): draw pad, text, box and background colours in BT.709; label untagged clips in mixed joins (#334)
Shogo Kajiyamaddbaf59d5500
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex 73569b9..631ef09 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -4,7 +4,10 @@## Unreleased-(nothing yet)+### Fixed++- **A colour the skill draws lands in the BT.709 its output is tagged with.** A `--pad-color` (`fit.py`, `export.py`, `join.py`, `broll.py`), `pad.py --color`, `straighten.py --fill-color`, `grid.py --background`/`--font-color`, `overlay.py --text` font/border/box colours, `caption.py`'s burned colours, `graphics.py`'s brand colours (boxes, bars, text), `background.py --color`/`--gradient` and `waveform.py`'s background and waves were turned into YUV by FFmpeg's drawing code with BT.601 under the output's BT.709 tag: pure red came out (255,24,0), pure green (0,216,0) (mean ΔRGB 6.6-33.5, max 34.9-64.0 depending on the tool, on FFmpeg 5.1.1, 6.1, 7.1.1 and 8.1.3; on 7.1+ a BT.709-tagged or converted source's pads were already right). From FFmpeg 7.1 the drawing filters follow the picture's label, so an untagged picture is labelled BT.709 (tags only); before 7.1 nothing makes them use another matrix, so the drawing runs on an RGB copy and only the pixels it touched are taken from it (the rest of the picture is bit-identical). Boxes and backgrounds are colour sources made in RGB and converted; `graphics.py`'s translucent boxes with chroma are overlaid colour layers now, which also blend their colour at the alpha they state (`drawbox` blended 4:2:0 chroma four times over). The ASS files `caption.py` and `graphics.py` write say `YCbCr Matrix: None`, which FFmpeg 8.x needs to draw them in the picture's own matrix (and libass-based players read the same way). After the fix every measured colour is within 1.8 of the truth (mean 0.3-0.95), and within 3.6 for the thin graphics bars. A grey -- black and white pads and text, the default [email protected] boxes, graphics' near-black background -- is the same under both matrices and plans the command it did; a GIF export, a prores or HDR line and a caller's own `--ass` file are unchanged.+- **`join.py`, `multicam.py`, `broll.py` and `overlay.py --video` label an untagged clip BT.709 beside a converted one**, as `grid.py` does since #333: on FFmpeg 7.1+ concat/overlay converted one of the two pictures to the other's "unknown" (BT.601) matrix (7.08 mean / 25.0 max ΔRGB with the BT.601 clip first, 9.12 / 40.1 with it second, on 7.1.1 and 8.1.3). The colour tests' "untagged" clips are now untagged on FFmpeg 7.1+ too (they came out tagged bt709 there, so CI on 7.1/8.x/9.x never ran the untagged case).## 2.10.2diff --git a/docs/contract.md b/docs/contract.mdindex b3dc96b..b06b3f2 100644--- a/docs/contract.md+++ b/docs/contract.md@@ -225,7 +225,12 @@ source adds no filter, and the tags themselves never convert (`bt709_tag_args()``waveform --image`) is converted to BT.709 YUV explicitly (`scale=out_color_matrix=bt709`; a JPEG isa `bt470bg` source); `grid.py` converts each cell by its own tags. Where a converted picture meets anuntagged one in a mixing filter, the untagged one is labelled BT.709 (`setparams`, tags only), so-FFmpeg 7.1+'s colourspace negotiation converts neither.+FFmpeg 7.1+'s colourspace negotiation converts neither (`join`, `multicam`, `broll` and `overlay --video`+too, beside a converted clip). A colour a tool draws from a hex or named value (a pad, `tpad` or+`rotate` fill, `drawtext` and libass text and boxes, a `graphics.py` box or bar, `background.py`'s+`color`/`gradients`, `waveform.py`'s plate and waves) lands in the same BT.709: from FFmpeg 7.1+an untagged picture is labelled for the drawing filters, before 7.1 the drawing runs on an RGB copy+and only the pixels it touched are taken from it (`_common.drawn_chain()`); a grey changes nothing.`cut.py --keep-hevc` (opt-in) re-encodes an SDR HEVC source as x265 8-bit BT.709 instead of x264; an HDR orBT.2020 source keeps the Main10 line with its own colour tags, with or without the flag. Theencoder each value needs is listed under the tool's optional capabilities (`--codec hevc` anddiff --git a/docs/design-decisions.md b/docs/design-decisions.mdindex b7fd877..3a0a86b 100644--- a/docs/design-decisions.md+++ b/docs/design-decisions.md@@ -176,17 +176,61 @@ exists. When a decision changes, edit the entry in the same PR.a converted picture meets an untagged one, the untagged one is labelled BT.709 (`setparams`, tagsonly; `label_chain()`), which is what the output's tag says it is: no conversion is left tonegotiate, and on 5.1/6.1 the label changes nothing. It is applied only there (a still over the- video, a grid with a converted cell), so a one-input re-encode of an untagged source, and a grid of- untagged/BT.709 clips, keep their commands; `join`/`multicam`/`broll`/`overlay --video` do not- label yet, and on 7.1+ mixing an untagged and a BT.601 clip there still shifts one of them (a- separate change). Known on FFmpeg 7.0.x only (not in CI): its `colorspace` filter does not declare- the converted link, so a BT.601 clip *first* in `grid`/`join` still takes the others with it.- Code: `_common/color.py`, `_common/decision.py`. Tests:+ video, a grid with a converted cell, and since 2.10.3 a `join`/`multicam`/`broll`/`overlay --video`+ with a converted clip, where an untagged clip beside a BT.601 one was shifted on 7.1+: 7.08 mean /+ 25.0 max with the BT.601 clip first, 9.12 / 40.1 with it second, on 7.1.1 and 8.1.3), so a+ one-input re-encode of an untagged source, and a grid or join of untagged/BT.709 clips, keep their+ commands. Known on FFmpeg 7.0.x only (not in CI): its `colorspace` filter does not declare the+ converted link, so a BT.601 clip *first* in `grid`/`join` still takes the others with it; the fix+ would change #332's conversion filter for every tool, so it is left. Code: `_common/color.py`,+ `_common/decision.py`. Tests:`test_an_rgb_or_jpeg_source_is_converted_to_the_bt709_it_is_tagged_with`,`test_a_still_composited_over_the_video_is_converted_to_bt709`,`test_grid_converts_each_cell_by_its_own_tags`,+ `test_mixing_tools_label_an_untagged_clip_beside_a_converted_one`,`test_an_rgb_source_or_a_still_is_converted_to_the_bt709_it_is_tagged_with`,- `test_still_and_rgb_tools_plan_the_bt709_conversion`.+ `test_still_and_rgb_tools_plan_the_bt709_conversion`. These tests' "untagged" clips were tagged+ bt709 when the fixtures were made by FFmpeg 7.1+ (the scale that drew them labels its output, and+ x264 wrote that label): they are relabelled unknown before the encode since 2.10.3, so 7.1+ CI runs+ the untagged case too.+- **A colour the skill draws is drawn in the BT.709 its output is tagged with: on FFmpeg 7.1+ by+ labelling an untagged picture, before 7.1 by drawing on an RGB copy and keeping every pixel the+ drawing did not touch.** A pad, `tpad`, `rotate` fill, `drawtext` font/box/border colour or libass+ colour is turned into YUV by the drawing filter itself (`ff_draw`): with BT.601 on 5.1-7.0 whatever+ the picture is tagged, with the link's colourspace from 7.1 on, "unknown" read as BT.601; `drawbox`+ uses BT.601 on every version, the `color`/`gradients` sources and an RGBA layer went through the+ encoder line's BT.601 default. Through 2.10.2 every drawn colour was BT.601 under the BT.709 tag+ (pure red (255,24,0), pure green (0,216,0): 8.80 mean / 38.9 max ΔRGB on 5.1.1, 6.1, 7.1.1 and+ 8.1.3; on 7.1+ only a BT.709-tagged or converted source's pads were right). `drawn_chain()` picks+ the cheapest way that is exact on each build. 7.1+: the untagged picture is labelled BT.709+ (`label_chain()`, tags only) and the filters follow the label; generated ASS says `YCbCr Matrix:+ None`, because 8.x's `ass`/`subtitles` read an absent header as BT.601 whatever the link says+ (FFmpeg's own SRT header already says None). Before 7.1 nothing makes `ff_draw` use another matrix,+ so the drawing runs on an RGB copy (`rgb_island()`: in RGB a colour is written as it is, converted+ back with BT.709), and maskedmerge takes the drawn copy only where it differs from the same round+ trip without the colour: the RGB round trip is not lossless, so the untouched pixels come from the+ YUV picture itself (bit-identical, `--quality 0`). The alternatives: a numeric pre-compensation of+ the colour (the RGB that BT.601 maths turns into the BT.709 YUV) is out of gamut for saturated+ colours -- pure red needs G = -27, and clamped it is as wrong as before (22.6 max), 15% of the RGB+ cube cannot be reached; drawing everything in an RGB copy without the mask changes the whole+ picture; the island on every build costs four scales per frame where 7.1+ needs none, and on 8.x+ an RGB link with a non-None ASS header is drawn in limited range. Boxes, bars and backgrounds are+ colour sources made in RGB and converted (`rgb_chain_to_bt709()`, as #333's logos): a `graphics.py`+ template with a box of chroma draws all its boxes as overlaid colour layers, in their order and+ under its text, which also ends drawbox's 4:2:0 quirk of blending chroma four times over (a 0.55+ box was ~0.96 opaque in colour; the layer blends at the stated alpha; a box starting on an odd row+ or column, which overlay cannot place in 4:2:0, gets a transparent first row/column, so it covers+ the drawbox's pixels); `background.py`'s `color`/`gradients` and `waveform.py`'s plate and RGBA waves are+ converted where they are made. Only a colour whose 8-bit YUV differs between the two matrices+ (`is_neutral_color()`: not a grey, not graphics.py's near-black 101418) changes a command, so the+ default black pads, white text and black/grey boxes plan what they did. Not converted: a GIF export+ (untagged, and palettegen reads the pad back with the BT.601 it was drawn in), a prores or HDR line,+ a caller's own `--ass` file (its colours and YCbCr header are the file's, as libass/FFmpeg read+ them), and the PNG contact sheets of `look.py`/`scenes.py` (RGB outputs, not tagged video). Code:+ `_common/color.py` (`is_neutral_color`, `rgb_island`), `_common/decision.py` (`drawn_chain`,+ `pad_filters`), and each drawing tool. Tests: `test_a_pad_fill_or_background_lands_in_the_bt709_its_output_is_tagged_with`,+ `test_drawn_text_and_boxes_land_in_the_bt709_their_output_is_tagged_with`,+ `test_a_picture_that_only_passes_through_keeps_its_pixels`, `test_drawn_colours_are_drawn_in_bt709`.- **`--correct` converts through bt601 on both legs** (not bt709), because on every testedFFmpeg the 709 round trip through 8-bit 4:2:0 loses ~5 dB more than 601. Code:`color.py _rgb_stage()`. Test: `test_color_correct_identity_holds_on_a_bt709_tagged_source`.diff --git a/references/ci-platform-pitfalls.md b/references/ci-platform-pitfalls.mdindex 74e176d..161ad2a 100644--- a/references/ci-platform-pitfalls.md+++ b/references/ci-platform-pitfalls.md@@ -162,6 +162,11 @@ these had ever shown up before.before measuring (40 dB for re-matrixed pixels), and it then flagged the fixed, byte-cleanoutput as 26 dB instead. The tests' `_psnr` helper now pins identical colour tags on bothinputs so every build reports the same number for the same two files.+- **A test fixture drawn with `scale=out_color_matrix=bt709` is not untagged on 7.1+.** The scale+ labels its output and x264 writes that label, so an "untagged" clip made that way probes as+ `color_space=bt709` on the trixie/macOS/Windows jobs and the untagged case silently went untested+ there (#333's colour fixtures until 2.10.3). Relabel the frames `setparams=colorspace=unknown`+ before the encode, and assert the probe says unknown.- **The conda-forge 7.1.1 build deadlocks on `tpad` + `adelay`/`apad` (pad.py) and ignoresSIGTERM.** Debian's 7.1.5 in CI does not. Run pad tests against CI, not that build, and notethat the tools have no subprocess timeout to get an agent out of such a hang.diff --git a/references/gotchas.md b/references/gotchas.mdindex 2e3a0d0..f6ff667 100644--- a/references/gotchas.md+++ b/references/gotchas.md@@ -14,7 +14,9 @@ by every re-encode (through 2.10.0 only the label changed); `color.py --retag btstream copy and still only relabels. Untagged sources are taken as BT.709 and left alone. An RGBpicture (PNG/JPEG still, logo, emoji PNG, PNG sequence, qtrle clip) is converted to BT.709 YUVexplicitly (through 2.10.1 FFmpeg's BT.601 default did it), and `grid.py` converts each cell by its-own tags. When `probe.py` reports+own tags. A colour the tools draw (pad and background colours, text, boxes, captions, waveform+colours) is drawn in BT.709 too; through 2.10.2 it was BT.601 under the BT.709 tag (pure red+showed as (255,24,0)). When `probe.py` reports`bt2020_or_hdr: true` (HDR10/PQ, HLG, Dolby Vision, or BT.2020 SDR), every editing script keepsthe output on the 10-bit path (HEVC Main10, source colour tags) so nothing is silentlyflattened. Decide with the user: keep HDR (fine for YouTube/phones) or rundiff --git a/scripts/_ass_overlay.py b/scripts/_ass_overlay.pyindex fa4ae9e..5f934d3 100644--- a/scripts/_ass_overlay.py+++ b/scripts/_ass_overlay.py@@ -110,7 +110,9 @@ def text_overlay_ass(elements: Sequence[Dict[str, Any]], *, play_w: int, play_h:"""Write `elements` as an ASS file at `path` and return the path."""header = ["[Script Info]", "ScriptType: v4.00+", f"PlayResX: {play_w}", f"PlayResY: {play_h}",- "WrapStyle: 2", "ScaledBorderAndShadow: yes", "",+ # YCbCr Matrix: None -- the colours are RGB, to be put in the video's own matrix: FFmpeg 8.x's+ # ass filter reads an absent header as BT.601 whatever the picture is tagged (drawn_chain)+ "WrapStyle: 2", "ScaledBorderAndShadow: yes", "YCbCr Matrix: None", "","[V4+ Styles]","Format: Name, Fontname, Fontsize, PrimaryColour, SecondaryColour, OutlineColour, ""BackColour, Bold, Italic, Underline, StrikeOut, ScaleX, ScaleY, Spacing, Angle, "diff --git a/scripts/_common/__init__.py b/scripts/_common/__init__.pyindex c6eed95..759d852 100644--- a/scripts/_common/__init__.py+++ b/scripts/_common/__init__.py@@ -83,7 +83,7 @@ from _common.decision import (_onset_strength, _pick_onsets, _autocorrelation_peak, _grid_score,brand_states_font, cfr_args, concat_list_line, db_to_linear, default_output, encoder_args, escape_filter_path,fmt_secs, fmt_smpte_time, fmt_srt_time, frame_size, is_audio_output, load_brand, MissingFpsError, pad_filters, parse_time,- signed_time_arg, source_codec_video_args, label_chain, matrix_chain, rgb_chain_to_bt709, SVT_PRESET, time_arg, video_args, x264_args, _x264_raw+ signed_time_arg, source_codec_video_args, drawn_chain, draws_chroma, label_chain, matrix_chain, rgb_chain_to_bt709, SVT_PRESET, time_arg, video_args, x264_args, _x264_raw)from _common.asr import (ASR_ENGINES, ASR_INSTALL_HINT, _asr_run, die_no_engine, parse_srt, transcribe, _transcribe_in,@@ -91,8 +91,8 @@ from _common.asr import (whisper_word_timings, write_srt)from _common.color import (- bt709_label_filter, bt709_matrix_args, bt709_matrix_filter, bt709_tag_args, color_hex, _COLOR_TOKEN_RE, fold_bt709_matrix,- is_rgb, NON_BT709_MATRICES, rgb_to_bt709_filter, _sdr_bt709, validate_color+ bt709_label_filter, bt709_matrix_args, bt709_matrix_filter, bt709_tag_args, color_hex, _COLOR_TOKEN_RE, drawn_stream_format, fold_bt709_matrix,+ is_neutral_color, is_rgb, NON_BT709_MATRICES, rgb_island, rgb_to_bt709_filter, _sdr_bt709, validate_color)from _common.text import (ADVANCE_EM, BIDI_SCRIPTS, _char_em, char_script, default_font_file, detect_script, drawtext_boxborderw, drawtext_center_align,@@ -213,7 +213,7 @@ __all__ = ["SCRIPTS", "_sdr_bt709", "_set_current_ctx", "_SHAPING_BUILD_CACHE", "SHAPING_SCRIPTS", "shell_quote", "sibling_temp","_SIGNALS_INSTALLED", "signed_time_arg", "_stage_existing_output", "STATE", "SVT_PRESET", "text_width_em","time_arg", "_timed_out", "_to_float", "_to_int", "_unwatch", "validate_color", "verify_output",- "source_codec_video_args", "label_chain", "matrix_chain", "rgb_chain_to_bt709", "bt709_label_filter", "rgb_to_bt709_filter", "is_rgb", "video_args", "_VS15", "_VS16", "_watch", "WINDOWS_FONTS", "write_plan", "x264_args", "X264_PRESETS",+ "source_codec_video_args", "drawn_chain", "draws_chroma", "drawn_stream_format", "is_neutral_color", "rgb_island", "label_chain", "matrix_chain", "rgb_chain_to_bt709", "bt709_label_filter", "rgb_to_bt709_filter", "is_rgb", "video_args", "_VS15", "_VS16", "_watch", "WINDOWS_FONTS", "write_plan", "x264_args", "X264_PRESETS","_x264_raw", "_ZWJ","ASR_ENGINES", "ASR_INSTALL_HINT", "_asr_run", "die_no_engine", "parse_srt", "transcribe","_transcribe_in", "transcribe_words", "_words_from_openai_whisper_json",diff --git a/scripts/_common/color.py b/scripts/_common/color.pyindex 022f7ed..15bb844 100644--- a/scripts/_common/color.py+++ b/scripts/_common/color.py@@ -123,6 +123,75 @@ def bt709_label_filter(meta: Optional[Dict[str, Any]]) -> Optional[str]:return "setparams=colorspace=bt709" + ("" if v.get("color_range") == "pc" else ":range=tv")+# FFmpeg's named colours that are greys (libavutil/parseutils.c's table): R = G = B.+_GREY_NAMES = frozenset(("black", "white", "gray", "grey", "silver", "darkgray", "darkgrey", "dimgray", "dimgrey",+ "gainsboro", "lightgray", "lightgrey", "whitesmoke"))+++def _yuv8(rgb: Sequence[int], kr: float, kb: float) -> "Tuple[int, int, int]":+ """8-bit limited-range YUV of an RGB colour under the matrix (kr, kb), rounded as FFmpeg's+ drawing code rounds it (ff_draw_color)."""+ r, g, b = (c / 255 for c in rgb)+ y = kr * r + (1 - kr - kb) * g + kb * b+ return (int(16 + 219 * y + 0.5), int(128 + 224 * (b - y) / (2 * (1 - kb)) + 0.5),+ int(128 + 224 * (r - y) / (2 * (1 - kr)) + 0.5))+++def is_neutral_color(value: Any) -> bool:+ """True for a colour whose 8-bit YUV is the same under BT.601 and BT.709, however translucent: a+ grey (R = G = B; black and white included), or one so near grey that both round to the same+ bytes (graphics.py's default background 101418). Only another colour lands differently, so only+ it needs drawn_chain(). A name counts only if it is one of FFmpeg's greys; anything else+ (`red`, `random`) is not neutral."""+ v = str(value or "").strip().split("@")[0].lower()+ if v in _GREY_NAMES:+ return True+ v = v[1:] if v.startswith("#") else v[2:] if v.startswith("0x") else v+ if re.fullmatch(r"[0-9a-f]{6}(?:[0-9a-f]{2})?", v):+ rgb = (int(v[0:2], 16), int(v[2:4], 16), int(v[4:6], 16))+ return _yuv8(rgb, 0.299, 0.114) == _yuv8(rgb, 0.2126, 0.0722)+ return False+++def drawn_stream_format(meta: Optional[Dict[str, Any]]) -> "Tuple[str, bool]":+ """(pix_fmt, full range) of the picture a tool draws on, for rgb_island(): the probed source's own+ 8-bit planar YUV format (an RGB source has become yuv444p, BT.709 limited, through+ bt709_matrix_filter), or yuv420p for anything else (the format the encoder line takes it to)."""+ v = (meta or {}).get("video") or {}+ if is_rgb(meta):+ return "yuv444p", False+ pf = str(v.get("pix_fmt") or "yuv420p")+ full = pf.startswith("yuvj") or v.get("color_range") == "pc"+ if pf not in ("yuv420p", "yuv422p", "yuv444p", "yuvj420p", "yuvj422p", "yuvj444p"):+ pf = "yuv420p"+ return pf, full+++def rgb_island(colour_chain: str, neutral_chain: str = "null", pix_fmt: str = "yuv420p", full: bool = False,+ tag: str = "d") -> str:+ """`colour_chain` (filters that draw a colour: drawtext, pad, tpad, rotate's fill, libass) run on+ an RGB copy of the picture, as one simple filtergraph (one input, one output; `tag` keeps its+ labels unique in a bigger graph). For FFmpeg < 7.1, whose drawing filters turn every colour into+ YUV with BT.601 whatever the picture is tagged (`ff_draw_init`), and drawbox on every version:+ in RGB a colour is written as it is, and the copy goes back to YUV with BT.709.++ Only the pixels the drawing touched come from that copy. The same round trip without the colour+ (`neutral_chain`: `null`, or the same pad/tpad/rotate with a black fill, which is the same under+ every matrix) is compared with it plane by plane, and maskedmerge takes the drawn copy where+ the two differ and the untouched picture (`neutral_chain` on the YUV itself) everywhere else:+ the RGB round trip is not lossless, so a picture that only passes through keeps its pixels."""+ rng = "pc" if full else "tv"+ to_rgb = f"scale=in_color_matrix=bt709:in_range={rng},format=rgb24"+ to_yuv = f"scale=out_color_matrix=bt709:out_range={rng},format={pix_fmt}"+ keep = "'255*gt(val,0)'"+ t = f"_{tag}"+ return (f"split=3[{t}p][{t}c][{t}n];[{t}p]format={pix_fmt},{neutral_chain}[{t}p1];"+ f"[{t}c]{to_rgb},{colour_chain},{to_yuv},split[{t}c1][{t}c2];"+ f"[{t}n]{to_rgb},{neutral_chain},{to_yuv}[{t}n1];"+ f"[{t}c1][{t}n1]blend=all_mode=difference,lut=c0={keep}:c1={keep}:c2={keep}[{t}m];"+ f"[{t}p1][{t}c2][{t}m]maskedmerge")++def bt709_matrix_args(meta: Optional[Dict[str, Any]]) -> List[str]:"""["-vf", bt709_matrix_filter(meta)] for an encoder line that tags its output BT.709, or [].diff --git a/scripts/_common/decision.py b/scripts/_common/decision.pyindex 3024d6f..1d68f88 100644--- a/scripts/_common/decision.py+++ b/scripts/_common/decision.py@@ -14,12 +14,14 @@ import argparsefrom fractions import Fractionfrom pathlib import Pathfrom typing import Any, Dict, List, Optional, Sequence, Tuple-from _common.color import bt709_label_filter, bt709_matrix_args, bt709_matrix_filter, bt709_tag_args, rgb_to_bt709_filter, _sdr_bt709+from _common.color import (bt709_label_filter, bt709_matrix_args, bt709_matrix_filter, bt709_tag_args, drawn_stream_format,+ is_neutral_color, rgb_island, rgb_to_bt709_filter, _sdr_bt709)from _common.emit import diefrom _common.runner import CODECS, STATE, ffmpeg_encoders, ffmpeg_version, hw_platform_reason-def pad_filters(out_w: int, out_h: int, fill: str, color: str, blur: int, darken: float = 0.0) -> str:+def pad_filters(out_w: int, out_h: int, fill: str, color: str, blur: int, darken: float = 0.0,+ meta: Optional[Dict[str, Any]] = None) -> str:"""The letterbox/pillarbox step shared by fit.py and export.py, as one -vf segment.fill="color": scale to fit, then pad with a solid colour (the historical behaviour).@@ -30,7 +32,10 @@ def pad_filters(out_w: int, out_h: int, fill: str, color: str, blur: int, darkenneeded beyond the usual scale/pad set, and that is already required by redact.py.`darken` > 0 also dims that background copy by that much brightness (eq), so the picture infront reads as the subject instead of competing with a bright blurred copy of itself --- what `fit.py --fit blur` uses (1.14)."""+ what `fit.py --fit blur` uses (1.14).+ `meta`: the probe of the picture being padded, when the output is that picture's re-encode: a+ --pad-color with chroma is then drawn in the BT.709 the line tags (drawn_chain()). Without it+ (a still made RGB, an export to GIF) the pad is what it always was."""if fill == "blur":# boxblur rejects a radius larger than half the smaller dimension ("radius 20, must be# <= 8" on a 16 px target); clamp instead of failing an otherwise valid request@@ -40,7 +45,9 @@ def pad_filters(out_w: int, out_h: int, fill: str, color: str, blur: int, darkenf"boxblur={radius}:2" + (f",eq=brightness=-{darken:g}" if darken else "") + "[__fitbgb];"f"[__fitfg]scale={out_w}:{out_h}:force_original_aspect_ratio=decrease[__fitfgs];"f"[__fitbgb][__fitfgs]overlay=(W-w)/2:(H-h)/2:format=auto")- return f"scale={out_w}:{out_h}:force_original_aspect_ratio=decrease,pad={out_w}:{out_h}:(ow-iw)/2:(oh-ih)/2:color={color}"+ pad = f"pad={out_w}:{out_h}:(ow-iw)/2:(oh-ih)/2:color="+ return (f"scale={out_w}:{out_h}:force_original_aspect_ratio=decrease,"+ + (pad + color if meta is None else drawn_chain(pad + color, meta, [color], pad + "black", tag="pad")))def add_pad_fill_args(parser: "argparse.ArgumentParser") -> None:@@ -574,6 +581,39 @@ def rgb_chain_to_bt709(line_meta: Optional[Dict[str, Any]], pix_fmt: str = "yuvareturn "," + rgb_to_bt709_filter(pix_fmt)+def draws_chroma(colours: Sequence[Any], line_meta: Optional[Dict[str, Any]]) -> bool:+ """True when one of `colours` has chroma (is_neutral_color) and the line video_args(line_meta)+ tags the output BT.709 -- when a drawn colour needs drawn_chain() at all."""+ return (not all(is_neutral_color(c) for c in colours if c is not None) and STATE.codec != "prores"+ and not ((line_meta or {}).get("video") or {}).get("bt2020_or_hdr"))+++def drawn_chain(colour_chain: str, meta: Optional[Dict[str, Any]], colours: Sequence[Any], neutral_chain: str = "null",+ line_meta: Any = "same", tag: str = "d", labelled: bool = False) -> str:+ """`colour_chain` -- filters that draw a colour into the picture `meta` probes (drawtext's+ font/box/border colours, pad/tpad's fill, rotate's fillcolor, libass) -- so that a colour of+ `colours` lands in the BT.709 the line video_args(line_meta) tags the output with (line_meta+ defaults to meta).++ FFmpeg's drawing filters turn an RGB colour into YUV with a matrix of their own (ff_draw):+ BT.601 always up to 7.0, the link's colourspace from 7.1 on, "unknown" read as BT.601. Through+ 2.10.2 a drawn colour was BT.601 under a BT.709 tag on every build (pure red decoded as+ (255,24,0)), and on 5.1/6.1 for a BT.709-tagged source too. From 7.1 an untagged picture gets+ its BT.709 label (label_chain(): tags only) and the filters follow it; before 7.1 the drawing+ runs in RGB (rgb_island(), `neutral_chain` being the same filters with a black fill, or null).++ Unchanged: a chain whose colours are all greys (the same YUV under every matrix: the default+ black pads and white text), a line that does not tag BT.709 (HDR, prores). `labelled`: the+ caller's graph already labels this picture (label_chain()) earlier in the same chain."""+ line = meta if line_meta == "same" else line_meta+ if not draws_chroma(colours, line):+ return colour_chain+ if ffmpeg_version() >= (7, 1):+ return ("" if labelled else label_chain(meta, line)) + colour_chain+ pix_fmt, full = drawn_stream_format(meta)+ return rgb_island(colour_chain, neutral_chain, pix_fmt, full, tag)++def source_codec_video_args(meta: Optional[Dict[str, Any]], crf: int = 18, preset: str = "medium",keep_hevc: bool = False) -> List[str]:"""video_args(), except that with `keep_hevc` (cut.py --keep-hevc) an SDR HEVC source re-encodesdiff --git a/scripts/background.py b/scripts/background.pyindex a784fb0..8312ad9 100644--- a/scripts/background.py+++ b/scripts/background.py@@ -14,7 +14,7 @@ import argparseimport mathimport sys-from _common import add_common, apply_common, die, emit, ffmpeg_base, info, parse_time, probe, run, validate_color, video_args, X264_PRESETS, time_arg, fmt_secs+from _common import add_common, apply_common, die, draws_chroma, emit, ffmpeg_base, info, parse_time, probe, rgb_chain_to_bt709, run, validate_color, video_args, X264_PRESETS, time_arg, fmt_secsdef main() -> int:@@ -61,9 +61,16 @@ def main() -> int:# speed's own valid range bottoms out at 1e-05 (0 is refused), so that's the closest to# motionless the filter allows.src_filter = f"gradients=size={args.width}x{args.height}:rate={args.fps:g}:c0={c0}:c1={c1}:x0=0:y0=0:x1={x1}:y1={y1}:seed=0:speed=1e-05"+ colours = [c0, c1]else:validate_color(args.color, "--color")src_filter = f"color=c={args.color}:size={args.width}x{args.height}:rate={args.fps:g}"+ colours = [args.color]+ if draws_chroma(colours, None):+ # the colours are made in RGB and converted to the BT.709 the output is tagged with: left to+ # the encoder line's own conversion (or `color`'s YUV), they were BT.601 under that tag+ # (pure red decoded as (255,24,0)); a grey is the same under every matrix+ src_filter += (",format=rgb24" if not args.gradient else "") + rgb_chain_to_bt709(None, "yuv420p")output = args.outputcmd = ffmpeg_base() + ["-f", "lavfi", "-i", src_filter, "-t", f"{target:.3f}"]diff --git a/scripts/broll.py b/scripts/broll.pyindex 4fc366d..a7966ba 100755--- a/scripts/broll.py+++ b/scripts/broll.py@@ -21,7 +21,7 @@ import argparseimport sysfrom typing import Any, Dict, List-from _common import STATE, add_common, aac_args, apply_common, cfr_args, default_output, die, emit, ffmpeg_base, info, time_arg, matrix_chain, probe, run, validate_color, video_args, X264_PRESETS, fmt_secs+from _common import STATE, add_common, aac_args, apply_common, cfr_args, default_output, die, draws_chroma, drawn_chain, emit, ffmpeg_base, info, time_arg, label_chain, matrix_chain, probe, run, validate_color, video_args, X264_PRESETS, fmt_secsdef main() -> int:@@ -101,14 +101,22 @@ def main() -> int:# A shows through unchanged, so A's timeline is never touched.parts: List[str] = []cur = "[0:v]"- # every clip is converted by its own colour tags to the BT.709 the output is tagged with- if matrix_chain(meta_a, meta_a):- parts.append(f"[0:v]{matrix_chain(meta_a, meta_a)}null[a709]")+ # every clip is converted by its own colour tags to the BT.709 the output is tagged with; beside+ # a converted clip (or under a --pad-color with chroma) an untagged one gets its BT.709 label,+ # tags only, so 7.1+'s overlay and pad have nothing to convert or misread+ convert = [matrix_chain(m, meta_a) for m in [meta_a] + [c["meta"] for c in cutaways]]+ if any(convert) or draws_chroma([args.pad_color], meta_a):+ convert = [cv or label_chain(m, meta_a) for cv, m in zip(convert, [meta_a] + [c["meta"] for c in cutaways])]+ if convert[0]:+ parts.append(f"[0:v]{convert[0]}null[a709]")cur = "[a709]"+ pad = f"pad={w}:{h}:(ow-iw)/2:(oh-ih)/2:color="for i, c in enumerate(cutaways):- geo = f"scale={w}:{h}:force_original_aspect_ratio=decrease,pad={w}:{h}:(ow-iw)/2:(oh-ih)/2:color={args.pad_color}"+ geo = (f"scale={w}:{h}:force_original_aspect_ratio=decrease,"+ + drawn_chain(pad + args.pad_color, c["meta"], [args.pad_color], pad + "black", line_meta=meta_a,+ tag=f"pad{i}", labelled=True))parts.append(f"[{i + 1}:v]trim=start={c['from']:.3f}:duration={c['length']:.3f},setpts=PTS-STARTPTS+{c['at']:.3f}/TB,"- f"{matrix_chain(c['meta'], meta_a)}{geo},setsar=1,fps={fps:g},format={'yuv420p10le' if (meta_a.get('video') or {}).get('bt2020_or_hdr') else 'yuv420p'}[b{i}]")+ f"{convert[i + 1]}{geo},setsar=1,fps={fps:g},format={'yuv420p10le' if (meta_a.get('video') or {}).get('bt2020_or_hdr') else 'yuv420p'}[b{i}]")parts.append(f"{cur}[b{i}]overlay=0:0:eof_action=pass:enable='between(t,{c['at']:.3f},{c['at'] + c['length']:.3f})'[v{i}]")cur = f"[v{i}]"vout = curdiff --git a/scripts/caption.py b/scripts/caption.pyindex 84c5ec2..f620706 100755--- a/scripts/caption.py+++ b/scripts/caption.py@@ -44,7 +44,7 @@ from typing import Any, Dict, List, Optional, Tuplefrom _platforms import (PLATFORMS, PLATFORM_CHOICES, ASS_SCRIPT_HEIGHT, ass_units,resolve as resolve_platform)from _ass_overlay import EMOJI_SENTINEL, emoji_placeholder, ass_escape-from _common import emoji_filter_chain, EMOJI_ASSET_HINT, emoji_asset_for, emoji_codepoint_name, emoji_support, resolve_emoji_assets, ADVANCE_EM, LATIN_EM, NO_SPACE_SCRIPTS, _char_em, char_script, text_width_em, emoji_clusters, has_emoji, detect_script, BIDI_SCRIPTS, STATE, brand_states_font, script_font_for_text, signed_time_arg, brand_caption_style, color_hex, label_chain, load_brand, rgb_chain_to_bt709, video_args, add_common, apply_common, emit, aac_args, cfr_args, default_output, die, escape_filter_path, ffmpeg_base, fmt_srt_time, fmt_smpte_time, info, MissingFpsError, parse_time, probe, run, x264_args, X264_PRESETS, read_text_or_die, fmt_secs, refuse_existing_outputs+from _common import emoji_filter_chain, EMOJI_ASSET_HINT, emoji_asset_for, emoji_codepoint_name, emoji_support, resolve_emoji_assets, ADVANCE_EM, LATIN_EM, NO_SPACE_SCRIPTS, _char_em, char_script, text_width_em, emoji_clusters, has_emoji, detect_script, BIDI_SCRIPTS, STATE, brand_states_font, script_font_for_text, signed_time_arg, brand_caption_style, color_hex, drawn_chain, label_chain, load_brand, rgb_chain_to_bt709, video_args, add_common, apply_common, emit, aac_args, cfr_args, default_output, die, escape_filter_path, ffmpeg_base, fmt_srt_time, fmt_smpte_time, info, MissingFpsError, parse_time, probe, run, x264_args, X264_PRESETS, read_text_or_die, fmt_secs, refuse_existing_outputs# The line breaker, lifted into _common/text.py in 1.16.0 so graphics.py can use the same rules.# The ASR bridge and the SRT reader/writer live in _common.asr since 1.17 (silence.py --filler# shares them). They stay caption.py's public names -- every caller and test that reached for@@ -633,7 +633,9 @@ def write_ass(cues: List[Tuple[float, float, str]], path: str, args, play_w: int# jump every time the highlight advances (the trap the issue this implements is filed against).header = ["[Script Info]", "ScriptType: v4.00+", f"PlayResX: {play_w}", f"PlayResY: {play_h}",- f"WrapStyle: {2 if word_style else 0}", "ScaledBorderAndShadow: yes", "",+ # YCbCr Matrix: None -- the colours are RGB, to be put in the video's own matrix: FFmpeg 8.x's+ # ass filter reads an absent header as BT.601 whatever the picture is tagged (drawn_chain)+ f"WrapStyle: {2 if word_style else 0}", "ScaledBorderAndShadow: yes", "YCbCr Matrix: None", "","[V4+ Styles]","Format: Name, Fontname, Fontsize, PrimaryColour, SecondaryColour, OutlineColour, BackColour, Bold, Italic, Underline, StrikeOut, ScaleX, ScaleY, Spacing, Angle, BorderStyle, Outline, Shadow, Alignment, MarginL, MarginR, MarginV, Encoding",f"Style: Default,{ass_font_name(args.font)},{size},{primary},{secondary},{outline},{back},{-1 if args.bold else 0},0,0,0,100,100,0,0,{3 if args.box else 1},{args.outline * scale:.1f},{args.shadow * scale:.1f},{ALIGN[args.position]},{margin_l},{margin_r},{margin},1",@@ -1560,6 +1562,12 @@ def main() -> int:vf = f"subtitles={escape_filter_path(srt_path)}:force_style='{force}'"if args.fonts_dir:vf += f":fontsdir={escape_filter_path(args.fonts_dir)}"+ if generated_ass or not args.ass:+ # a text/outline/karaoke colour with chroma lands in the BT.709 the output is tagged with+ # (drawn_chain). A caller's own --ass is burned as libass and FFmpeg read it: its colours,+ # and its YCbCr Matrix header, are the file's.+ vf = drawn_chain(vf, meta, [args.color, args.outline_color]+ + ([args.highlight_color, args.upcoming_color] if args.karaoke else []))cmd = ffmpeg_base() + ["-i", args.input]chains, emoji_inputs = (emoji_filter_chain(emoji_plan or {}, "vsub", "vout", to_yuv=rgb_chain_to_bt709(meta),diff --git a/scripts/export.py b/scripts/export.pyindex ddf2446..3d16f88 100755--- a/scripts/export.py+++ b/scripts/export.py@@ -165,7 +165,10 @@ def main() -> int:if args.fit == "crop":vf += [f"scale={p['w']}:{p['h']}:force_original_aspect_ratio=increase", f"crop={p['w']}:{p['h']}"]else:- vf.append(pad_filters(p["w"], p["h"], args.pad_fill, args.pad_color, args.pad_blur))+ # a --pad-color with chroma is drawn in the BT.709 the line is tagged with; a GIF is+ # not tagged, and palettegen reads the pad back with the same BT.601 it was drawn in+ vf.append(pad_filters(p["w"], p["h"], args.pad_fill, args.pad_color, args.pad_blur,+ meta=None if args.preset == "gif" else meta))vf.append("setsar=1")else:vf.append(f"scale={p['w']}:-2")diff --git a/scripts/fit.py b/scripts/fit.pyindex e6114ea..f8e9bff 100755--- a/scripts/fit.py+++ b/scripts/fit.py@@ -202,7 +202,7 @@ def main() -> int:"run color.py --to-sdr first for the SDR look")vf.append(pad_filters(out_w, out_h, "blur", args.pad_color, args.pad_blur, darken))else:- vf.append(pad_filters(out_w, out_h, args.pad_fill, args.pad_color, args.pad_blur))+ vf.append(pad_filters(out_w, out_h, args.pad_fill, args.pad_color, args.pad_blur, meta=meta))vf.append("setsar=1")if args.fps:diff --git a/scripts/graphics.py b/scripts/graphics.pyindex 546d439..6cf2c19 100755--- a/scripts/graphics.py+++ b/scripts/graphics.py@@ -33,7 +33,7 @@ from typing import List, Optionalfrom _platforms import PLATFORMS, PLATFORM_CHOICES, safe_margins_px, resolve as resolve_platformfrom _common import (aac_args, add_common, brand_caption_style, script_font_for_text, apply_common, cfr_args,- color_hex, default_font_file, default_output, die, emit, escape_drawtext, escape_filter_path,+ color_hex, default_font_file, default_output, die, draws_chroma, drawn_chain, emit, escape_drawtext, escape_filter_path,ffmpeg_base, info, label_chain, load_brand, parse_time, probe, rgb_chain_to_bt709, run, run_keeping_subtitles, video_args,drawtext_boxborderw, drawtext_center_align, X264_PRESETS, time_arg, fmt_secs, STATE, drawtext_text_opts,LANGUAGE_NAMES, needs_shaping, detect_script, BIDI_SCRIPTS, font_family_of_file, font_family_for_script, has_emoji,@@ -42,6 +42,9 @@ from _common import (aac_args, add_common, brand_caption_style, script_font_for_SAFE_WIDTH_FRACTION)from _ass_overlay import text_overlay_ass, EMOJI_SENTINEL+# where the template's text chain goes in a filter_complex it builds (graphics.main fills it in)+TEXT = "@TEXT@"+BOX = "@BOX@" # a box's place in the filter chain, followed by its index (graphics.main fill_box)TEMPLATES = ["lower-third", "title", "chapter", "progress", "countdown", "bug", "sticker", "hook", "meme"]@@ -185,6 +188,35 @@ def main() -> int:en = f"enable='between(t,{s:.3f},{e:.3f})'"base = min(W, H) * args.scale # scale everything from the short sidefilters: List[str] = []+ rate = f"{meta['video'].get('fps') or 30:g}"+ # A drawn colour lands in the BT.709 the output is tagged with. When a template's box has+ # chroma, its boxes are colour sources made in RGB and converted (rgb_chain_to_bt709), overlaid+ # under the text, in order: drawbox turns its colour into YUV with BT.601 on every FFmpeg (and+ # blends 4:2:0 chroma four times over, so a translucent box was nearly opaque in colour). A+ # template whose boxes are all grey keeps its drawboxes, which are the same under every matrix.+ # The text (drawtext, libass) goes through drawn_chain() at the end.+ boxes: List[tuple] = [] # (colour, alpha, drawbox geometry, x, y, w, h, enable, x_expr), in order+ layers: List[tuple] = [] # (colour source chain, overlay position and enable), under the text+ converted: List[str] = [] # the colour sources layer_src() converted++ def fill_box(colour: str, alpha: float, drawbox_geo: str, x: int, y: int, w: int, h: int, enable: str,+ x_expr: Optional[str] = None) -> None:+ """A filled box: a drawbox (`drawbox_geo`) at its place in the filter chain, or a colour layer+ of w x h at x, y (or at the per-frame `x_expr`) -- decided for all of a template's boxes."""+ filters.append(f"{BOX}{len(boxes)}")+ boxes.append((colour, alpha, drawbox_geo, x, y, w, h, enable, x_expr))++ def layer_src(colour: str, alpha: float, w: int, h: int, left: int = 0, top: int = 0) -> str:+ """A colour source to overlay (a box, the lower-third bar, the progress fill), converted when it+ has chroma. overlay places a 4:2:0 picture on even rows and columns only: a box that starts on+ an odd one is placed one earlier with a transparent first column/row (`left`, `top`), so it+ covers the rows and columns the drawbox did."""+ conv = rgb_chain_to_bt709(meta) if draws_chroma([colour], meta) else ""+ if conv:+ converted.append(colour)+ edge = f",pad={w + left}:{h + top}:{left}:{top}:color=black@0" if left or top else ""+ return (f"color=c=0x{colour}" + (f"@{alpha:g}" if alpha < 1 else "") + f":s={w}x{h}:r={rate},format=rgba"+ + edge + conv)fade_a = f"if(lt(t,{s:.3f}+0.3),(t-{s:.3f})/0.3,if(gt(t,{e:.3f}-0.3),({e:.3f}-t)/0.3,1))"# --- which renderer draws the text (1.15) --------------------------------------------------@@ -333,8 +365,8 @@ def main() -> int:y0 = H - m_bottom - bar_h# slide in from the left over 0.4 s, slide out over 0.3 s (overlay evaluates x per frame)x_expr = f"if(lt(t,{s:.3f}+0.4),-{bar_w}+({bar_w}+{m_left})*((t-{s:.3f})/0.4),if(gt(t,{e:.3f}-0.3),{m_left}-({bar_w}+{m_left})*(1-({e:.3f}-t)/0.3),{m_left}))"- fc.append(f"color=c=0x{bg}@0.85:s={bar_w}x{bar_h}:r={meta['video'].get('fps') or 30:g},format=rgba[bar]")- fc.append(f"color=c=0x{primary}:s={int(base * 0.012)}x{bar_h}:r={meta['video'].get('fps') or 30:g},format=rgba[acc]")+ fc.append(f"{layer_src(bg, 0.85, bar_w, bar_h)}[bar]")+ fc.append(f"{layer_src(primary, 1.0, int(base * 0.012), bar_h)}[acc]")fc.append(f"[0:v][bar]overlay=x='{x_expr}':y={y0}:{en}:eof_action=pass[v1]")fc.append(f"[v1][acc]overlay=x='{x_expr}':y={y0}:{en}:eof_action=pass[v2]")tx_pad = int(base * 0.035)@@ -361,14 +393,16 @@ def main() -> int:align=7, x=x_rest, y=ty, outline=max(1.0, fs / 16.0),outline_color="000000", start=max(s, e - 0.3), end=e,move=(x_rest, ty, x_off, ty, 0, 300), x_expr=tx))- fc.append(f"[v2]{','.join(draws)}[vout]" if route != "ass" else "[v2]null[vout]")+ fc.append(f"[v2]{TEXT}[vout]" if route != "ass" else "[v2]null[vout]")+ if route != "ass":+ filters.extend(draws) # the text chain, which drawn_chain() places at TEXTelif args.template == "title":if not args.title:die("title needs --title")h1 = int(base * 0.11)h2 = int(base * 0.045)- filters.append(f"drawbox=x=0:y=0:w=iw:h=ih:color={ff_color(bg, 0.55)}:t=fill:{en}")+ fill_box(bg, 0.55, "x=0:y=0:w=iw:h=ih", 0, 0, W, H, en)# the title is centred as a block, so the bar and the subtitle sit below its last line:# `title_h` is the block's height, as the one-line layout has always taken h1; a further# subtitle line hangs below the first, whose position the one-line layout fixes@@ -380,7 +414,9 @@ def main() -> int:size=h1, color=text_c, font=ass_font_family(), align=5, x=W / 2,y=H / 2 - (h2 if args.subtitle else 0), outline=max(1.0, h1 / 20.0),outline_color="000000", start=s, end=e, fade=(300, 300))- filters.append(f"drawbox=x=(iw-{int(base * 0.12)})/2:y=(ih)/2+{title_h // 2}:w={int(base * 0.12)}:h={max(2, int(base * 0.006))}:color={ff_color(primary)}:t=fill:{en}")+ acc_w, acc_h = int(base * 0.12), max(2, int(base * 0.006))+ fill_box(primary, 1.0, f"x=(iw-{acc_w})/2:y=(ih)/2+{title_h // 2}:w={acc_w}:h={acc_h}",+ (W - acc_w) // 2, H // 2 + title_h // 2, acc_w, acc_h, en)if args.subtitle:add_text(args.subtitle,f"drawtext={drawtext_text_opts(args.subtitle)}:{fo}:fontsize={h2}:fontcolor={ff_color(primary)}{drawtext_center_align()}:x=(w-text_w)/2:y=(h-text_h)/2+{sub_y + sub_extra // 2}:alpha='{fade_a}':{en}",@@ -412,8 +448,12 @@ def main() -> int:elif args.template == "progress":h = max(3, int(base * 0.008))fps = meta['video'].get('fps') or 30- fc.append(f"color=c=0x{primary}:s={W}x{h}:r={fps:g},format=rgba[pb]")- fc.append(f"[0:v]drawbox=x=0:y=ih-{h}:w=iw:h={h}:color={ff_color(bg, 0.5)}:t=fill:{en}[v1]")+ fc.append(f"{layer_src(primary, 1.0, W, h)}[pb]")+ if draws_chroma([bg], meta):+ fc.append(f"{layer_src(bg, 0.5, W, h, 0, (H - h) % 2)}[pbg]")+ fc.append(f"[0:v][pbg]overlay=x=0:y={(H - h) // 2 * 2}:{en}:eof_action=pass[v1]")+ else:+ fc.append(f"[0:v]drawbox=x=0:y=ih-{h}:w=iw:h={h}:color={ff_color(bg, 0.5)}:t=fill:{en}[v1]")fc.append(f"[v1][pb]overlay=x='-w+w*min(1,max(0,(t-{s:.3f})/{e - s:.3f}))':y={H - h}:{en}:eof_action=pass[vout]")elif args.template == "sticker":@@ -465,14 +505,15 @@ def main() -> int:# drawing white text on the bare video. The band is clipped at the frame edges.band_h = min(H, max(int(base * 0.30), block_h(hook_title, h1) + int(h1 * 0.7)))y0 = (H - band_h) // 2- filters.append(f"drawbox=x=0:y={y0}:w=iw:h={band_h}:color={ff_color(bg, 0.78)}:t=fill:{hen}")+ fill_box(bg, 0.78, f"x=0:y={y0}:w=iw:h={band_h}", 0, y0, W, band_h, hen)add_text(hook_title,f"drawtext={drawtext_text_opts(hook_title)}:{fo}:fontsize={h1}:fontcolor={ff_color(text_c)}"f"{drawtext_center_align()}:x=(w-text_w)/2:y=(h-text_h)/2:{hen}",size=h1, color=text_c, font=ass_font_family(), align=5, x=W / 2, y=H / 2,outline=max(1.0, h1 / 20.0), outline_color="000000", start=s, end=he)- filters.append(f"drawbox=x=0:y=0:w='iw*max(0,1-(t-{s:.3f})/{max(0.001, he - s):.3f})':h={bar_h}:"- f"color={ff_color(primary)}:t=fill:{hen}")+ # the bar empties from the right: the overlay slides a full-width bar out to the left+ fill_box(primary, 1.0, f"x=0:y=0:w='iw*max(0,1-(t-{s:.3f})/{max(0.001, he - s):.3f})':h={bar_h}", 0, 0, W, bar_h,+ hen, x_expr=f"'-W*min(1,max(0,(t-{s:.3f})/{max(0.001, he - s):.3f}))'")elif args.template == "meme":# The classic layout: heavy white upper-case lines with a black outline, top and bottom,@@ -588,15 +629,42 @@ def main() -> int:"clusters": sorted({emoji_codepoint_name(cl) for _i, cl in emoji_clusters(all_text)}),"assets": emoji_assets, "missing": [], "overlays": 0}+ # The boxes with chroma (layers) go under the text, and the picture they and a converted colour+ # source are overlaid on gets its BT.709 label (an untagged one, tags only), so 7.1+'s overlay+ # converts neither picture to the other's matrix. The text is drawn in BT.709 (drawn_chain).+ as_layers = any(draws_chroma([b[0]], meta) for b in boxes)+ drawboxes = []+ for colour, alpha, geo, x, y, w, h, enable, x_expr in boxes:+ if as_layers:+ layers.append((layer_src(colour, alpha, w, h, x % 2, y % 2), f"x={x_expr or x - x % 2}:y={y - y % 2}:{enable}"))+ drawboxes.append(f"drawbox={geo}:color={ff_color(colour, alpha)}:t=fill:{enable}")+ filters = [("" if as_layers else drawboxes[int(f[len(BOX):])]) if f.startswith(BOX) else f for f in filters]+ filters = [f for f in filters if f]+ base_label = label_chain(meta, meta) if (layers or converted) else ""+ text = (drawn_chain(",".join(filters), meta, re.findall(r"color=([0-9A-Za-z#@.]+)", ",".join(filters)),+ labelled=bool(base_label)) if filters else "")+ if layers:+ cur = "0:v"+ for k, (src, pos) in enumerate(layers):+ fc.append(f"{src}[gl{k}]")+ fc.append(f"[{cur}][gl{k}]overlay={pos}:eof_action=pass[gv{k}]")+ cur = f"gv{k}"+ fc.append(f"[{cur}]{TEXT}[vout]")+ fc = [c.replace(TEXT, text or "null") for c in fc]+ if fc and base_label:+ fc = [f"[0:v]{base_label}null[gbase]"] + [c.replace("[0:v]", "[gbase]") for c in fc]+cmd = ffmpeg_base() + ["-i", args.input]if route == "ass" or emoji_overlays:- chains = list(fc) if fc else [f"[0:v]{','.join(filters) if filters else 'null'}[vout]"]+ chains = list(fc) if fc else [f"[0:v]{text or 'null'}[vout]"]last = "vout"if route == "ass":vf = f"ass={escape_filter_path(ass_path)}"fdir = ass_fonts_dir()if fdir:vf += f":fontsdir={escape_filter_path(fdir)}"+ vf = drawn_chain(vf, meta, [el.get(k) for el in elements for k in ("color", "outline_color", "box_color")],+ labelled=bool(base_label))chains.append(f"[{last}]{vf}[vtxt]")last = "vtxt"eo, emoji_inputs = emoji_filter_chain({"overlays": emoji_overlays}, last, "vfinal", first_input=1,@@ -615,7 +683,7 @@ def main() -> int:elif fc:cmd += ["-filter_complex", ";".join(fc), "-map", "[vout]", "-map", f"0:a:{args.audio_stream}?"]else:- cmd += ["-vf", ",".join(filters), "-map", "0:v:0", "-map", f"0:a:{args.audio_stream}?"]+ cmd += ["-vf", text, "-map", "0:v:0", "-map", f"0:a:{args.audio_stream}?"]cmd += video_args(meta, args.crf, args.preset) + cfr_args(meta)cmd += aac_args() if meta.get("audio") else ["-an"]dropped_streams = run_keeping_subtitles(cmd, output)diff --git a/scripts/grid.py b/scripts/grid.pyindex d278aab..d314192 100644--- a/scripts/grid.py+++ b/scripts/grid.py@@ -26,7 +26,7 @@ import osimport sysfrom _common import add_common, apply_common, aac_args, cfr_args, default_font_file, default_output, die, emit, X264_PRESETS, fmt_secs, \- escape_drawtext, escape_filter_path, ffmpeg_base, info, label_chain, matrix_chain, probe, run, validate_color, video_args+ draws_chroma, drawn_chain, escape_drawtext, escape_filter_path, ffmpeg_base, info, label_chain, matrix_chain, probe, run, validate_color, video_argsLABEL_MARGIN = 10@@ -95,28 +95,39 @@ def main() -> int:# input): without it 5.1/6.1 relabelled them, and 7.1+ converted cells to one another's matrix.# Beside a converted cell an untagged one gets a BT.709 label, so 7.1+'s xstack has nothing to# convert; a grid of untagged/BT.709 clips is what it was.+ # A --background or --font-color with chroma is drawn in the BT.709 the grid is tagged with+ # (drawn_chain): every untagged cell is labelled for it too, and the gaps are each cell's own+ # pad instead of xstack's fill, which 5.1/6.1 draw in BT.601 (a grey needs neither).+ colours = [args.background] + ([args.font_color] if args.label == "auto" else [])+ chroma = draws_chroma(colours, None)convert = [matrix_chain(m, None) for m in metas]- if any(convert):+ if any(convert) or chroma:convert = [c or label_chain(m, None) for c, m in zip(convert, metas)]parts = []for i, p in enumerate(args.inputs):- chain = [- f"fps={args.fps:g}",- f"scale={args.cell_width}:{args.cell_height}:force_original_aspect_ratio=decrease",- f"pad={args.cell_width}:{args.cell_height}:(ow-iw)/2:(oh-ih)/2:color={args.background}",- "setsar=1",+ drawn = [ # (with this colour, with black)+ (f"pad={args.cell_width}:{args.cell_height}:(ow-iw)/2:(oh-ih)/2:color={args.background}",+ f"pad={args.cell_width}:{args.cell_height}:(ow-iw)/2:(oh-ih)/2:color=black"),+ ("setsar=1",) * 2,]if args.pad and durations[i] < target_duration:- chain.append(f"tpad=stop_mode=clone:stop_duration={target_duration - durations[i]:.3f}")+ drawn.append((f"tpad=stop_mode=clone:stop_duration={target_duration - durations[i]:.3f}",) * 2)elif not args.pad:- chain.append(f"trim=duration={target_duration:.3f}")+ drawn.append((f"trim=duration={target_duration:.3f}",) * 2)if args.label == "auto":stem = os.path.splitext(os.path.basename(p))[0]font_opt = f"fontfile={escape_filter_path(font_file)}" if font_file else f"font='{escape_drawtext(args.font)}'"- chain.append(f"drawtext=text='{escape_drawtext(stem)}':{font_opt}:fontsize={args.font_size}:"- f"fontcolor={args.font_color}:x=w-tw-{LABEL_MARGIN}:y=h-th-{LABEL_MARGIN}:"- f"box=1:[email protected]:boxborderw=4")- parts.append(f"[{i}:v]{convert[i]}{','.join(chain)}[v{i}]")+ drawn.append((f"drawtext=text='{escape_drawtext(stem)}':{font_opt}:fontsize={args.font_size}:"+ f"fontcolor={args.font_color}:x=w-tw-{LABEL_MARGIN}:y=h-th-{LABEL_MARGIN}:"+ f"box=1:[email protected]:boxborderw=4", "null"))+ gap_r = args.gap if i % args.cols < args.cols - 1 else 0+ gap_b = args.gap if i // args.cols < args.rows - 1 else 0+ if chroma and (gap_r or gap_b):+ drawn.append((f"pad=iw+{gap_r}:ih+{gap_b}:0:0:color={args.background}", f"pad=iw+{gap_r}:ih+{gap_b}:0:0:color=black"))+ chain = f"fps={args.fps:g},scale={args.cell_width}:{args.cell_height}:force_original_aspect_ratio=decrease,"+ chain += drawn_chain(",".join(c for c, _n in drawn), metas[i], colours, ",".join(n for _c, n in drawn),+ line_meta=None, tag=f"c{i}", labelled=True)+ parts.append(f"[{i}:v]{convert[i]}{chain}[v{i}]")cw, ch = args.cell_width + args.gap, args.cell_height + args.gaplayout = "|".join(f"{c * cw}_{r * ch}" for r in range(args.rows) for c in range(args.cols))diff --git a/scripts/join.py b/scripts/join.pyindex ea0433e..a38ffe2 100755--- a/scripts/join.py+++ b/scripts/join.py@@ -39,7 +39,7 @@ import osimport sysfrom typing import Any, Dict, List, Optional, Tuple-from _common import STATE, AUDIO_MEDIA_EXT, matrix_chain, video_args, aac_args, add_common, apply_common, audio_codec_for, default_output, die, dry_run_input_pending, emit, ffmpeg_base, info, measured_level_dbfs, is_audio_output, probe, require_tool, run, validate_color, X264_PRESETS, fmt_secs+from _common import STATE, AUDIO_MEDIA_EXT, draws_chroma, drawn_chain, label_chain, matrix_chain, video_args, aac_args, add_common, apply_common, audio_codec_for, default_output, die, dry_run_input_pending, emit, ffmpeg_base, info, measured_level_dbfs, is_audio_output, probe, require_tool, run, validate_color, X264_PRESETS, fmt_secsTRANSITIONS = ["fade", "dissolve", "wipeleft", "wiperight", "wipeup", "wipedown", "slideleft", "slideright","circleopen", "circleclose", "fadeblack", "fadewhite", "smoothleft", "smoothright", "radial", "none"]@@ -478,14 +478,26 @@ def main() -> int:added += 1cmd += extra_inputs- if args.fit == "crop":- geo = f"scale={w}:{h}:force_original_aspect_ratio=increase,crop={w}:{h}"- else:- geo = f"scale={w}:{h}:force_original_aspect_ratio=decrease,pad={w}:{h}:(ow-iw)/2:(oh-ih)/2:color={args.pad_color}"# Any HDR input makes the join HDR (10-bit, HEVC via video_args on that clip's tags): an SDR# first clip used to drag an HDR second clip down to 8-bit without a tone map.hdr_meta = next((m for m in metas if (m.get("video") or {}).get("bt2020_or_hdr")), None)pixfmt = "yuv420p10le" if hdr_meta else "yuv420p"+ line = hdr_meta or metas[0]+ # a BT.601 or RGB clip is converted to the BT.709 the join is tagged with, each clip by its own+ # tags; beside a converted clip (or under a --pad-color with chroma) an untagged one gets its+ # BT.709 label, tags only, so 7.1+'s concat/xfade and pad have nothing to convert or misread+ convert = [matrix_chain(m, line) for m in metas]+ chroma_pad = args.fit != "crop" and draws_chroma([args.pad_color], line)+ if any(convert) or chroma_pad:+ convert = [c or label_chain(m, line) for c, m in zip(convert, metas)]++ def geo(i: int) -> str:+ if args.fit == "crop":+ return f"scale={w}:{h}:force_original_aspect_ratio=increase,crop={w}:{h}"+ pad = f"pad={w}:{h}:(ow-iw)/2:(oh-ih)/2:color="+ return (f"scale={w}:{h}:force_original_aspect_ratio=decrease,"+ + drawn_chain(pad + args.pad_color, metas[i], [args.pad_color], pad + "black", line_meta=line,+ tag=f"pad{i}", labelled=True))# the audio-only join keeps the widest layout; the video join used to force stereo and# silently dropped the centre/LFE of 5.1 material. A pending input's stub has no measured# layout: plan from the inputs that exist, as join_audio() does.@@ -497,8 +509,7 @@ def main() -> int:if len(set(chans)) > 1:info(f"channel counts differ ({', '.join(str(c) for c in chans)}); every clip becomes {layout}")for i in range(n):- # a BT.601 clip is converted to the BT.709 the join is tagged with, each clip by its own tags- parts.append(f"[{i}:v]{matrix_chain(metas[i], hdr_meta or metas[0])}{geo},setsar=1,fps={fps:g},format={pixfmt},settb=AVTB[v{i}]")+ parts.append(f"[{i}:v]{convert[i]}{geo(i)},setsar=1,fps={fps:g},format={pixfmt},settb=AVTB[v{i}]")parts.append(f"[{audio_src[i]}]aformat=sample_rates=48000:channel_layouts={layout},asetpts=PTS-STARTPTS[a{i}]")# pad the shorter stream to the clip's length: the audio with silence, the picture by holdingdiff --git a/scripts/multicam.py b/scripts/multicam.pyindex 13b8fde..1b04a32 100755--- a/scripts/multicam.py+++ b/scripts/multicam.py@@ -31,7 +31,7 @@ import mathimport sysfrom typing import Dict, List, Tuple-from _common import matrix_chain, video_args, aac_args, add_common, apply_common, default_output, die, emit, ffmpeg_base, info, time_arg, probe, run, x264_args, X264_PRESETS, fmt_secs, decode_pcm_mono, rms_envelope, STATE+from _common import label_chain, matrix_chain, video_args, aac_args, add_common, apply_common, default_output, die, emit, ffmpeg_base, info, time_arg, probe, run, x264_args, X264_PRESETS, fmt_secs, decode_pcm_mono, rms_envelope, STATEfrom sync import measure_offsetENERGY_WINDOW_S = 0.25 # --switch energy: loudness measured over this window on the reference timeline@@ -269,6 +269,12 @@ def main() -> int:parts: List[str] = []labels: List[str] = []project_clips: List[Dict] = []+ # each camera converted by its own colour tags to the BT.709 the cut is tagged with; beside a+ # converted camera an untagged one gets its BT.709 label, tags only, so 7.1+'s concat does not+ # convert one to the other's "unknown" (BT.601) matrix+ convert = [matrix_chain(m, metas[0]) for m in metas]+ if any(convert):+ convert = [c or label_chain(m, metas[0]) for c, m in zip(convert, metas)]for i, (s, e, c) in enumerate(filled):# reference time t maps to source time (t - offset_c) * ratio_csrc_s = (s - offsets[c]) * ratios[c]@@ -276,8 +282,7 @@ def main() -> int:if src_s < 0:info(f"warning: camera {c} has not started at reference {s:.2f}s; using camera 0 for that range")c, src_s, src_e = 0, s, e- # each camera converted by its own colour tags to the BT.709 the cut is tagged with- parts.append(f"[{c}:v]trim=start={src_s:.4f}:end={src_e:.4f},setpts=PTS-STARTPTS,{matrix_chain(metas[c], metas[0])}{geo}[v{i}]")+ parts.append(f"[{c}:v]trim=start={src_s:.4f}:end={src_e:.4f},setpts=PTS-STARTPTS,{convert[c]}{geo}[v{i}]")labels.append(f"[v{i}]")project_clips.append({"src": args.inputs[c], "in": round(src_s, 3), "out": round(src_e, 3)})parts.append("".join(labels) + f"concat=n={len(filled)}:v=1:a=0[vout]")diff --git a/scripts/overlay.py b/scripts/overlay.pyindex 2951c63..591e025 100755--- a/scripts/overlay.py+++ b/scripts/overlay.py@@ -25,7 +25,7 @@ import sysfrom typing import List, Optionalfrom _platforms import PLATFORMS, PLATFORM_CHOICES, safe_margins_px, resolve as resolve_platform-from _common import STATE, script_font_for_text, drawtext_text_opts, needs_shaping, LANGUAGE_NAMES, has_emoji, detect_script, emoji_clusters, load_brand, label_chain, matrix_chain, rgb_chain_to_bt709, video_args, add_common, apply_common, default_font_file, emit, aac_args, cfr_args, default_output, die, escape_drawtext, escape_filter_path, ffmpeg_base, info, parse_time, probe, run, run_keeping_subtitles, validate_color, x264_args, X264_PRESETS, time_arg, fmt_secs+from _common import STATE, script_font_for_text, drawtext_text_opts, needs_shaping, LANGUAGE_NAMES, has_emoji, detect_script, emoji_clusters, load_brand, drawn_chain, label_chain, matrix_chain, rgb_chain_to_bt709, video_args, add_common, apply_common, default_font_file, emit, aac_args, cfr_args, default_output, die, escape_drawtext, escape_filter_path, ffmpeg_base, info, parse_time, probe, run, run_keeping_subtitles, validate_color, x264_args, X264_PRESETS, time_arg, fmt_secs# Per-edge margins: a platform's UI does not cover the same fraction of every edge (TikTok's# like column is 14 % of the width, its description block 22 % of the height), so a position@@ -304,9 +304,13 @@ def main() -> int:if enable:ov += f":enable='{enable}'"cmd = ffmpeg_base() + ["-i", args.input, "-i", args.video]- # both pictures converted by their own colour tags to the BT.709 the output is tagged with- base = matrix_chain(meta, meta)- fc = (f"[1:v]{matrix_chain(pip_meta, meta)}{','.join(chain)}[ov];"+ # both pictures converted by their own colour tags to the BT.709 the output is tagged with;+ # beside a converted one an untagged one gets its BT.709 label, tags only, so 7.1+'s+ # overlay does not convert one to the other's "unknown" (BT.601) matrix+ base, pip = matrix_chain(meta, meta), matrix_chain(pip_meta, meta)+ if base or pip:+ base, pip = base or label_chain(meta, meta), pip or label_chain(pip_meta, meta)+ fc = (f"[1:v]{pip}{','.join(chain)}[ov];"+ (f"[0:v]{base}null[base];[base]" if base else "[0:v]") + f"[ov]{ov}[out]")cmd += ["-filter_complex", fc, "-map", "[out]", "-map", f"0:a:{args.audio_stream}?"]if meta.get("duration"):@@ -333,7 +337,9 @@ def main() -> int:opts += ["box=1", f"boxcolor={args.box_color}", "boxborderw=12"]if enable:opts.append(f"enable='{enable}'")- cmd += ["-vf", "drawtext=" + ":".join(opts), "-map", "0:v:0"]+ # a font/border/box colour with chroma lands in the BT.709 the output is tagged with+ colours = [args.font_color] + ([args.border_color] if args.border else []) + ([args.box_color] if args.box else [])+ cmd += ["-vf", drawn_chain("drawtext=" + ":".join(opts), meta, colours), "-map", "0:v:0"]if meta.get("audio"):cmd += ["-map", f"0:a:{args.audio_stream}"]diff --git a/scripts/pad.py b/scripts/pad.pyindex 26368d9..5fd2dda 100644--- a/scripts/pad.py+++ b/scripts/pad.py@@ -16,7 +16,7 @@ Examples:import argparseimport sys-from _common import add_common, apply_common, aac_args, cfr_args, default_output, die, emit, ffmpeg_base, info, probe, run_keeping_subtitles, validate_color, video_args, X264_PRESETS, time_arg, fmt_secs, run+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, time_arg, fmt_secs, rundef main() -> int:@@ -46,7 +46,10 @@ def main() -> int:has_audio = bool(meta.get("audio"))output = args.output or default_output(args.input, "pad")- vf = f"tpad=start_duration={args.start:.3f}:stop_duration={args.end:.3f}:color={args.color}"+ tpad = f"tpad=start_duration={args.start:.3f}:stop_duration={args.end:.3f}:color="+ # a --color with chroma lands in the BT.709 the output is tagged with (tpad's own YUV is BT.601+ # before FFmpeg 7.1, and for an untagged picture after it); black is the same under every matrix+ vf = drawn_chain(tpad + args.color, meta, [args.color], tpad + "black")cmd = ffmpeg_base() + ["-i", args.input, "-vf", vf, "-map", "0:v:0"]if has_audio:af = f"adelay={int(args.start * 1000)}:all=1,apad=pad_dur={args.end:.3f}"diff --git a/scripts/straighten.py b/scripts/straighten.pyindex 7da6ec8..851cb16 100644--- a/scripts/straighten.py+++ b/scripts/straighten.py@@ -22,7 +22,7 @@ import argparseimport mathimport sys-from _common import add_common, apply_common, aac_args, cfr_args, default_output, die, emit, ffmpeg_base, info, probe, run_keeping_subtitles, validate_color, video_args, X264_PRESETS, fmt_secs+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_secsdef main() -> int:@@ -63,8 +63,10 @@ def main() -> int:radians = math.radians(args.degrees)if args.fit == "pad":- rotate = (f"rotate={radians:.8f}:fillcolor={args.fill_color}:"- f"ow=trunc(rotw({radians:.8f})/2)*2:oh=trunc(roth({radians:.8f})/2)*2")+ geometry = f"ow=trunc(rotw({radians:.8f})/2)*2:oh=trunc(roth({radians:.8f})/2)*2"+ # a --fill-color with chroma lands in the BT.709 the output is tagged with (drawn_chain)+ rotate = drawn_chain(f"rotate={radians:.8f}:fillcolor={args.fill_color}:{geometry}", meta, [args.fill_color],+ f"rotate={radians:.8f}:fillcolor=black:{geometry}")else:# Pre-scale the frame up by a safe, conservative factor (|cos|+|sin|, the exact growth# factor for a square, over-generous for a rectangle) so the rotated content fullydiff --git a/scripts/waveform.py b/scripts/waveform.pyindex 8e88c63..5628c70 100644--- a/scripts/waveform.py+++ b/scripts/waveform.py@@ -35,7 +35,7 @@ import osimport sysfrom _platforms import PLATFORMS, PLATFORM_CHOICES, resolve as resolve_platform-from _common import (add_common, apply_common, aac_args, default_output, die, emit, ffmpeg_base,+from _common import (add_common, apply_common, aac_args, default_output, die, draws_chroma, emit, ffmpeg_base,info, load_brand, pad_filters, probe, rgb_chain_to_bt709, run, STATE, validate_color, video_args, X264_PRESETS,fmt_secs, child_args, run_tool, measured_level_dbfs)from _common.emit import absorb_stage_hw@@ -193,7 +193,14 @@ def main() -> int:# showwaves/showspectrum paint the visualization on a transparent-black canvas; composite# it over an explicit solid background instead of assuming that canvas already matches# --background.- vf = f"color=c={args.background}:s={args.width}x{args.height}:r={args.fps:g}[bg];[0:a:{args.audio_stream}]{vf}[vis];[bg][vis]overlay=format=auto"+ bg, vis = f"color=c={args.background}:s={args.width}x{args.height}:r={args.fps:g}", vf+ if draws_chroma([args.background] + (args.color.split("|") if args.style == "waveform" else []), None):+ # the background and the RGBA waves are converted to the BT.709 the output is tagged+ # with (the encoder line's own RGB-to-YUV step used BT.601); the spectrum is YUV of its+ # own and only gets the label the background carries, so 7.1+'s overlay converts neither+ bg += ",format=rgb24" + rgb_chain_to_bt709(None, "yuv420p")+ vis += rgb_chain_to_bt709(None, "yuva420p") if args.style == "waveform" else ",setparams=colorspace=bt709"+ vf = f"{bg}[bg];[0:a:{args.audio_stream}]{vis}[vis];[bg][vis]overlay=format=auto"render = outputif args.title or args.srt or args.text:diff --git a/tests/fixtures/caption_1_16_1_fitsize_off.ass b/tests/fixtures/caption_1_16_1_fitsize_off.assindex 1891b27..48c5691 100644--- a/tests/fixtures/caption_1_16_1_fitsize_off.ass+++ b/tests/fixtures/caption_1_16_1_fitsize_off.ass@@ -4,6 +4,7 @@ PlayResX: 1080PlayResY: 1920WrapStyle: 0ScaledBorderAndShadow: yes+YCbCr Matrix: None[V4+ Styles]Format: Name, Fontname, Fontsize, PrimaryColour, SecondaryColour, OutlineColour, BackColour, Bold, Italic, Underline, StrikeOut, ScaleX, ScaleY, Spacing, Angle, BorderStyle, Outline, Shadow, Alignment, MarginL, MarginR, MarginV, Encodingdiff --git a/tests/test_audio.py b/tests/test_audio.pyindex ed11e29..ef8abb7 100644--- a/tests/test_audio.py+++ b/tests/test_audio.py@@ -471,7 +471,17 @@ class AudiogramTests(MediaFixtures):expected.extend(bt709_tag_args())else:expected.append(part.format(ffmpeg=plan[0], input=str(self.src), output=str(out)))+ # 2.10.3: the default lime waves have chroma, so they and the plate are made in RGB and+ # converted to the BT.709 the output is tagged with (drawn colours); 1.15.1's graph is+ # otherwise unchanged -- and byte for byte unchanged for grey waves (below)+ graph = expected.index("-filter_complex") + 1+ pinned = expected[graph]+ expected[graph] = (pinned.replace("r=25[bg]", "r=25,format=rgb24,scale=out_color_matrix=bt709:out_range=tv,format=yuv420p[bg]")+ .replace("colors=lime[vis]", "colors=lime,scale=out_color_matrix=bt709:out_range=tv,format=yuva420p[vis]"))self.assertEqual(plan, expected)+ grey = self._plan(self.src, "--width", "640", "--height", "360", "--color", "white", "-o", out)+ expected[graph] = pinned.replace("colors=lime", "colors=white")+ self.assertEqual(grey, expected)def test_audiogram_graph_has_one_overlay_per_layer(self):plate = self._plate(640, 360)diff --git a/tests/test_contract.py b/tests/test_contract.pyindex 8f98fbe..4458a14 100644--- a/tests/test_contract.py+++ b/tests/test_contract.py@@ -1866,10 +1866,12 @@ class ContractTests(unittest.TestCase):commands = " ".join(json.loads(proc.stdout)["commands"])self.assertEqual(commands.count(conv), expected, commands)if src == self.src:- # the source is not converted (overlay --image converts its logo, and labels- # the untagged main BT.709 for the logo to meet: test_still_and_rgb_tools_plan...)+ # the source is not converted (overlay --image converts its logo, and the title+ # its brand-yellow accent line, and each labels the untagged main BT.709 for it+ # to meet: test_still_and_rgb_tools_plan..., test_drawn_colours_plan...)self.assertNotIn("colorspace=ispace", commands)- self.assertEqual(commands.count("setparams=colorspace=bt709"), 1 if "--image" in args else 0, commands)+ self.assertEqual(commands.count("setparams=colorspace=bt709"),+ 1 if "--image" in args or name == "graphics" else 0, commands)# the other way round: the BT.601 file as the second input is converted in its own chainfor name, args in (("join", [self.camb, s601]), ("broll", [self.camb, "--insert", s601, "--at", "1", "--duration", "1"]),("overlay", [self.camb, "--video", s601])):@@ -1926,6 +1928,101 @@ class ContractTests(unittest.TestCase):for s, count in want.items():self.assertEqual(commands.count(s), count, f"{s!r} in {commands}")+ def test_drawn_colours_are_drawn_in_bt709(self):+ """A colour the skill draws (pad, tpad, rotate fill, drawtext, libass) was turned into YUV by+ the drawing filter itself: with BT.601 on 5.1-7.0 whatever the picture is tagged, with the+ link's colourspace from 7.1 on ("unknown" = BT.601). drawn_chain() labels an untagged picture+ BT.709 on 7.1+ (tags only) and draws in RGB on an older build, keeping the untouched pixels+ (rgb_island). A grey -- the same 8-bit YUV under either matrix -- a prores line and an HDR+ line change nothing. Pinned with the version forced."""+ n = _common.is_neutral_color+ for c in ("black", "white", "gray", "Silver", "0x808080", "#FFFFFF", "000000", "[email protected]", "0x101418"):+ self.assertTrue(n(c), c)+ for c in ("red", "lime", "random", "0xFF0000", "#2878C8", "FFD200", "[email protected]", "0x1E6F8E", "nonsense"):+ self.assertFalse(n(c), c)++ def meta(**v):+ return {"file": "src", "video": dict({"bt2020_or_hdr": False, "pix_fmt": "yuv420p"}, **v)}+ pad, black = "pad=8:8:0:0:color=red", "pad=8:8:0:0:color=black"+ un, t709, rgb = meta(), meta(color_space="bt709"), meta(pix_fmt="rgb24", color_space="gbr")+ full, hdr = meta(pix_fmt="yuvj420p", color_range="pc"), meta(pix_fmt="yuv420p10le", bt2020_or_hdr=True)+ dc = _common.drawn_chain+ saved = (_common._FFMPEG_VERSION, _common.STATE.codec)+ try:+ for version in ((7, 1), (8, 1)):+ _common._FFMPEG_VERSION = version+ self.assertEqual(dc(pad, un, ["red"], black), "setparams=colorspace=bt709:range=tv," + pad, version)+ self.assertEqual(dc(pad, full, ["red"], black), "setparams=colorspace=bt709," + pad, version)+ for m in (t709, rgb, meta(color_space="smpte170m")): # a tagged or converted link is followed already+ self.assertEqual(dc(pad, m, ["red"], black), pad, (version, m))+ self.assertEqual(dc(pad, un, ["red"], black, labelled=True), pad)+ for version in ((5, 1), (6, 1), (7, 0), (0, 0)):+ _common._FFMPEG_VERSION = version+ for m, fmt, rng in ((un, "yuv420p", "tv"), (t709, "yuv420p", "tv"), (rgb, "yuv444p", "tv"), (full, "yuvj420p", "pc")):+ island = dc(pad, m, ["red"], black)+ self.assertEqual(island, _common.rgb_island(pad, black, fmt, rng == "pc", "d"))+ self.assertTrue(island.startswith(f"split=3[_dp][_dc][_dn];[_dp]format={fmt},{black}[_dp1];"), island)+ self.assertIn(f"[_dc]scale=in_color_matrix=bt709:in_range={rng},format=rgb24,{pad},"+ f"scale=out_color_matrix=bt709:out_range={rng},format={fmt},split[_dc1][_dc2];", island)+ self.assertIn(f"[_dn]scale=in_color_matrix=bt709:in_range={rng},format=rgb24,{black},", island)+ self.assertTrue(island.endswith("blend=all_mode=difference,lut=c0='255*gt(val,0)':c1='255*gt(val,0)':"+ "c2='255*gt(val,0)'[_dm];[_dp1][_dc2][_dm]maskedmerge"), island)+ self.assertEqual(dc("drawtext=x=1", un, ["red"], tag="t7"),+ _common.rgb_island("drawtext=x=1", "null", "yuv420p", False, "t7"))+ for version in ((6, 1), (8, 1)):+ _common._FFMPEG_VERSION = version+ self.assertEqual(dc(pad, un, ["black", "[email protected]", "0x808080", None], black), pad) # greys+ self.assertEqual(dc(pad, hdr, ["red"], black), pad) # an HDR line keeps its own tags+ self.assertEqual(dc(pad, un, ["red"], black, line_meta=hdr), pad)+ _common.STATE.codec = "prores" # prores keeps the source's+ self.assertEqual(dc(pad, un, ["red"], black), pad)+ _common.STATE.codec = None+ finally:+ _common._FFMPEG_VERSION, _common.STATE.codec = saved++ # every tool that draws a colour, planned on this build: a colour with chroma takes the label+ # (7.1+) or the RGB island (older), once per picture it is drawn on; the default greys plan+ # neither. join/broll/grid label every untagged input on any build (the label changes nothing+ # before 7.1). background.py and waveform.py make their colours in RGB and convert them.+ modern = _common.ffmpeg_version() >= (7, 1)+ conv = "scale=out_color_matrix=bt709:out_range=tv"+ cases = [ # (tool, args, labels on 7.1+, (islands, labels) before 7.1, conversions)+ ("fit", [self.src, "--width", "320", "--height", "320", "--pad-color", "red"], 1, (1, 0), 0),+ ("fit", [self.src, "--width", "320", "--height", "320"], 0, (0, 0), 0),+ ("export", [self.src, "--preset", "reels", "--pad-color", "0x2878C8"], 1, (1, 0), 0),+ ("export", [self.src, "--preset", "gif", "--pad-color", "red"], 0, (0, 0), 0), # palettegen reads BT.601 back+ ("pad", [self.src, "--start", "1", "--color", "red"], 1, (1, 0), 0),+ ("pad", [self.src, "--start", "1", "--color", "0x101010"], 0, (0, 0), 0),+ ("straighten", [self.src, "--degrees", "2", "--fit", "pad", "--fill-color", "0x00FF00"], 1, (1, 0), 0),+ ("overlay", [self.src, "--text", "hi", "--font-color", "yellow"], 1, (1, 0), 0),+ ("overlay", [self.src, "--text", "hi", "--box", "--box-color", "[email protected]"], 1, (1, 0), 0),+ ("overlay", [self.src, "--text", "hi", "--box"], 0, (0, 0), 0),+ ("caption", [self.src, "--text", self.cues, "--color", "FF0000"], 1, (1, 0), 0),+ ("caption", [self.src, "--text", self.cues], 0, (0, 0), 0),+ ("join", [self.src, self.camb, "--pad-color", "red"], 2, (2, 2), 0),+ ("join", [self.src, self.camb], 0, (0, 0), 0),+ ("broll", [self.src, "--insert", self.camb, "--at", "1", "--duration", "1", "--pad-color", "red"], 2, (1, 2), 0),+ ("grid", [self.src, self.camb, "--cols", "2", "--rows", "1", "--background", "red", "--gap", "8"], 2, (2, 2), 0),+ ("grid", [self.src, self.camb, "--cols", "2", "--rows", "1", "--gap", "8"], 0, (0, 0), 0),+ ("graphics", [self.src, "--template", "chapter", "--title", "T"], 1, (1, 0), 0), # the brand-yellow chip+ ("graphics", [self.src, "--template", "meme", "--top", "T"], 0, (0, 0), 0),+ ("background", ["--duration", "1", "--width", "64", "--height", "64", "--color", "red"], 0, (0, 0), 1),+ ("background", ["--duration", "1", "--width", "64", "--height", "64"], 0, (0, 0), 0),+ ("waveform", [self.src], 0, (0, 0), 2), # lime waves: the background and the waves+ ("waveform", [self.src, "--color", "white"], 0, (0, 0), 0),+ ]+ for k, (name, args, labels, (islands, old_labels), convs) in enumerate(cases):+ with self.subTest(tool=name, args=[Path(str(a)).name for a in args]):+ ext = ".gif" if "gif" in args else ".mp4"+ proc = tool(name, *args, "-o", self.out(f"drawn_{k}{ext}"), "--dry-run", "--json", check=False)+ self.assertEqual(proc.returncode, 0, proc.stderr[-600:])+ commands = " ".join(json.loads(proc.stdout)["commands"])+ if modern:+ islands, old_labels = 0, labels+ self.assertEqual(commands.count("maskedmerge"), islands, commands)+ self.assertEqual(commands.count("setparams=colorspace=bt709"), old_labels, commands)+ self.assertEqual(commands.count(conv) - 2 * islands, convs, commands) # an island converts twice+def test_json_success_and_failure_shapes_and_exit_codes(self):doc = json.loads(tool("cut", self.wav, "--start", "1", "--end", "3", "-o", self.out("shape.wav"), "--json").stdout)self.assertEqual(doc["status"], "completed")diff --git a/tests/test_picture.py b/tests/test_picture.pyindex dba62ce..04ead9d 100644--- a/tests/test_picture.py+++ b/tests/test_picture.py@@ -4,6 +4,7 @@python3 tests/test_picture.py # this group alonepython3 tests/test_all.py # every group"""+import collectionsimport contextlibimport ioimport json@@ -2037,7 +2038,8 @@ class PictureTests(MediaFixtures):the defect this release fixes -- MarginL=MarginR=MarginV=420 on a 1080-wide frame, a240 px text column -- so it pinned wrong bytes. The re-pin keeps --fit-size off meaningexactly what it meant (the size is never shrunk); only the side margins, and the wrap- budget that now agrees with them, differ."""+ budget that now agrees with them, differ. 2.10.3 added the header line `YCbCr Matrix: None`+ (the colours are RGB, drawn in the video's own matrix), and nothing else."""vert, cues = self._vertical(), self._fit_cues()ass = OUT / "cap_fit_off.ass"script("caption.py", vert, "--text", cues, "--platform", "tiktok", "--max-lines", "2",@@ -2352,13 +2354,17 @@ class RgbAndStillConversionTests(unittest.TestCase):loop = (*q, "-loop", "1", "-framerate", "25", "-i", d / "bars.png", "-t", "1")# the bars as a base to composite on, 768x256 with audio, BT.709-tagged and untagged (encoded# with BT.709, what players take untagged video as): a shifted main picture shows as well+ # (an untagged clip's frames are relabelled unknown: from 7.1 the scale's own BT.709 output+ # would otherwise reach x264's VUI, and the "untagged" clip came out tagged bt709)+ untag = {"untagged": ",setparams=colorspace=unknown"}for name, tag in (("base709", "bt709"), ("baseun", "untagged")):- sh(*loop, "-f", "lavfi", "-i", "sine=d=1", "-vf", "scale=768:256:flags=neighbor:out_color_matrix=bt709:out_range=tv,format=yuv420p",+ sh(*loop, "-f", "lavfi", "-i", "sine=d=1", "-vf",+ "scale=768:256:flags=neighbor:out_color_matrix=bt709:out_range=tv,format=yuv420p" + untag.get(tag, ""),*x264, *tags[tag], "-c:a", "aac", "-shortest", d / f"{name}.mp4")# the bars as video: an RGB qtrle clip, and BT.601- / BT.709-tagged and untagged clips encoded with their own matrixsh(*loop, "-vf", "scale=384:128:flags=neighbor", "-c:v", "qtrle", "-pix_fmt", "rgb24", d / "qtrle.mov")for name, m, tag in (("v601", "bt601", "smpte170m"), ("v709", "bt709", "bt709"), ("vun", "bt709", "untagged")):- sh(*loop, "-vf", f"scale=384:128:flags=neighbor:out_color_matrix={m}:out_range=tv,format=yuv420p",+ sh(*loop, "-vf", f"scale=384:128:flags=neighbor:out_color_matrix={m}:out_range=tv,format=yuv420p" + untag.get(tag, ""),*x264, *tags[tag], d / f"{name}.mp4")cls.jpeg = cls._patches(d / "bars.jpg", cls._bars(0, 0), t=0) # what the JPEG holds, decoded as JPEG@@ -2473,6 +2479,309 @@ class RgbAndStillConversionTests(unittest.TestCase):with self.subTest(order=order, cell=name):self._assert_colours(out, self._bars(384 * c, 0, 2), self.COLOURS)+ def test_mixing_tools_label_an_untagged_clip_beside_a_converted_one(self):+ """#332 converted a BT.601 clip in its own chain in join, broll, multicam and overlay --video,+ but left an untagged clip beside it untagged: FFmpeg 7.1+ negotiates one colourspace for+ concat/overlay and converts the rest, taking "unknown" as BT.601, so one of the two pictures+ shifted (7.08 mean / 25.0 max with the BT.601 clip first, 9.12 / 40.1 with it second, on+ 7.1.1 and 8.1.3; 5.1.1 and 6.1 were already right). The untagged clip now gets its BT.709+ label, tags only, as grid.py's cells did in #333. Every order, every picture."""+ d = self.dir+ clips = {}+ for name, vf, tag in (("un", "out_color_matrix=bt709:out_range=tv,format=yuv420p,setparams=colorspace=unknown", ()),+ ("601", "out_color_matrix=bt601:out_range=tv,format=yuv420p",+ ("-x264-params", "colorprim=smpte170m:transfer=smpte170m:colormatrix=smpte170m"))):+ clips[name] = d / f"mix_{name}.mp4"+ if not clips[name].exists(): # 2 s of the bars, the same noise under both for multicam's alignment+ sh("ffmpeg", "-y", "-hide_banner", "-loglevel", "error", "-loop", "1", "-framerate", "25", "-i", d / "bars.png",+ "-f", "lavfi", "-i", "anoisesrc=d=2:seed=7:amplitude=0.3", "-t", "2", "-vf",+ f"scale=384:128:flags=neighbor:{vf}", "-c:v", "libx264", "-preset", "ultrafast", "-crf", "1",+ "-pix_fmt", "yuv420p", *tag, "-c:a", "aac", "-shortest", clips[name])+ big = d / "baseun.mp4" # 768x256 untagged bars: the main picture for overlay --video+ for clip in (clips["un"], big): # really untagged on 7.1+ too+ self.assertIn(probe(str(clip))["video"].get("color_space"), (None, "unknown", "unspecified"), clip.name)+ pip = [(384 + 12 + k * 128, 128 + 12, 384 + (k + 1) * 128 - 12, 256 - 12) for k in range(3)]+ main = [(k * 256 + 20, 20, k * 256 + 236, 100) for k in range(3)]+ for a, b in (("un", "601"), ("601", "un")):+ out = d / f"mix_join_{a}_{b}.mp4"+ script("join.py", clips[a], clips[b], "--transition", "none", "--fast", "-o", out)+ for t, name in ((0.5, a), (2.5, b)):+ with self.subTest(tool="join.py", order=(a, b), clip=name):+ self._assert_colours(out, self._bars(0, 0, 2), self.COLOURS, t)+ out = d / f"mix_broll_{a}_{b}.mp4"+ script("broll.py", clips[a], "--insert", clips[b], "--at", "0.5", "--duration", "1", "--fast", "-o", out)+ for t, name in ((0.2, a), (1.0, b)):+ with self.subTest(tool="broll.py", order=(a, b), clip=name):+ self._assert_colours(out, self._bars(0, 0, 2), self.COLOURS, t)+ out = d / f"mix_multicam_{a}_{b}.mp4"+ script("multicam.py", clips[a], clips[b], "--switch", "0-1:0,1-2:1", "--fast", "-o", out)+ for t, name in ((0.5, a), (1.5, b)):+ with self.subTest(tool="multicam.py", order=(a, b), clip=name):+ self._assert_colours(out, self._bars(0, 0, 2), self.COLOURS, t)+ for main_clip, pip_clip in ((big, clips["601"]), (d / "base601big.mp4", clips["un"])):+ if not main_clip.exists():+ sh("ffmpeg", "-y", "-hide_banner", "-loglevel", "error", "-loop", "1", "-framerate", "25", "-i", d / "bars.png",+ "-f", "lavfi", "-i", "sine=d=1", "-t", "1", "-vf",+ "scale=768:256:flags=neighbor:out_color_matrix=bt601:out_range=tv,format=yuv420p", "-c:v", "libx264",+ "-preset", "ultrafast", "-crf", "1", "-pix_fmt", "yuv420p", "-x264-params",+ "colorprim=smpte170m:transfer=smpte170m:colormatrix=smpte170m", "-c:a", "aac", "-shortest", main_clip)+ out = d / f"mix_overlay_{main_clip.stem}.mp4"+ script("overlay.py", main_clip, "--video", pip_clip, "--position", "bottom-right", "--margin", "0", "--fast", "-o", out)+ for part, rects in (("main", main), ("pip", pip)):+ with self.subTest(tool="overlay.py --video", main=main_clip.name, part=part):+ self._assert_colours(out, rects, self.COLOURS)+++class DrawnColourTests(unittest.TestCase):+ """A colour the skill draws -- a pad, a background, a box, text, a caption, a waveform -- from a+ hex or named value. FFmpeg's drawing filters turn it into YUV with a matrix of their own: BT.601+ always up to 7.0 (and drawbox on every version), the link's colourspace from 7.1 on, "unknown"+ read as BT.601; the `color` source and an RGBA layer went through the encoder line's own BT.601+ conversion. So through 2.10.2 the colour landed as BT.601 YUV under the output's BT.709 tag on+ 5.1.1, 6.1, 7.1.1 and 8.1.3: pure red decoded as (255,24,0), pure green as (0,216,0), mean+ ΔRGB 8.8 / max 38.9 (7.1+ was right only for a BT.709-tagged or converted source's pad).++ Each output is decoded with the matrix its own tags name, and the drawn region is compared with+ the true colour (a translucent element: blended with the grey under it). Fixed, every one is+ within ~1.6. A picture that only passes through keeps its pixels (the last test)."""++ RED, GREEN, BLUE = (255, 0, 0), (0, 255, 0), (40, 120, 200)+ GREY = (128, 128, 128)++ @classmethod+ def setUpClass(cls):+ encoders = subprocess.run(["ffmpeg", "-hide_banner", "-encoders"], stdout=subprocess.PIPE, text=True).stdout+ if "libx264" not in encoders:+ raise unittest.SkipTest("this ffmpeg build has no libx264")+ cls.filters = subprocess.run(["ffmpeg", "-hide_banner", "-filters"], stdout=subprocess.PIPE, text=True).stdout+ cls.dir = OUT / "drawn"+ cls.dir.mkdir(parents=True, exist_ok=True)+ d = cls.dir+ q = ("ffmpeg", "-y", "-hide_banner", "-loglevel", "error")+ x264 = ("-c:v", "libx264", "-preset", "ultrafast", "-crf", "1", "-pix_fmt", "yuv420p")+ bt709 = ("-x264-params", "colorprim=bt709:transfer=bt709:colormatrix=bt709")+ grey = lambda size, dur: ("-f", "lavfi", "-i", f"color=c=0x808080:s={size}:r=25:d={dur}") # noqa: E731+ sine = lambda dur: ("-f", "lavfi", "-i", f"sine=d={dur}") # noqa: E731+ for name, args in (("grey", (*grey("640x360", 2), *sine(2), *x264, "-c:a", "aac", "-shortest")),+ ("grey709", (*grey("640x360", 2), *sine(2), *x264, *bt709, "-c:a", "aac", "-shortest")),+ ("silent", (*grey("640x360", 2), *x264)), # pad.py: no audio track to delay+ ("tall", (*grey("360x640", 2), *sine(2), *x264, "-c:a", "aac", "-shortest")),+ ("big", (*grey("1280x720", 3), *sine(3), *x264, "-c:a", "aac", "-shortest"))):+ if not (d / f"{name}.mp4").exists():+ sh(*q, *args, d / f"{name}.mp4")+ if not (d / "loud.wav").exists():+ sh(*q, "-f", "lavfi", "-i", "sine=f=3:d=2:sample_rate=48000,volume=4", d / "loud.wav")+ if not (d / "subs.srt").exists():+ (d / "subs.srt").write_text("1\n00:00:00,000 --> 00:00:05,000\nHHH\n", encoding="utf-8")++ def _has(self, name):+ return re.search(rf"^\s*\S+\s+{name}\s", self.filters, re.M)++ @staticmethod+ def _blend(c, a):+ return tuple(a * x + (1 - a) * 128 for x in c)++ def _check(self, out, got, want):+ v = probe(str(out))["video"]+ self.assertEqual(v["color_space"], "bt709", f"{out.name}: the output is not tagged BT.709")+ diffs = [abs(a - b) for g, w in zip(got, want) for a, b in zip(g, w)]+ # measured fixed: mean <= 0.6, max <= 1.6 (5.1.1, 6.1, 7.1.1, 8.1.3); through 2.10.2 the+ # colour was BT.601 under that tag: mean >= 6.6, max >= 24 for every case below+ shown = [tuple(round(c) for c in g) for g in got]+ self.assertLess(sum(diffs) / len(diffs), 3.0, f"{out.name}: {shown} != {want}")+ self.assertLess(max(diffs), 8.0, f"{out.name}: {shown} != {want}")++ def _patches(self, out, rects, t=0.5):+ return RgbAndStillConversionTests._patches(out, rects, t)++ def _mode(self, out, rect, t=0.5, ignore=(128, 128, 128)):+ """The most common colour of the flat 2x2 blocks in `rect` that are not `ignore`: the drawn+ text's or box's own colour, without knowing where the glyphs are."""+ s = json.loads(subprocess.run(["ffprobe", "-v", "error", "-select_streams", "v:0", "-show_entries",+ "stream=width,height,color_range", "-of", "json", str(out)],+ stdout=subprocess.PIPE, text=True, check=True).stdout)["streams"][0]+ w, h = s["width"], s["height"]+ raw = subprocess.run(["ffmpeg", "-v", "error", "-ss", str(t), "-i", str(out), "-frames:v", "1", "-f", "rawvideo",+ "-pix_fmt", "yuv420p", "-"], stdout=subprocess.PIPE, check=True).stdout+ cw, ch = w // 2, h // 2+ bins = {}+ x0, y0, x1, y1 = rect+ for by in range(y0 // 2, y1 // 2):+ for bx in range(x0 // 2, x1 // 2):+ ys = [raw[(2 * by + dy) * w + 2 * bx + dx] for dy in (0, 1) for dx in (0, 1)]+ if max(ys) - min(ys) > 3:+ continue # an edge+ yy = (sum(ys) / 4 - 16) * 255 / 219+ u = (raw[w * h + by * cw + bx] - 128) * 255 / 224+ v = (raw[w * h + cw * ch + by * cw + bx] - 128) * 255 / 224+ r, b = yy + 1.5748 * v, yy + 1.8556 * u+ rgb = tuple(max(0.0, min(255.0, c)) for c in (r, (yy - 0.2126 * r - 0.0722 * b) / 0.7152, b))+ if max(abs(a - c) for a, c in zip(rgb, ignore)) > 10:+ bins.setdefault(tuple(int(c) // 6 for c in rgb), []).append(rgb)+ self.assertTrue(bins, f"{out.name}: nothing drawn in {rect}")+ best = max(bins.values(), key=len)+ return tuple(sum(c[i] for c in best) / len(best) for i in range(3))++ def test_a_pad_fill_or_background_lands_in_the_bt709_its_output_is_tagged_with(self):+ """Solid fills: background.py (the `color` source and a `gradients` one), the pad of fit.py,+ export.py, join.py, broll.py and grid.py (its cells and its gaps), pad.py's tpad,+ straighten.py's rotate fill and waveform.py's background, on an untagged and a BT.709-tagged+ source. The picture beside the pad stays grey."""+ d = self.dir+ runs = [ # (tool, args, [(rect, colour)], t)+ ("background.py", ["--duration", "1", "--width", "128", "--height", "64", "--color", "0xFF0000"],+ [((16, 16, 112, 48), self.RED)], 0.5),+ ("background.py", ["--duration", "1", "--width", "128", "--height", "64", "--color", "#2878C8"],+ [((16, 16, 112, 48), self.BLUE)], 0.5),+ ("fit.py", [d / "grey.mp4", "--width", "640", "--height", "640", "--fit", "pad", "--pad-color", "0x00FF00"],+ [((100, 20, 540, 120), self.GREEN), ((100, 200, 540, 440), self.GREY)], 0.5),+ ("fit.py", [d / "grey709.mp4", "--width", "640", "--height", "640", "--fit", "pad", "--pad-color", "red"],+ [((100, 20, 540, 120), self.RED), ((100, 200, 540, 440), self.GREY)], 0.5),+ ("export.py", [d / "grey.mp4", "--preset", "reels", "--pad-color", "0x2878C8"],+ [((100, 40, 980, 400), self.BLUE)], 0.5),+ ("pad.py", [d / "silent.mp4", "--start", "1", "--color", "0xFF0000"],+ [((100, 100, 540, 260), self.RED)], 0.5),+ ("join.py", [d / "grey.mp4", d / "tall.mp4", "--transition", "none", "--pad-color", "0x00FF00"],+ [((20, 100, 200, 260), self.GREEN)], 3.0),+ ("broll.py", [d / "grey.mp4", "--insert", d / "tall.mp4", "--at", "0.5", "--duration", "1",+ "--pad-color", "0x2878C8"], [((20, 100, 200, 260), self.BLUE)], 1.0),+ ("grid.py", [d / "grey.mp4", d / "tall.mp4", "--cols", "2", "--rows", "1", "--cell-width", "320",+ "--cell-height", "180", "--gap", "16", "--background", "0xFF0000", "--label", "none"],+ [((340, 20, 440, 160), self.RED), ((322, 20, 334, 160), self.RED), ((20, 20, 300, 160), self.GREY)], 0.5),+ ("straighten.py", [d / "grey.mp4", "--degrees", "10", "--fit", "pad", "--fill-color", "0x00FF00"],+ [((2, 2, 18, 18), self.GREEN)], 0.5),+ ("waveform.py", [d / "loud.wav", "--width", "320", "--height", "180", "--background", "0x2878C8",+ "--color", "white"], [((2, 2, 30, 30), self.BLUE)], 0.5),+ ]+ for n, (tool, args, want, t) in enumerate(runs):+ with self.subTest(tool=tool, args=[Path(str(a)).name for a in args if not str(a).startswith("--")][:3]):+ out = d / f"fill_{n}_{tool[:-3]}.mp4"+ script(tool, *args, "--fast", "-o", out)+ self._check(out, self._patches(out, [r for r, _c in want], t), [c for _r, c in want])+ with self.subTest(tool="background.py --gradient"):+ out = d / "fill_gradient.mp4"+ script("background.py", "--duration", "1", "--width", "256", "--height", "64", "--gradient",+ "0xFF0000:0x00FF00", "--fast", "-o", out)+ rects = [(4, 16, 20, 48), (120, 16, 136, 48), (236, 16, 252, 48)]+ # the truth: the same gradients source rendered as RGB by this very build+ src = "gradients=size=256x64:rate=30:c0=0xFF0000:c1=0x00FF00:x0=0:y0=0:x1=256:y1=0:seed=0:speed=1e-05"+ raw = subprocess.run(["ffmpeg", "-v", "error", "-f", "lavfi", "-i", src, "-frames:v", "1", "-f", "rawvideo",+ "-pix_fmt", "rgb24", "-"], stdout=subprocess.PIPE, check=True).stdout+ want = []+ for x0, y0, x1, y1 in rects:+ px = [raw[(y * 256 + x) * 3:(y * 256 + x) * 3 + 3] for y in range(y0, y1) for x in range(x0, x1)]+ want.append(tuple(sum(p[i] for p in px) / len(px) for i in range(3)))+ self._check(out, self._patches(out, rects), want)++ def test_drawn_text_and_boxes_land_in_the_bt709_their_output_is_tagged_with(self):+ """Text and boxes: overlay.py --text (font colour, box colour), graphics.py's templates (the+ lower-third bar and accent, the title's box and accent line, the hook's band and bar, the+ progress bar and its strip, a chapter chip, a countdown digit; brand primary and background+ set to the colour), the libass route, caption.py's burn (--color) and waveform.py's waves."""+ if not self._has("drawtext"):+ self.skipTest("this ffmpeg build has no drawtext filter")+ d = self.dir+ cases = [] # (label, out, [measured colours], [true colours])+ out = d / "text_box.mp4"+ script("overlay.py", d / "grey.mp4", "--text", ".", "--box", "--box-color", "0xFF0000", "--font-size", "20",+ "--position", "top-left", "--fast", "-o", out)+ cases.append(("overlay.py --box-color", out, [self._mode(out, (0, 0, 320, 180))], [self.RED]))+ out = d / "text_font.mp4"+ script("overlay.py", d / "grey.mp4", "--text", "HHH", "--font-color", "0x00FF00", "--font-size", "160",+ "--border", "0", "--position", "center", "--fast", "-o", out)+ cases.append(("overlay.py --font-color", out, [self._mode(out, (0, 0, 640, 360))], [self.GREEN]))+ for tpl, colour, extra in (("lower-third", self.BLUE, ["--name", "N"]), ("title", self.RED, ["--title", "T"]),+ ("hook", self.GREEN, ["--title", "T", "--duration", "2"]), ("progress", self.RED, []),+ ("chapter", self.BLUE, ["--title", "CHAPTER"]), ("countdown", self.GREEN, ["--from", "1"])):+ hx = "%02X%02X%02X" % colour+ brand = d / f"brand_{hx}.json"+ brand.write_text(json.dumps({"colors": {"primary": hx, "background": hx, "text": "FFFFFF"}}), encoding="utf-8")+ out = d / f"gfx_{tpl}.mp4"+ doc = json.loads(script("graphics.py", d / "big.mp4", "--template", tpl, "--brand", brand, *extra, "--start", "0",+ "--end", "3", "--fast", "--json", "-o", out).stdout)+ if tpl == "lower-third":+ m = re.search(r"s=(\d+)x(\d+).*?s=(\d+)x\2.*?,(\d+)\)\)\S*?:y=(\d+)", " ".join(doc["commands"]))+ _bw, bh, aw, x, y = (int(g) for g in m.groups())+ got = self._patches(out, [(x + aw + 2, y + 4, x + aw + 14, y + bh - 4), (x + 2, y + 4, x + aw - 2, y + bh - 4)], 1.5)+ want = [self._blend(colour, 0.85), colour]+ elif tpl == "title": # a full-frame box at 0.55, the accent line under the title+ got = self._patches(out, [(20, 20, 200, 200)], 1.5) + [self._mode(out, (500, 398, 780, 404), 1.5,+ ignore=self._blend(colour, 0.55))]+ want = [self._blend(colour, 0.55), colour]+ elif tpl == "hook": # the band at 0.78, the bar along the top+ got = self._patches(out, [(10, 280, 100, 300)], 0.5) + self._patches(out, [(10, 0, 200, 6)], 0.05)+ want = [self._blend(colour, 0.78), colour]+ elif tpl == "progress": # the bar (overlay puts it on even rows: 714-718), and the strip at 0.5 ahead of it+ got = self._patches(out, [(10, 714, 200, 718)], 2.5) + self._patches(out, [(1180, 716, 1270, 720)], 0.5)+ want = [colour, self._blend(colour, 0.5)]+ elif tpl == "chapter": # drawtext's box at 0.9+ got, want = [self._mode(out, (0, 360, 640, 720), 1.5)], [self._blend(colour, 0.9)]+ else: # the digit at 0.9 opacity at 0.5 s, over its own 0.9 border (the same colour): 0.99+ got, want = [self._mode(out, (320, 0, 960, 720), 0.5)], [self._blend(colour, 0.99)]+ cases.append((f"graphics.py {tpl}", out, got, want))+ if self._has("ass"):+ out = d / "gfx_ass.mp4"+ brand = d / "brand_2878C8.json"+ brand.write_text(json.dumps({"colors": {"primary": "2878C8", "background": "2878C8", "text": "FFFFFF"}}), encoding="utf-8")+ script("graphics.py", d / "big.mp4", "--template", "chapter", "--brand", brand, "--title", "CHAPTER",+ "--text-render", "ass", "--start", "0", "--end", "3", "--fast", "-o", out)+ # libass's chip: an opaque box in the primary (BorderStyle 3)+ cases.append(("graphics.py --text-render ass", out, [self._mode(out, (0, 360, 640, 720), 1.5)], [self.BLUE]))+ out = d / "caption.mp4"+ script("caption.py", d / "grey.mp4", "--srt", d / "subs.srt", "--color", "FF0000", "--size", "120",+ "--outline", "0", "--shadow", "0", "--animate", "none", "--position", "center", "--fast", "-o", out)+ cases.append(("caption.py --color", out, [self._mode(out, (0, 0, 640, 360), 1.0)], [self.RED]))+ out = d / "waves.mp4"+ doc = json.loads(script("waveform.py", d / "loud.wav", "--width", "320", "--height", "180", "--color", "0x00FF00",+ "--waveform-mode", "cline", "--quality", "0", "--fast", "--json", "-o", out).stdout) # lossless+ # the truth: showwaves draws its colour scaled by its own alpha (0x00FC00 at 252 on this frame,+ # not 0x00FF00), so the same waves are composited over black in RGB by this build+ waves = re.search(r"(showwaves=[^,\[]+)", " ".join(doc["commands"])).group(1)+ raw = subprocess.run(["ffmpeg", "-v", "error", "-i", str(d / "loud.wav"), "-filter_complex",+ f"color=c=black:s=320x180:r=25,format=rgba[bg];[0:a]{waves}[v];[bg][v]overlay=format=auto,format=rgb24",+ "-ss", "0.5", "-frames:v", "1", "-f", "rawvideo", "-"], stdout=subprocess.PIPE, check=True).stdout+ truth = collections.Counter(tuple(raw[i:i + 3]) for i in range(0, len(raw), 3) if any(raw[i:i + 3])).most_common(1)[0][0]+ cases.append(("waveform.py --color", out, [self._mode(out, (0, 0, 320, 180), ignore=(0, 0, 0))], [truth]))+ for label, out, got, want in cases:+ with self.subTest(tool=label):+ self._check(out, got, want)++ def test_a_picture_that_only_passes_through_keeps_its_pixels(self):+ """The fix changes only the drawn pixels. Encoded losslessly (--quality 0), the picture of a+ pad with chroma and of the same pad in black, and of chromatic text and the same text in+ white, are bit-identical away from what was drawn -- on every build: 7.1+ only labels the+ untagged picture (tags), 5.1-7.0 draw on an RGB copy and keep the untouched YUV pixels."""+ src = self.dir / "bars_un.mp4" # untagged, colourful+ if not src.exists():+ sh("ffmpeg", "-y", "-hide_banner", "-loglevel", "error", "-f", "lavfi", "-i", "testsrc2=size=384x128:rate=25",+ "-t", "1", "-c:v", "libx264", "-preset", "ultrafast", "-crf", "1", "-pix_fmt", "yuv420p", src)++ def planes(out, rect):+ w, h = 384, int(probe(str(out))["video"]["height"])+ raw = subprocess.run(["ffmpeg", "-v", "error", "-ss", "0.5", "-i", str(out), "-frames:v", "1", "-f", "rawvideo",+ "-pix_fmt", "yuv420p", "-"], stdout=subprocess.PIPE, check=True).stdout+ x0, y0, x1, y1 = rect+ luma = bytes(raw[y * w + x] for y in range(y0, y1) for x in range(x0, x1))+ cw, ch = w // 2, h // 2+ chroma = bytes(raw[w * h + p * cw * ch + y * cw + x] for p in (0, 1)+ for y in range(y0 // 2, y1 // 2) for x in range(x0 // 2, x1 // 2))+ return luma, chroma++ pairs = [("fit.py", ["--width", "384", "--height", "256", "--fit", "pad"], "--pad-color", "0xFF0000", "black",+ (0, 72, 384, 184))]+ if self._has("drawtext"):+ pairs.append(("overlay.py", ["--text", "X", "--font-size", "40", "--position", "top-left", "--margin", "8",+ "--border", "0"], "--font-color", "0x00FF00", "white", (0, 72, 384, 128)))+ for tool, args, flag, colour, grey, rect in pairs:+ with self.subTest(tool=tool):+ outs = []+ for c in (colour, grey):+ out = self.dir / f"keep_{tool[:-3]}_{c.strip('#')}.mp4"+ script(tool, src, *args, flag, c, "--quality", "0", "--fast", "-o", out)+ outs.append(planes(out, rect))+ self.assertEqual(outs[0][0], outs[1][0], f"{tool}: the luma beside the drawing changed")+ self.assertEqual(outs[0][1], outs[1][1], f"{tool}: the chroma beside the drawing changed")+class ScriptFontTests(unittest.TestCase):"""1.12: which script text is written in, and a font file that actually covers it."""@@ -3372,7 +3681,12 @@ class LineAwareLayoutTests(unittest.TestCase):proc = script("graphics.py", bg, "--template", "title", "--title", "One line", "--subtitle", "plain sub","-o", OUT / "t2_title1.mp4", "--dry-run", "--json")cmd = " ".join(json.loads(proc.stdout)["commands"])- self.assertIn("drawbox=x=(iw-86)/2:y=(ih)/2+39:", cmd)+ # the brand-yellow bar is a converted colour layer since 2.10.3 (drawn colours in BT.709): 86x4+ # at x=597, y=399 as the drawbox was, placed on overlay's even 4:2:0 grid with a transparent+ # first column and row+ self.assertIn("color=c=0xFFD200:s=86x4:", cmd)+ self.assertIn("pad=87:5:1:1:color=black@0", cmd)+ self.assertIn("overlay=x=596:y=398:", cmd)self.assertIn("y=(h-text_h)/2+60:", cmd)def test_a_two_line_name_and_title_fit_in_the_lower_third_bar(self):