Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
157 changes: 157 additions & 0 deletions tests/tools/test_cinematic_remotion_adapter.py
Original file line number Diff line number Diff line change
Expand Up @@ -213,3 +213,160 @@ def test_remotion_render_rejects_cinematic_overlays_before_rendering(
"CinematicRenderer does not support non-empty overlays; remove overlays "
"or use a composition that supports them."
)


def test_resolve_music_canonical_and_legacy() -> None:
asset_lookup = {
"asset_music_bed": {"id": "asset_music_bed", "path": "path/to/bed.mp3"},
"asset_legacy": {"id": "asset_legacy", "path": "path/to/legacy.mp3"},
}

# Canonical audio.music
canonical_ed = {
"audio": {
"music": {
"asset_id": "asset_music_bed",
"volume": 0.6,
"fade_in_seconds": 1.0,
"fade_out_seconds": 4.0,
"ducking": False,
}
}
}
resolved, err = VideoCompose._resolve_music(canonical_ed, asset_lookup)
assert err is None
assert resolved is not None
assert resolved["src"] == "path/to/bed.mp3"
assert resolved["volume"] == 0.6
assert resolved["fadeInSeconds"] == 1.0
assert resolved["fadeOutSeconds"] == 4.0
assert resolved["ducking"] is False

# Legacy music
legacy_ed = {
"music": {
"asset_id": "asset_legacy",
"volume": 0.3,
"fade_in_seconds": 0.5,
"fade_out_seconds": 2.0,
}
}
resolved, err = VideoCompose._resolve_music(legacy_ed, asset_lookup)
assert err is None
assert resolved is not None
assert resolved["src"] == "path/to/legacy.mp3"
assert resolved["volume"] == 0.3
assert resolved["fadeInSeconds"] == 0.5
assert resolved["fadeOutSeconds"] == 2.0


def test_resolve_music_missing_asset_id_fails_loudly() -> None:
ed = {
"audio": {
"music": {
"asset_id": "asset_nonexistent_bed",
}
}
}
resolved, err = VideoCompose._resolve_music(ed, {})
assert resolved is None
assert err == "Music asset_id 'asset_nonexistent_bed' not found in asset_manifest."


def test_render_resolves_and_stages_music_into_remotion_props(monkeypatch, tmp_path) -> None:

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[P2] This test fails in CI. It goes through _render -> _needs_remotion -> _remotion_available(), which requires remotion-composer/node_modules. CI installs Python deps only, so the render falls back to ffmpeg and the fake run_command hits StopIteration (empty error string). On a Python-only container: 12 passed, 1 failed in this file; with node_modules installed: 13 passed.

Fix: add monkeypatch.setattr(VideoCompose, "_remotion_available", lambda self: True) at the top of the test (the other render tests in this file call _remotion_render directly, which is why they are unaffected).

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Addressed in 5d34656 — added monkeypatch for _remotion_available to return True so the test passes cleanly in Python-only CI environments without node_modules.

clip = tmp_path / "clip.mp4"
clip.write_bytes(b"dummy-clip")
music = tmp_path / "music_bed.mp3"
music.write_bytes(b"dummy-audio-content")
output = tmp_path / "out.mp4"
captured = {}

def fake_run_command(self, command, **kwargs):
captured["command"] = command
props_arg = next(arg for arg in command if arg.startswith("--props="))
captured["props"] = json.loads(Path(props_arg.split("=", 1)[1]).read_text())
public_arg = next(arg for arg in command if arg.startswith("--public-dir="))
public_dir = Path(public_arg.split("=", 1)[1])
captured["staged_music_exists"] = (
public_dir / captured["props"]["music"]["src"]
).exists()
output.write_bytes(b"rendered-mp4")

monkeypatch.setattr(VideoCompose, "run_command", fake_run_command)
monkeypatch.setattr(
VideoCompose,
"_run_final_review",
lambda self, *args, **kwargs: {"status": "pass", "issues": []},
)

