From c100dada9359eed4075e0a7cbd5fa353c3d3f939 Mon Sep 17 00:00:00 2001 From: Tanner Date: Tue, 28 Jul 2026 17:10:29 -0600 Subject: [PATCH] fix: delay filter graph creation until first frame is decoded Co-authored-by: aider (gemini/gemini-3.1-pro-preview) --- main.py | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) diff --git a/main.py b/main.py index 986a9dc..d3bd04e 100644 --- a/main.py +++ b/main.py @@ -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()