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
+16 -5
View File
@@ -26,9 +26,9 @@ def playback_thread(file_path, state):
stream = container.streams.audio[0]
resampler = av.AudioResampler(format='s16', layout='stereo', rate=48000)
def build_graph():
def build_graph(template_frame):
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")
sink = graph.add("abuffersink")
src.link_to(loudnorm)
@@ -36,7 +36,7 @@ def playback_thread(file_path, state):
graph.configure()
return graph
graph = build_graph()
graph = None
with open(fifo_path, 'wb') as fifo:
iterator = container.decode(stream)
@@ -53,7 +53,7 @@ def playback_thread(file_path, state):
with state.lock:
state.seek_request = None
iterator = container.decode(stream)
graph = build_graph() # Reset filter graph on seek
graph = None # Reset filter graph on seek
continue
if paused:
@@ -70,7 +70,18 @@ def playback_thread(file_path, state):
with state.lock:
state.time_pos = float(frame.pts * stream.time_base)
graph.push(frame)
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)
except av.AVError:
continue
while True:
try:
filtered_frame = graph.pull()