Compare commits
No commits in common. "d60f65d960580923e2e8019e1cd581f3efd44c65" and "d5943affe79c26db2a123a1abb64a2e971ba28f2" have entirely different histories.
d60f65d960
...
d5943affe7
2 changed files with 14 additions and 72 deletions
43
cadence.py
43
cadence.py
|
|
@ -55,7 +55,7 @@ from typing import Literal
|
||||||
|
|
||||||
logger = logging.getLogger("cadence")
|
logger = logging.getLogger("cadence")
|
||||||
|
|
||||||
__version__ = "0.2.1"
|
__version__ = "0.2.0"
|
||||||
|
|
||||||
# Regex to catch elongated sounds like 'aaaa', 'uhhh', 'ummm', 'mmmm', 'eeeh', etc.
|
# Regex to catch elongated sounds like 'aaaa', 'uhhh', 'ummm', 'mmmm', 'eeeh', etc.
|
||||||
HESITATION_REGEX = re.compile(
|
HESITATION_REGEX = re.compile(
|
||||||
|
|
@ -769,33 +769,14 @@ class RenderPlan:
|
||||||
return self.total_frames / self.fps
|
return self.total_frames / self.fps
|
||||||
|
|
||||||
|
|
||||||
def _balanced_sum(terms: list[str]) -> str:
|
|
||||||
"""Fold ``terms`` into a balanced binary tree of ``+``.
|
|
||||||
|
|
||||||
ffmpeg's expression parser recurses on the left-associative ``a+b+c+...``
|
|
||||||
chain and overflows past ~110 terms ("Error while parsing expression", then
|
|
||||||
"Cannot allocate memory"): a 34-minute cut list has ~600. The identical sum
|
|
||||||
grouped pairwise keeps the parser depth at log2(n), so long keep timelines
|
|
||||||
render instead of failing.
|
|
||||||
"""
|
|
||||||
level = list(terms)
|
|
||||||
while len(level) > 1:
|
|
||||||
level = [
|
|
||||||
f"({level[i]}+{level[i + 1]})" if i + 1 < len(level) else level[i]
|
|
||||||
for i in range(0, len(level), 2)
|
|
||||||
]
|
|
||||||
return level[0] if level else "0"
|
|
||||||
|
|
||||||
|
|
||||||
def build_video_select_expression(ranges: list[tuple[int, int]]) -> str:
|
def build_video_select_expression(ranges: list[tuple[int, int]]) -> str:
|
||||||
"""Inclusive source-frame ranges -> ffmpeg ``select`` expression.
|
"""Inclusive source-frame ranges -> ffmpeg ``select`` expression.
|
||||||
|
|
||||||
Uses the decoder frame counter ``n`` so cuts land exactly on frame
|
Uses the decoder frame counter ``n`` so cuts land exactly on frame
|
||||||
boundaries regardless of floating-point timestamps. Terms are summed as a
|
boundaries regardless of floating-point timestamps.
|
||||||
balanced tree (see ``_balanced_sum``) so long timelines do not overflow the
|
|
||||||
expression parser.
|
|
||||||
"""
|
"""
|
||||||
return _balanced_sum([f"between(n,{start},{end})" for start, end in ranges])
|
terms = [f"between(n,{start},{end})" for start, end in ranges]
|
||||||
|
return "+".join(terms) if terms else "0"
|
||||||
|
|
||||||
|
|
||||||
def build_audio_select_expression(ranges: list[tuple[int, int]], fps: float) -> str:
|
def build_audio_select_expression(ranges: list[tuple[int, int]], fps: float) -> str:
|
||||||
|
|
@ -803,16 +784,14 @@ def build_audio_select_expression(ranges: list[tuple[int, int]], fps: float) ->
|
||||||
|
|
||||||
Audio has no frame counter, so boundaries become half-open time intervals
|
Audio has no frame counter, so boundaries become half-open time intervals
|
||||||
(a frame's samples span ``[start/fps, (end+1)/fps)``), keeping every audio
|
(a frame's samples span ``[start/fps, (end+1)/fps)``), keeping every audio
|
||||||
segment aligned to a video frame boundary without cumulative drift. Terms
|
segment aligned to a video frame boundary without cumulative drift.
|
||||||
are summed as a balanced tree (see ``_balanced_sum``).
|
|
||||||
"""
|
"""
|
||||||
return _balanced_sum(
|
terms = []
|
||||||
[
|
for start, end in ranges:
|
||||||
f"gte(t,{start / fps:.9f})*lt(t,{(end + 1) / fps:.9f})"
|
t0 = start / fps
|
||||||
for start, end in ranges
|
t1 = (end + 1) / fps
|
||||||
]
|
terms.append(f"gte(t,{t0:.9f})*lt(t,{t1:.9f})")
|
||||||
)
|
return "+".join(terms) if terms else "0"
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
def _keep_frame_ranges(
|
def _keep_frame_ranges(
|
||||||
|
|
|
||||||
|
|
@ -27,19 +27,7 @@ def test_video_select_expression_ranges():
|
||||||
assert cadence.build_video_select_expression([]) == "0"
|
assert cadence.build_video_select_expression([]) == "0"
|
||||||
assert (
|
assert (
|
||||||
cadence.build_video_select_expression([(0, 29), (45, 59)])
|
cadence.build_video_select_expression([(0, 29), (45, 59)])
|
||||||
== "(between(n,0,29)+between(n,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)))"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -50,31 +38,6 @@ def test_audio_select_expression_is_half_open():
|
||||||
assert cadence.build_audio_select_expression([], 30.0) == "0"
|
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
|
# graph builder
|
||||||
# ---------------------------------------------------------------------------
|
# ---------------------------------------------------------------------------
|
||||||
|
|
@ -98,7 +61,7 @@ def test_render_graph_single_stream_matches_keep_segments(make_track):
|
||||||
assert plan.audio_label == "[voice]"
|
assert plan.audio_label == "[voice]"
|
||||||
assert plan.total_frames == 45 # 30 + 15 frames kept
|
assert plan.total_frames == 45 # 30 + 15 frames kept
|
||||||
assert plan.duration == pytest.approx(1.5)
|
assert plan.duration == pytest.approx(1.5)
|
||||||
assert "select='(between(n,0,29)+between(n,45,59))'" in plan.filtergraph
|
assert "select='between(n,0,29)+between(n,45,59)'" in plan.filtergraph
|
||||||
assert "setpts=N/FRAME_RATE/TB" in plan.filtergraph
|
assert "setpts=N/FRAME_RATE/TB" in plan.filtergraph
|
||||||
assert "asetpts=N/SR/TB" in plan.filtergraph
|
assert "asetpts=N/SR/TB" in plan.filtergraph
|
||||||
assert "amix" not in plan.filtergraph
|
assert "amix" not in plan.filtergraph
|
||||||
|
|
@ -156,7 +119,7 @@ def test_render_graph_honors_jl_shifts(make_track):
|
||||||
# Each segment's video in-point inherits the *incoming* shift; the last
|
# Each segment's video in-point inherits the *incoming* shift; the last
|
||||||
# segment's outgoing shift is always 0.
|
# segment's outgoing shift is always 0.
|
||||||
assert (
|
assert (
|
||||||
"select='((between(n,0,31)+between(n,92,121))+between(n,152,179))'"
|
"select='between(n,0,31)+between(n,92,121)+between(n,152,179)'"
|
||||||
in plan.filtergraph
|
in plan.filtergraph
|
||||||
)
|
)
|
||||||
# J/L only redistributes frames, so total length is unchanged.
|
# J/L only redistributes frames, so total length is unchanged.
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue