All checks were successful
CI / lint-and-test (push) Successful in 11s
Reviewed-on: #4 Co-authored-by: pedro-bento <mail@pbento.pt> Co-committed-by: pedro-bento <mail@pbento.pt>
346 lines
11 KiB
Python
346 lines
11 KiB
Python
"""Tests for the ``--render`` stage (ffmpeg graph builder + CLI).
|
|
|
|
Pure builder/parse tests run everywhere; the integration test is skipped when
|
|
ffmpeg/ffprobe are not on ``PATH``.
|
|
"""
|
|
|
|
import hashlib
|
|
import json
|
|
import shutil
|
|
import subprocess
|
|
|
|
import pytest
|
|
|
|
import cadence
|
|
from cadence import Segment, Word
|
|
|
|
HAVE_FFMPEG = (
|
|
shutil.which("ffmpeg") is not None and shutil.which("ffprobe") is not None
|
|
)
|
|
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# expression builders
|
|
# ---------------------------------------------------------------------------
|
|
|
|
def test_video_select_expression_ranges():
|
|
assert cadence.build_video_select_expression([]) == "0"
|
|
assert (
|
|
cadence.build_video_select_expression([(0, 29), (45, 59)])
|
|
== "(between(n,0,29)+between(n,45,59))"
|
|
)
|
|
|
|
|
|
def test_video_select_expression_balanced_tree():
|
|
# Pairwise grouping ((a+b)+(c+d)), not the flat a+b+c+d chain that
|
|
# overflows ffmpeg's recursive expression parser past ~110 terms.
|
|
expr = cadence.build_video_select_expression(
|
|
[(0, 5), (10, 15), (20, 25), (30, 35)]
|
|
)
|
|
assert expr == (
|
|
"((between(n,0,5)+between(n,10,15))"
|
|
"+(between(n,20,25)+between(n,30,35)))"
|
|
)
|
|
|
|
|
|
def test_audio_select_expression_is_half_open():
|
|
# Frame 0..29 at 30fps is the half-open interval [0s, 1s).
|
|
expr = cadence.build_audio_select_expression([(0, 29)], 30.0)
|
|
assert expr == "gte(t,0.000000000)*lt(t,1.000000000)"
|
|
assert cadence.build_audio_select_expression([], 30.0) == "0"
|
|
|
|
|
|
def test_audio_select_expression_balanced_tree():
|
|
expr = cadence.build_audio_select_expression([(0, 29), (45, 59)], 30.0)
|
|
assert expr == (
|
|
"(gte(t,0.000000000)*lt(t,1.000000000)"
|
|
"+gte(t,1.500000000)*lt(t,2.000000000))"
|
|
)
|
|
|
|
|
|
@pytest.mark.skipif(not HAVE_FFMPEG, reason="ffmpeg/ffprobe not on PATH")
|
|
def test_long_select_expression_parses():
|
|
# Regression: a flat chain of >110 terms overflowed ffmpeg's expression
|
|
# parser ("Error while parsing expression" / "Cannot allocate memory"); a
|
|
# ~34-minute cut list has ~600. The balanced tree must parse.
|
|
expr = cadence.build_video_select_expression(
|
|
[(i * 10, i * 10 + 5) for i in range(600)]
|
|
)
|
|
cmd = [
|
|
"ffmpeg", "-hide_banner", "-loglevel", "error",
|
|
"-f", "lavfi", "-i", "color=black:s=64x36:r=30:d=2",
|
|
"-vf", f"select='{expr}'", "-f", "null", "-",
|
|
]
|
|
result = subprocess.run(cmd, capture_output=True, text=True)
|
|
assert result.returncode == 0, result.stderr[-800:]
|
|
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# graph builder
|
|
# ---------------------------------------------------------------------------
|
|
|
|
def test_render_graph_single_stream_matches_keep_segments(make_track):
|
|
td = make_track(
|
|
duration=2.0,
|
|
audio_stream_count=1,
|
|
audio_track_idx=0,
|
|
timeline=[
|
|
Segment(0.0, 1.0, "keep"),
|
|
Segment(1.0, 1.5, "drop", reason="silence"),
|
|
Segment(1.5, 2.0, "keep"),
|
|
],
|
|
)
|
|
|
|
plan = cadence.build_render_graph(td, jl_mode="off", jl_frames=0)
|
|
|
|
assert plan.voice_stream == 0
|
|
assert plan.system_stream is None
|
|
assert plan.audio_label == "[voice]"
|
|
assert plan.total_frames == 45 # 30 + 15 frames kept
|
|
assert plan.duration == pytest.approx(1.5)
|
|
assert "select='(between(n,0,29)+between(n,45,59))'" in plan.filtergraph
|
|
assert "setpts=N/FRAME_RATE/TB" in plan.filtergraph
|
|
assert "asetpts=N/SR/TB" in plan.filtergraph
|
|
assert "amix" not in plan.filtergraph
|
|
|
|
|
|
def test_render_graph_system_audio_mixes_to_stereo(make_track):
|
|
td = make_track(
|
|
duration=2.0,
|
|
audio_stream_count=2,
|
|
audio_track_idx=0,
|
|
timeline=[Segment(0.0, 2.0, "keep")],
|
|
)
|
|
|
|
plan = cadence.build_render_graph(td, jl_mode="off", jl_frames=0)
|
|
|
|
assert plan.voice_stream == 0
|
|
assert plan.system_stream == 1
|
|
assert plan.audio_label == "[aout]"
|
|
assert "[0:a:0]" in plan.filtergraph
|
|
assert "[0:a:1]" in plan.filtergraph
|
|
assert "channel_layouts=stereo" in plan.filtergraph
|
|
assert "amix=inputs=2:duration=longest:normalize=0" in plan.filtergraph
|
|
|
|
|
|
def test_render_graph_system_stream_swaps_with_voice(make_track):
|
|
td = make_track(
|
|
duration=2.0,
|
|
audio_stream_count=2,
|
|
audio_track_idx=1,
|
|
timeline=[Segment(0.0, 2.0, "keep")],
|
|
)
|
|
|
|
plan = cadence.build_render_graph(td, jl_mode="off", jl_frames=0)
|
|
|
|
assert plan.voice_stream == 1
|
|
assert plan.system_stream == 0
|
|
|
|
|
|
def test_render_graph_honors_jl_shifts(make_track):
|
|
td = make_track(
|
|
duration=6.0,
|
|
fps_num=30,
|
|
fps_den=1,
|
|
timeline=[
|
|
Segment(0.0, 1.0, "keep"),
|
|
Segment(1.0, 3.0, "drop", reason="silence"),
|
|
Segment(3.0, 4.0, "keep"),
|
|
Segment(4.0, 5.0, "drop", reason="silence"),
|
|
Segment(5.0, 6.0, "keep"),
|
|
],
|
|
)
|
|
|
|
plan = cadence.build_render_graph(td, jl_mode="j", jl_frames=2)
|
|
|
|
# Each segment's video in-point inherits the *incoming* shift; the last
|
|
# segment's outgoing shift is always 0.
|
|
assert (
|
|
"select='((between(n,0,31)+between(n,92,121))+between(n,152,179))'"
|
|
in plan.filtergraph
|
|
)
|
|
# J/L only redistributes frames, so total length is unchanged.
|
|
assert plan.total_frames == 90
|
|
assert plan.duration == pytest.approx(3.0)
|
|
|
|
|
|
def test_render_graph_requires_a_keep_segment(make_track):
|
|
td = make_track(
|
|
duration=2.0,
|
|
timeline=[Segment(0.0, 2.0, "drop", reason="silence")],
|
|
)
|
|
with pytest.raises(ValueError):
|
|
cadence.build_render_graph(td)
|
|
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# command assembly
|
|
# ---------------------------------------------------------------------------
|
|
|
|
def test_build_render_command_uses_exact_export_settings(make_track, tmp_path):
|
|
td = make_track(
|
|
duration=1.0,
|
|
timeline=[Segment(0.0, 1.0, "keep")],
|
|
)
|
|
plan = cadence.build_render_graph(td, jl_mode="off", jl_frames=0)
|
|
out = tmp_path / "edited.mp4"
|
|
|
|
cmd = cadence.build_render_command(plan, td.video, out)
|
|
|
|
assert cmd[0] == "ffmpeg"
|
|
assert "-filter_complex" in cmd
|
|
assert "-filter_complex_script" not in cmd
|
|
assert cmd[cmd.index("-map") + 1] == "[vout]"
|
|
assert cmd[cmd.index("-r") + 1] == "30/1"
|
|
for flag in ("-c:v", "libx264", "-preset", "veryfast", "-crf", "18", "-g", "15"):
|
|
assert flag in cmd
|
|
for flag in ("-pix_fmt", "yuv420p", "-c:a", "aac", "-b:a", "160k", "-ac", "2"):
|
|
assert flag in cmd
|
|
for flag in ("-movflags", "+faststart", "-f", "mp4", "-threads", "0"):
|
|
assert flag in cmd
|
|
assert cmd[-1] == str(out)
|
|
|
|
|
|
def test_build_render_command_filter_script(make_track, tmp_path):
|
|
td = make_track(duration=1.0, timeline=[Segment(0.0, 1.0, "keep")])
|
|
plan = cadence.build_render_graph(td)
|
|
script = tmp_path / "graph.txt"
|
|
|
|
cmd = cadence.build_render_command(plan, td.video, tmp_path / "o.mp4", filter_script=script)
|
|
|
|
assert "-filter_complex_script" in cmd
|
|
assert str(script) in cmd
|
|
assert "-filter_complex" not in cmd
|
|
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# parse_args / output resolution
|
|
# ---------------------------------------------------------------------------
|
|
|
|
def _parse(monkeypatch, argv):
|
|
monkeypatch.setattr("sys.argv", ["cadence", *argv])
|
|
return cadence.parse_args()
|
|
|
|
|
|
def test_parse_args_render_flags(monkeypatch):
|
|
args = _parse(monkeypatch, ["video.mp4"])
|
|
assert args.render is False
|
|
assert args.keep_project is False
|
|
|
|
args = _parse(monkeypatch, ["video.mp4", "--render", "--keep-project"])
|
|
assert args.render is True
|
|
assert args.keep_project is True
|
|
|
|
|
|
def test_resolve_output_path_render_default_is_mp4(tmp_path):
|
|
video = tmp_path / "recording.mkv"
|
|
assert (
|
|
cadence.resolve_output_path([video], None, render=True)
|
|
== tmp_path / "recording.mp4"
|
|
)
|
|
# Unchanged historical default without --render.
|
|
assert (
|
|
cadence.resolve_output_path([video], None, render=False)
|
|
== tmp_path / "recording.kdenlive"
|
|
)
|
|
|
|
|
|
def test_resolve_output_path_render_forces_mp4(tmp_path):
|
|
video = tmp_path / "recording.mkv"
|
|
explicit = tmp_path / "custom.kdenlive"
|
|
assert (
|
|
cadence.resolve_output_path([video], explicit, render=True)
|
|
== tmp_path / "custom.mp4"
|
|
)
|
|
assert cadence.resolve_output_path([video], explicit, render=False) == explicit
|
|
|
|
|
|
def test_resolve_output_path_multi_input_concat(tmp_path):
|
|
first = tmp_path / "a.mp4"
|
|
second = tmp_path / "b.mp4"
|
|
assert (
|
|
cadence.resolve_output_path([first, second], None, render=False)
|
|
== tmp_path / "a_concat.kdenlive"
|
|
)
|
|
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# handoff integration
|
|
# ---------------------------------------------------------------------------
|
|
|
|
def test_sha256_file(tmp_path):
|
|
path = tmp_path / "blob.bin"
|
|
path.write_bytes(b"cadence")
|
|
assert cadence.sha256_file(path) == hashlib.sha256(b"cadence").hexdigest()
|
|
|
|
|
|
def test_generate_handoff_populates_master(make_track, tmp_path):
|
|
td = make_track(
|
|
duration=1.0,
|
|
timeline=[Segment(0.0, 1.0, "keep")],
|
|
words=[Word("Hi.", 0.1, 0.3)],
|
|
)
|
|
out = tmp_path / "recording.mp4"
|
|
master = {"path": str(out), "sha256": "deadbeef"}
|
|
|
|
cadence.generate_handoff([td], out, master=master)
|
|
|
|
handoff = json.loads((tmp_path / "recording.handoff.json").read_text(encoding="utf-8"))
|
|
assert handoff["edited"]["master"] == master
|
|
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# integration (ffmpeg-gated)
|
|
# ---------------------------------------------------------------------------
|
|
|
|
@pytest.mark.skipif(not HAVE_FFMPEG, reason="ffmpeg/ffprobe not available")
|
|
def test_render_video_end_to_end(tmp_path):
|
|
src = tmp_path / "src.mp4"
|
|
subprocess.run(
|
|
[
|
|
"ffmpeg", "-y", "-loglevel", "error",
|
|
"-f", "lavfi", "-i", "testsrc=duration=2:size=320x240:rate=30",
|
|
"-f", "lavfi", "-i", "sine=frequency=440:sample_rate=48000:duration=2",
|
|
"-c:v", "libx264", "-pix_fmt", "yuv420p",
|
|
"-c:a", "aac", "-b:a", "160k",
|
|
"-shortest", str(src),
|
|
],
|
|
check=True,
|
|
)
|
|
|
|
td = cadence.VideoTrackData(
|
|
video=src,
|
|
duration=2.0,
|
|
fps_num=30,
|
|
fps_den=1,
|
|
width=320,
|
|
height=240,
|
|
audio_track_idx=0,
|
|
audio_stream_count=1,
|
|
words=[],
|
|
timeline=[
|
|
Segment(0.0, 1.0, "keep"),
|
|
Segment(1.0, 1.5, "drop", reason="silence"),
|
|
Segment(1.5, 2.0, "keep"),
|
|
],
|
|
)
|
|
out = tmp_path / "edited.mp4"
|
|
|
|
cadence.render_video(td, out, jl_mode="off", jl_frames=0)
|
|
|
|
assert out.exists()
|
|
assert out.stat().st_size > 0
|
|
assert cadence.probe_duration(out) == pytest.approx(1.5, abs=0.15)
|
|
|
|
raw = cadence.check_ff_output(
|
|
[
|
|
"ffprobe", "-v", "error", "-select_streams", "a",
|
|
"-show_entries", "stream=codec_name,channels",
|
|
"-of", "json", str(out),
|
|
],
|
|
"probe rendered audio",
|
|
)
|
|
streams = json.loads(raw)["streams"]
|
|
assert len(streams) == 1
|
|
assert streams[0]["codec_name"] == "aac"
|
|
assert streams[0]["channels"] == 2
|