remotion-ffmpeg-skill 0.14.2 → 0.14.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "remotion-ffmpeg-skill",
3
- "version": "0.14.2",
3
+ "version": "0.14.4",
4
4
  "description": "Agent Skill: ffmpeg-skill measures the footage, Remotion draws the motion graphics from those measurements, ffmpeg-skill checks and delivers the result. Timeline-driven captions, silence jump cuts and audio-peak motion with probe-first / verify-last rendering. Local, no API keys.",
5
5
  "keywords": [
6
6
  "remotion",
@@ -23,6 +23,7 @@ import shutil
23
23
  import subprocess
24
24
  import sys
25
25
  import time
26
+ from fractions import Fraction
26
27
  from pathlib import Path
27
28
  from typing import Any, Dict, List, Optional, Sequence, Tuple
28
29
 
@@ -267,6 +268,59 @@ def _finite(v: Any) -> bool:
267
268
  return isinstance(v, (int, float)) and not isinstance(v, bool) and math.isfinite(v)
268
269
 
269
270
 
271
+ # velisa/timebase@1 (Velisa ADR-026; docs/adr/0004): a frame rate is an exact fraction. A decimal that is not an
272
+ # integer and lies within 0.005 of base x 1000/1001 (base 24, 30, 48, 60, 120, 240) is that NTSC rate.
273
+ NTSC_BASES = (24, 30, 48, 60, 120, 240)
274
+ _DECIMAL_RE = re.compile(r"^\+?(\d+(\.\d*)?|\.\d+)([eE][+-]?\d+)?$")
275
+ _RATIONAL_RE = re.compile(r"^\s*(\d+)\s*/\s*(\d+)\s*$")
276
+
277
+
278
+ def exact_rate(value: Any) -> Optional[Fraction]:
279
+ """A frame rate as an exact fraction, or None when it is not one in (0, 1000] (rule TB-2): 'N/D' as written
280
+ (ffprobe's r_frame_rate; '2997/100' stays), an integer as N/1, an NTSC decimal (29.97, 23.976, 59.94) as N/1001,
281
+ never as 2997/100, any other decimal as its exact value; a float through its shortest text."""
282
+ if isinstance(value, bool) or value is None:
283
+ return None
284
+ if isinstance(value, Fraction):
285
+ r = value
286
+ elif isinstance(value, int):
287
+ r = Fraction(value)
288
+ else:
289
+ if isinstance(value, float):
290
+ if not math.isfinite(value):
291
+ return None
292
+ text = repr(value)
293
+ elif isinstance(value, str):
294
+ text = value.strip()
295
+ else:
296
+ return None
297
+ m = _RATIONAL_RE.match(text)
298
+ if m:
299
+ if int(m.group(2)) == 0:
300
+ return None
301
+ r = Fraction(int(m.group(1)), int(m.group(2)))
302
+ elif _DECIMAL_RE.match(text):
303
+ r = Fraction(text)
304
+ if r.denominator != 1:
305
+ r = next((Fraction(b * 1000, 1001) for b in NTSC_BASES
306
+ if abs(r - Fraction(b * 1000, 1001)) <= Fraction(5, 1000)), r)
307
+ else:
308
+ return None
309
+ return r if 0 < r <= 1000 else None
310
+
311
+
312
+ def stream_rate(v: Dict[str, Any], default: float = 30.0) -> float:
313
+ """The rate a timeline is drawn at, as the float of the exact fraction (TB-1, TB-2): ffprobe's r_frame_rate when
314
+ it is the stream's rate (within 0.001 of the probe's fps), else the probe's 3-decimal fps read by TB-2 (29.97 ->
315
+ 30000/1001). The compositions and expected_frames() then multiply by 29.97002997..., not by 29.97: the file
316
+ Remotion writes carries 30000/1001, and a long timeline does not drift a frame every ~9 hours."""
317
+ fps = exact_rate(v.get("fps"))
318
+ r = exact_rate(v.get("r_frame_rate"))
319
+ if r is not None and (fps is None or abs(r - fps) < Fraction(1, 1000)):
320
+ return float(r)
321
+ return float(fps) if fps is not None else float(default)
322
+
323
+
270
324
  def sec_to_frames(seconds: float, fps: float) -> int:
271
325
  # JavaScript's Math.round (round half up), not Python's round() (half to even): the TypeScript
272
326
  # compositions compute the same frame numbers and the two must agree to the frame (94.5 -> 95).
@@ -299,8 +299,9 @@ def tool_spec(name: str) -> Dict[str, Any]:
299
299
  # The AI Video Production OS registry's keys (skill_id, provides[], dependencies[]): one capability
300
300
  # per public tool except doctor, all EXPERIMENTAL; ffmpeg-skill is the only Skill this one calls.
301
301
  PROVIDES_EXCLUDE = {"doctor"}
302
- # 3.x accepted (docs/design-decisions.md): 3.0.0 removed nothing this skill calls, and its cut.py changes do not reach it
303
- FFMPEG_SKILL_RANGE = ">=2.0.0 <4.0.0"
302
+ # 3.x and 4.x accepted (docs/design-decisions.md): 3.0.0 and 4.0.0 removed nothing this skill calls, and their
303
+ # behaviour changes (cut.py in 3.0; export loudness, fit --method, join --copy in 4.0) do not reach the calls made here
304
+ FFMPEG_SKILL_RANGE = ">=2.0.0 <5.0.0"
304
305
 
305
306
 
306
307
  def provides() -> List[Dict[str, str]]:
@@ -34,7 +34,7 @@ from typing import Any, Dict, List, Optional, Tuple
34
34
  sys.path.insert(0, str(Path(__file__).resolve().parent))
35
35
  from _common import (STATE, TIMELINE_SCHEMA, SKILL_ID, add_common, apply_common, default_output, die,
36
36
  emit, ffmpeg_skill_version, ffskill, ffskill_cmd, info, refuse_existing,
37
- require_ffmpeg_skill, shell_join, version)
37
+ require_ffmpeg_skill, shell_join, stream_rate, version)
38
38
  from _captions import (CaptionError, cues_from_words, parse_chapters, read_captions, read_emphasis,
39
39
  read_word_timings, resolve_emphasis, youtube_chapter_warnings)