result = VideoCompose().execute({
"operation": "render",
"output_path": str(output),
"edit_decisions": {
"renderer_family": "documentary-montage",
"render_runtime": "remotion",
"cuts": [
{
"id": "c1",
"source": "asset_clip",
"in_seconds": 0,
"out_seconds": 4,
}
],
"audio": {
"music": {
"asset_id": "asset_music_bed",
"volume": 0.6,
"fade_in_seconds": 1.0,
"fade_out_seconds": 4.0,
"ducking": False,
}
},
},
"asset_manifest": {
"version": "1.0",
"assets": [
{"id": "asset_clip", "path": str(clip)},
{"id": "asset_music_bed", "path": str(music)},
],
},
})

assert result.success, result.error
assert captured["staged_music_exists"] is True
# Verify top-level music prop for CinematicRenderer
music_prop = captured["props"]["music"]
assert music_prop["volume"] == 0.6
assert music_prop["fadeInSeconds"] == 1.0
assert music_prop["fadeOutSeconds"] == 4.0
assert music_prop["src"] != str(music) # Staged filename
# Verify audio.music prop for Explainer
assert captured["props"]["audio"]["music"]["src"] == music_prop["src"]
# 1 video clip + 1 music file = 2 staged media
assert result.data["staged_media_count"] == 2


def test_render_missing_music_asset_id_returns_error(tmp_path) -> None:
clip = tmp_path / "clip.mp4"
clip.write_bytes(b"dummy-clip")
result = VideoCompose().execute({
"operation": "render",
"output_path": str(tmp_path / "out.mp4"),
"edit_decisions": {
"renderer_family": "documentary-montage",
"render_runtime": "remotion",
"cuts": [{"id": "c1", "source": "asset_clip", "in_seconds": 0, "out_seconds": 4}],
"audio": {
"music": {
"asset_id": "asset_missing_music",
}
},
},
"asset_manifest": {
"version": "1.0",
"assets": [{"id": "asset_clip", "path": str(clip)}],
},
})
assert not result.success
assert "Music asset_id 'asset_missing_music' not found in asset_manifest." in result.error

139 changes: 135 additions & 4 deletions tools/video/video_compose.py
Original file line number Diff line number Diff line change
Expand Up @@ -479,6 +479,15 @@ def _compose(self, inputs: dict[str, Any]) -> ToolResult:
output_path = Path(inputs.get("output_path", "composed_output.mp4"))
output_path.parent.mkdir(parents=True, exist_ok=True)
audio_path = inputs.get("audio_path")
if not audio_path and edit_decisions:
music_cfg = (
(edit_decisions.get("audio") or {}).get("music")
or edit_decisions.get("music")
)
if isinstance(music_cfg, dict):
cand = music_cfg.get("src") or music_cfg.get("source") or music_cfg.get("path")
if cand and Path(cand).exists():
audio_path = str(Path(cand).resolve())

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[P1] This breaks the ffmpeg runtime. Setting audio_path from music.src feeds the existing -map 0:v -map 1:a -c:a aac -shortest branch below, which treats audio_path as the complete replacement track. volume, fades, loop and ducking are ignored, the clip's own audio is dropped, and -shortest cuts the video to the music length.

Repro (render_runtime: "ffmpeg", 6 s clip that has its own audio, 2 s music asset, volume: 0.1):

  • PR: 1.9 s video, 2.0 s audio, music at full level (mean -21.1 dB, so volume is ignored), clip audio gone
  • main, same inputs: 6.0 s, clip audio kept

_render_via_ffmpeg now passes the resolved music (edit_decisions=resolved_edit_decisions), so this is reachable from the normal render path. Please drop this block. If ffmpeg-runtime music is wanted, it needs a real mix (volume, fades, loop, ducking, as audio_mixer does), as a separate change.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Addressed in 5d34656 — dropped this block from _compose so clip audio is kept and video duration is not truncated.

