fix: delay filter graph creation until first frame is decoded

Co-authored-by: aider (gemini/gemini-3.1-pro-preview) <aider@aider.chat>
This commit is contained in:
Tanner
2026-07-28 17:10:29 -06:00
parent 4b0774b3fa
commit c100dada93
+15 -4
View File
@@ -26,9 +26,9 @@ def playback_thread(file_path, state):
stream = container.streams.audio[0] stream = container.streams.audio[0]
resampler = av.AudioResampler(format='s16', layout='stereo', rate=48000) resampler = av.AudioResampler(format='s16', layout='stereo', rate=48000)
def build_graph(): def build_graph(template_frame):
graph = av.filter.Graph() graph = av.filter.Graph()
src = graph.add_abuffer(template=stream) src = graph.add_abuffer(template=template_frame)
loudnorm = graph.add("loudnorm", "I=-16:TP=-1.5:LRA=11") loudnorm = graph.add("loudnorm", "I=-16:TP=-1.5:LRA=11")
sink = graph.add("abuffersink") sink = graph.add("abuffersink")
src.link_to(loudnorm) src.link_to(loudnorm)
@@ -36,7 +36,7 @@ def playback_thread(file_path, state):
graph.configure() graph.configure()
return graph return graph
graph = build_graph() graph = None
with open(fifo_path, 'wb') as fifo: with open(fifo_path, 'wb') as fifo:
iterator = container.decode(stream) iterator = container.decode(stream)
@@ -53,7 +53,7 @@ def playback_thread(file_path, state):
with state.lock: with state.lock:
state.seek_request = None state.seek_request = None
iterator = container.decode(stream) iterator = container.decode(stream)
graph = build_graph() # Reset filter graph on seek graph = None # Reset filter graph on seek
continue continue
if paused: if paused:
@@ -70,7 +70,18 @@ def playback_thread(file_path, state):
with state.lock: with state.lock:
state.time_pos = float(frame.pts * stream.time_base) state.time_pos = float(frame.pts * stream.time_base)
if graph is None:
try:
graph = build_graph(frame)
except Exception as e:
print(f"Filter graph error: {e}", file=sys.stderr)
break
try:
graph.push(frame) graph.push(frame)
except av.AVError:
continue
while True: while True:
try: try:
filtered_frame = graph.pull() filtered_frame = graph.pull()