40
40
 
@@ -490,7 +490,7 @@ def main() -> None:
490
490
  pip = {"path": str(cam.resolve()), "name": cam.name,
491
491
  "duration": round(float(cv.get("duration") or cp.get("duration") or 0), 3),
492
492
  "width": int(cv.get("width") or 0), "height": int(cv.get("height") or 0),
493
- "fps": float(cv.get("fps") or 0), "offset": 0.0, "confidence": None, "measured": False,
493
+ "fps": stream_rate(cv, 0.0), "offset": 0.0, "confidence": None, "measured": False,
494
494
  "conform": cam_conform}
495
495
  if a.pip_offset is not None:
496
496
  pip["offset"] = round(float(a.pip_offset), 4)
@@ -514,7 +514,7 @@ def main() -> None:
514
514
  "path": str(src.resolve()),
515
515
  "name": src.name,
516
516
  "duration": round(duration, 3),
517
- "fps": float(v.get("fps") or 30.0),
517
+ "fps": stream_rate(v),
518
518
  "width": int(v.get("width") or 1920),
519
519
  "height": int(v.get("height") or 1080),
520
520
  "has_video": bool(video),
@@ -11,14 +11,16 @@ Examples:
11
11
  """
12
12
  import argparse
13
13
  import json
14
+ import shutil
14
15
  import sys
16
+ import tempfile
15
17
  from pathlib import Path
16
- from typing import Optional
18
+ from typing import List, Optional
17
19
 
18
20
  sys.path.insert(0, str(Path(__file__).resolve().parent))
19
21
  from _captions import parse_chapters
20
- from _common import (STATE, add_common, apply_common, default_output, die, emit, ffskill, ffskill_cmd, info,
21
- refuse_existing, require_ffmpeg_skill, shell_join)
22
+ from _common import (STATE, add_common, apply_common, default_output, die, emit, ffmpeg_skill_version, ffskill,
23
+ ffskill_cmd, info, refuse_existing, require_ffmpeg_skill, shell_join)
22
24
 
23
25
  # ffmpeg-skill's templates/*.json names (render.py --list-templates).
24
26
  PLATFORMS = ["youtube", "youtube-shorts", "shorts", "reels", "tiktok", "x", "facebook", "linkedin", "podcast", "audiogram"]
@@ -39,6 +41,37 @@ def brand_logo(brand: Optional[str]) -> Optional[Path]:
39
41
  return lp if lp.is_absolute() else bp.resolve().parent / lp
40
42
 
41
43
 
44
+ def template_runs_content_check() -> bool:
45
+ """ffmpeg-skill 4.0.0 (#575) made a delivery template's check stage also run check.py --content (black frames,
46
+ freezes, flashes, silence), and a failed row fails the render. A Remotion deliverable often holds a still picture
47
+ on purpose (a title card, a slide), which 4.0 reads as `frozen`. deliver keeps 3.x's template (platform rows
48
+ only); its own check.py --platform still runs on the result (docs/design-decisions.md)."""
49
+ version = ffmpeg_skill_version(require_ffmpeg_skill()) or ""
50
+ try:
51
+ return int(version.split(".")[0]) >= 4
52
+ except ValueError:
53
+ return False
54
+
55
+
56
+ def render_template(args: List[str], commands: List[str]) -> None:
57
+ """render.py --template with args; on ffmpeg-skill 4.x through --write-project with the check stage's
58
+ `content` set to false (the migration ffmpeg-skill's docs/contract.md gives), so only the platform rows run."""
59
+ if not template_runs_content_check():
60
+ commands.append(ffskill("render", args)["_command"])
61
+ return
62
+ work = Path(tempfile.mkdtemp(prefix="rfs-deliver-"))
63
+ try:
64
+ project = work / "project.json"
65
+ commands.append(ffskill("render", [*args, "--write-project", str(project)])["_command"])
66
+ proj = json.loads(project.read_text(encoding="utf-8"))
67
+ if isinstance(proj.get("check"), dict):
68
+ proj["check"]["content"] = False
69
+ project.write_text(json.dumps(proj, indent=2) + "\n", encoding="utf-8")
70
+ commands.append(ffskill("render", [str(project)] + (["--overwrite"] if "--overwrite" in args else []))["_command"])
71
+ finally:
72
+ shutil.rmtree(work, ignore_errors=True)
73
+
74
+
42
75
  def youtube_description(chapters_file: Path) -> str:
43
76
  """The chapter list as YouTube reads it from a description: `00:00 Title` per line."""
44
77
  chapters = parse_chapters(chapters_file.read_text(encoding="utf-8-sig"), 0)
@@ -86,7 +119,8 @@ def main() -> None:
86
119
  out = Path(a.output).expanduser() if a.output else default_output(src, f"_{a.platform}", ".mp4")
87
120
  refuse_existing(out)
88
121
  templated = out.with_name(out.stem + ".nochapters" + out.suffix) if a.chapters else out
89
- args = ["--template", a.platform, str(src), "-o", str(templated)]
122
+ # absolute paths: on ffmpeg-skill 4.x the filled project is rendered from a temporary directory
123
+ args = ["--template", a.platform, str(src.resolve()), "-o", str(templated.resolve())]
90
124
  if a.brand:
91
125
  bp = Path(a.brand).expanduser().resolve()
92
126
  if not bp.is_file():
@@ -103,14 +137,18 @@ def main() -> None:
103
137
  meta_args = ([str(templated), "--chapters", str(Path(a.chapters).expanduser()), "-o", str(out)]
104
138
  + (["--overwrite"] if STATE.overwrite else [])) if a.chapters else []
105
139
  if STATE.dry_run:
106
- commands.append(shell_join(ffskill_cmd("render", args)))
140
+ if template_runs_content_check():
141
+ project = "<tmp>/project.json"
142
+ commands.append(shell_join(ffskill_cmd("render", [*args, "--write-project", project])))
143
+ commands.append(shell_join(ffskill_cmd("render", [project] + (["--overwrite"] if "--overwrite" in args else []))))
144
+ else:
145
+ commands.append(shell_join(ffskill_cmd("render", args)))
107
146
  if a.chapters:
108
147
  commands.append(shell_join(ffskill_cmd("metadata", meta_args)))
109
148
  commands.append(shell_join(ffskill_cmd("check", [str(out), "--platform", a.platform])))
110
149
  emit({"output": str(out), "commands": commands}, f"would deliver {out}")
111
150
  return
112
- r = ffskill("render", args)
113
- commands.append(r["_command"])
151
+ render_template(args, commands)
114
152
  if a.chapters:
115
153
  m = ffskill("metadata", meta_args)
116
154
  commands.append(m["_command"])