subtitle_path = inputs.get("subtitle_path")
codec = inputs.get("codec", "libx264")
crf = inputs.get("crf", 23)
Expand Down Expand Up @@ -855,6 +864,100 @@ def _cuts_to_cinematic_scenes(cuts: list[dict[str, Any]]) -> list[dict[str, Any]

return scenes

@classmethod
def _resolve_music(
cls,
edit_decisions: dict[str, Any],
asset_lookup: dict[str, Any],
) -> tuple[dict[str, Any] | None, str | None]:
"""Resolve music configuration from edit_decisions and asset manifest.

Supports canonical `edit_decisions.audio.music` and legacy `edit_decisions.music`.
Resolves `asset_id` against `asset_lookup` to a local file path and adapts
snake_case fade settings (`fade_in_seconds`, `fade_out_seconds`) to the camelCase
properties (`fadeInSeconds`, `fadeOutSeconds`) expected by Remotion compositions.

Returns (resolved_music_dict, error_string). If an explicit asset_id is present
but cannot be found in asset_lookup, returns (None, error_string) to fail loudly.
"""
music_cfg = (
(edit_decisions.get("audio") or {}).get("music")
or edit_decisions.get("music")
)
if not music_cfg or not isinstance(music_cfg, dict):
return None, None

music_asset_id = music_cfg.get("asset_id")
music_source = (
music_asset_id
or music_cfg.get("src")
or music_cfg.get("source")
or music_cfg.get("path")
)
if not music_source:
return None, None

resolved_path = None
if music_source in asset_lookup:
resolved_path = asset_lookup[music_source].get("path", music_source)
elif Path(music_source).exists():
resolved_path = str(Path(music_source).resolve())
elif music_asset_id:
return None, f"Music asset_id {music_asset_id!r} not found in asset_manifest."
else:
resolved_path = music_source

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[P2] "Fails loudly" only covers an unknown asset_id. When the asset is in the manifest but its file does not exist (or an explicit src points at a missing file), this returns success. Staging then leaves the dead path in the props, so the failure shows up late (mid-render) or as silence. Please check Path(resolved_path).is_file() and return the same kind of error.

Related: the Remotion-unavailable fallback in _render still builds compose_inputs from the unresolved edit_decisions (compose_inputs["edit_decisions"] = dict(edit_decisions, cuts=resolved_cuts)), so music is silently dropped there, which is the #937 symptom. Either pass the resolved decisions or call it out as out of scope.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Addressed in 5d34656 — added Path(resolved_path).is_file() validation (including file:// URI support) so missing music files fail loudly with a clear error. Also forwarded resolved_edit_decisions in the Remotion-unavailable fallback in _render.


volume = music_cfg.get("volume", 0.15)
try:
volume = float(volume)
except (TypeError, ValueError):
volume = 0.15

fade_in = music_cfg.get(
"fade_in_seconds", music_cfg.get("fadeInSeconds", 2.0)
)
try:
fade_in = float(fade_in)
except (TypeError, ValueError):
fade_in = 2.0

fade_out = music_cfg.get(
"fade_out_seconds", music_cfg.get("fadeOutSeconds", 3.0)
)
try:
fade_out = float(fade_out)
except (TypeError, ValueError):
fade_out = 3.0

resolved: dict[str, Any] = {
"src": str(resolved_path),
"volume": volume,
"fadeInSeconds": fade_in,
"fadeOutSeconds": fade_out,
"fade_in_seconds": fade_in,
"fade_out_seconds": fade_out,
}
if music_asset_id:
resolved["asset_id"] = music_asset_id
if "ducking" in music_cfg:
resolved["ducking"] = music_cfg["ducking"]
if "loop" in music_cfg:
resolved["loop"] = music_cfg["loop"]
if "trim_before_seconds" in music_cfg or "trimBeforeSeconds" in music_cfg:
resolved["trimBeforeSeconds"] = music_cfg.get(
"trim_before_seconds", music_cfg.get("trimBeforeSeconds")
)
if "trim_after_seconds" in music_cfg or "trimAfterSeconds" in music_cfg:
resolved["trimAfterSeconds"] = music_cfg.get(
"trim_after_seconds", music_cfg.get("trimAfterSeconds")
)
if "offset_seconds" in music_cfg or "offsetSeconds" in music_cfg:
resolved["offsetSeconds"] = music_cfg.get(
"offset_seconds", music_cfg.get("offsetSeconds")
)

return resolved, None

@staticmethod
def _file_uri_to_raw_path(uri: str) -> str:
"""Convert a ``file:`` URI to a raw filesystem path string.
Expand Down Expand Up @@ -1684,9 +1787,21 @@ def _render(self, inputs: dict[str, Any]) -> ToolResult:
resolved_cut["source"] = asset_lookup[source_id]["path"]
resolved_cuts.append(resolved_cut)

# Resolve music config from edit_decisions
resolved_music, music_err = self._resolve_music(edit_decisions, asset_lookup)
if music_err:
return ToolResult(success=False, error=music_err)

resolved_edit_decisions = dict(edit_decisions, cuts=resolved_cuts)
if resolved_music:
resolved_edit_decisions["music"] = resolved_music
audio_dict = dict(resolved_edit_decisions.get("audio") or {})
audio_dict["music"] = resolved_music
resolved_edit_decisions["audio"] = audio_dict

# --- Pre-compose validation gate ---
scene_plan = inputs.get("scene_plan")
validation_block = self._pre_compose_validation(edit_decisions, resolved_cuts, scene_plan)
validation_block = self._pre_compose_validation(resolved_edit_decisions, resolved_cuts, scene_plan)
if validation_block is not None:
return validation_block

Expand All @@ -1696,7 +1811,7 @@ def _render(self, inputs: dict[str, Any]) -> ToolResult:
if render_runtime == "hyperframes":
return self._render_via_hyperframes(
inputs=inputs,
edit_decisions=edit_decisions,
edit_decisions=resolved_edit_decisions,
asset_manifest=asset_manifest,
resolved_cuts=resolved_cuts,
output_path=output_path,
Expand All @@ -1706,15 +1821,15 @@ def _render(self, inputs: dict[str, Any]) -> ToolResult:
# Caller explicitly asked for FFmpeg — don't auto-upgrade to Remotion.
return self._render_via_ffmpeg(
inputs=inputs,
edit_decisions=edit_decisions,
edit_decisions=resolved_edit_decisions,
resolved_cuts=resolved_cuts,
output_path=output_path,
profile=profile,
)
# --- Explicit Remotion path (render_runtime == 'remotion') ---
if self._needs_remotion(resolved_cuts):
remotion_inputs: dict[str, Any] = {
"edit_decisions": dict(edit_decisions, cuts=resolved_cuts),
"edit_decisions": resolved_edit_decisions,
"output_path": str(output_path),
}
if profile:
Expand Down Expand Up @@ -2040,6 +2155,22 @@ def _remotion_render(self, inputs: dict[str, Any]) -> ToolResult:
# Deep-copy props so we don't mutate the original
props = json.loads(json.dumps(composition_data))

# Adapt audio.music <-> top-level music and snake_case <-> camelCase for Remotion
music_block = (props.get("audio") or {}).get("music") or props.get("music")
if isinstance(music_block, dict):
if "fade_in_seconds" in music_block and "fadeInSeconds" not in music_block:
music_block["fadeInSeconds"] = music_block["fade_in_seconds"]
if "fade_out_seconds" in music_block and "fadeOutSeconds" not in music_block:
music_block["fadeOutSeconds"] = music_block["fade_out_seconds"]
if "source" in music_block and "src" not in music_block:
music_block["src"] = music_block["source"]
if "music" not in props:
props["music"] = music_block
if "audio" not in props or not isinstance(props["audio"], dict):
props["audio"] = {}
if "music" not in props["audio"]:
props["audio"]["music"] = music_block

# Build a custom themeConfig from the playbook's actual colors.
# This ensures every video gets a unique visual identity derived
# from its production decisions — not picked from a preset menu.
Expand Down