feat: add volume normalization using FFmpeg loudnorm filter
Co-authored-by: aider (gemini/gemini-3.1-pro-preview) <aider@aider.chat>
This commit is contained in:
@@ -26,6 +26,18 @@ def playback_thread(file_path, state):
|
||||
stream = container.streams.audio[0]
|
||||
resampler = av.AudioResampler(format='s16', layout='stereo', rate=48000)
|
||||
|
||||
def build_graph():
|
||||
graph = av.filter.Graph()
|
||||
src = graph.add_abuffer(template=stream)
|
||||
loudnorm = graph.add("loudnorm", "I=-16:TP=-1.5:LRA=11")
|
||||
sink = graph.add("abuffersink")
|
||||
src.link_to(loudnorm)
|
||||
loudnorm.link_to(sink)
|
||||
graph.configure()
|
||||
return graph
|
||||
|
||||
graph = build_graph()
|
||||
|
||||
with open(fifo_path, 'wb') as fifo:
|
||||
iterator = container.decode(stream)
|
||||
while state.running:
|
||||
@@ -41,6 +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
|
||||
continue
|
||||
|
||||
if paused:
|
||||
@@ -57,7 +70,14 @@ def playback_thread(file_path, state):
|
||||
with state.lock:
|
||||
state.time_pos = float(frame.pts * stream.time_base)
|
||||
|
||||
resampled_frames = resampler.resample(frame)
|
||||
graph.push(frame)
|
||||
while True:
|
||||
try:
|
||||
filtered_frame = graph.pull()
|
||||
except (av.AVError, BlockingIOError):
|
||||
break
|
||||
|
||||
resampled_frames = resampler.resample(filtered_frame)
|
||||
for r_frame in resampled_frames:
|
||||
arr = r_frame.to_ndarray()
|
||||
|
||||
|
||||
Reference in New Issue
Block a user