diff --git a/tests/tools/test_cinematic_remotion_adapter.py b/tests/tools/test_cinematic_remotion_adapter.py index 4d8ee43fb8..f2ca0846d2 100644 --- a/tests/tools/test_cinematic_remotion_adapter.py +++ b/tests/tools/test_cinematic_remotion_adapter.py @@ -213,3 +213,248 @@ 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(tmp_path) -> None: + bed = tmp_path / "bed.mp3" + bed.write_bytes(b"dummy-bed") + legacy = tmp_path / "legacy.mp3" + legacy.write_bytes(b"dummy-legacy") + asset_lookup = { + "asset_music_bed": {"id": "asset_music_bed", "path": str(bed)}, + "asset_legacy": {"id": "asset_legacy", "path": str(legacy)}, + } + + # 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"] == str(bed.resolve()) + assert resolved["volume"] == 0.6 + assert resolved["fadeInSeconds"] == 1.0 + assert resolved["fadeOutSeconds"] == 4.0 + assert resolved["ducking"] is False + assert "fade_in_seconds" not in resolved + assert "fade_out_seconds" not in resolved + + # 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"] == str(legacy.resolve()) + assert resolved["volume"] == 0.3 + assert resolved["fadeInSeconds"] == 0.5 + assert resolved["fadeOutSeconds"] == 2.0 + assert "fade_in_seconds" not in resolved + assert "fade_out_seconds" not in resolved + + +def test_resolve_music_omitted_volume_and_fades_preserves_composition_defaults(tmp_path) -> None: + bed = tmp_path / "bed.mp3" + bed.write_bytes(b"dummy-bed") + asset_lookup = { + "asset_music_bed": {"id": "asset_music_bed", "path": str(bed)}, + } + ed = { + "audio": { + "music": { + "asset_id": "asset_music_bed", + } + } + } + resolved, err = VideoCompose._resolve_music(ed, asset_lookup) + assert err is None + assert resolved is not None + assert "volume" not in resolved + assert "fadeInSeconds" not in resolved + assert "fadeOutSeconds" not in resolved + + +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_resolve_music_missing_file_fails_loudly(tmp_path) -> None: + nonexistent = tmp_path / "nonexistent.mp3" + asset_lookup = { + "asset_music_bed": {"id": "asset_music_bed", "path": str(nonexistent)}, + } + ed = { + "audio": { + "music": { + "asset_id": "asset_music_bed", + } + } + } + resolved, err = VideoCompose._resolve_music(ed, asset_lookup) + assert resolved is None + assert err == f"Music file not found: {nonexistent}" + + # Explicit src pointing to nonexistent file + ed_src = { + "audio": { + "music": { + "src": str(nonexistent), + } + } + } + resolved2, err2 = VideoCompose._resolve_music(ed_src, {}) + assert resolved2 is None + assert err2 == f"Music file not found: {nonexistent}" + + +def test_render_resolves_and_stages_music_into_remotion_props(monkeypatch, tmp_path) -> None: + monkeypatch.setattr(VideoCompose, "_remotion_available", lambda self: True) + 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 + + +def test_render_missing_music_file_returns_error(tmp_path) -> None: + clip = tmp_path / "clip.mp4" + clip.write_bytes(b"dummy-clip") + missing_music = tmp_path / "missing_music.mp3" + 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_music_bed", + } + }, + }, + "asset_manifest": { + "version": "1.0", + "assets": [ + {"id": "asset_clip", "path": str(clip)}, + {"id": "asset_music_bed", "path": str(missing_music)}, + ], + }, + }) + assert not result.success + assert f"Music file not found: {missing_music}" in result.error + diff --git a/tools/video/video_compose.py b/tools/video/video_compose.py index d5066e3c2a..8aea33bbfd 100644 --- a/tools/video/video_compose.py +++ b/tools/video/video_compose.py @@ -855,6 +855,112 @@ 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 music_asset_id: + return None, f"Music asset_id {music_asset_id!r} not found in asset_manifest." + else: + resolved_path = music_source + + # Validate that the resolved file exists on disk + try: + if str(resolved_path).lower().startswith("file:"): + raw_file_path = cls._file_uri_to_raw_path(str(resolved_path)) + else: + raw_file_path = str(resolved_path) + file_obj = Path(raw_file_path) + if not file_obj.is_file(): + return None, f"Music file not found: {raw_file_path}" + resolved_path = str(file_obj.resolve()) + except OSError: + return None, f"Music file not found: {resolved_path}" + + resolved: dict[str, Any] = { + "src": resolved_path, + } + + # Volume: only emit when explicitly provided so composition defaults apply + # (CinematicRenderer defaults to 0.15, Explainer defaults to 0.1). + if "volume" in music_cfg and music_cfg["volume"] is not None: + try: + resolved["volume"] = float(music_cfg["volume"]) + except (TypeError, ValueError): + pass + + # Fades: only emit camelCase properties expected by Remotion + fade_in = music_cfg.get( + "fadeInSeconds", music_cfg.get("fade_in_seconds") + ) + if fade_in is not None: + try: + resolved["fadeInSeconds"] = float(fade_in) + except (TypeError, ValueError): + pass + + fade_out = music_cfg.get( + "fadeOutSeconds", music_cfg.get("fade_out_seconds") + ) + if fade_out is not None: + try: + resolved["fadeOutSeconds"] = float(fade_out) + except (TypeError, ValueError): + pass + + 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( + "trimBeforeSeconds", music_cfg.get("trim_before_seconds") + ) + if "trim_after_seconds" in music_cfg or "trimAfterSeconds" in music_cfg: + resolved["trimAfterSeconds"] = music_cfg.get( + "trimAfterSeconds", music_cfg.get("trim_after_seconds") + ) + if "offset_seconds" in music_cfg or "offsetSeconds" in music_cfg: + resolved["offsetSeconds"] = music_cfg.get( + "offsetSeconds", music_cfg.get("offset_seconds") + ) + + return resolved, None + @staticmethod def _file_uri_to_raw_path(uri: str) -> str: """Convert a ``file:`` URI to a raw filesystem path string. @@ -1684,9 +1790,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 @@ -1696,7 +1814,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, @@ -1706,7 +1824,7 @@ 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, @@ -1714,7 +1832,7 @@ def _render(self, inputs: dict[str, Any]) -> ToolResult: # --- 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: @@ -1768,7 +1886,7 @@ def _render(self, inputs: dict[str, Any]) -> ToolResult: # Build compose inputs compose_inputs = dict(inputs) - compose_inputs["edit_decisions"] = dict(edit_decisions, cuts=resolved_cuts) + compose_inputs["edit_decisions"] = dict(resolved_edit_decisions, cuts=resolved_cuts) compose_inputs["output_path"] = str(output_path) if subtitle_path: compose_inputs["subtitle_path"] = subtitle_path @@ -2040,6 +2158,26 @@ 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: + if "fadeInSeconds" not in music_block: + music_block["fadeInSeconds"] = music_block["fade_in_seconds"] + del music_block["fade_in_seconds"] + if "fade_out_seconds" in music_block: + if "fadeOutSeconds" not in music_block: + music_block["fadeOutSeconds"] = music_block["fade_out_seconds"] + del 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.