From d77e75180d0fa28c243ce59aed93a47f378f6364 Mon Sep 17 00:00:00 2001 From: StephenGrable1 Date: Sat, 25 May 2024 15:21:47 -0400 Subject: [PATCH] add ffmpeg audio filter support enable -af flag pass through Signed-off-by: Stephen Grable --- CHANGELOG.md | 1 + examples/ffmpeg_audio_filter.py | 26 ++++++++++++++++++++++++++ picamera2/outputs/ffmpegoutput.py | 5 ++++- 3 files changed, 31 insertions(+), 1 deletion(-) create mode 100644 examples/ffmpeg_audio_filter.py diff --git a/CHANGELOG.md b/CHANGELOG.md index 125730ac..2d8755e4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,7 @@ ## Unreleased (on "next" branch) ### Added +* FfmpegOutput support custom audio filter ### Changed diff --git a/examples/ffmpeg_audio_filter.py b/examples/ffmpeg_audio_filter.py new file mode 100644 index 00000000..e1106915 --- /dev/null +++ b/examples/ffmpeg_audio_filter.py @@ -0,0 +1,26 @@ +#!/usr/bin/python3 +import time + +from picamera2 import Picamera2 +from picamera2.encoders import H264Encoder +from picamera2.outputs import FfmpegOutput + +picam2 = Picamera2() +video_config = picam2.create_video_configuration() +picam2.configure(video_config) +encoder = H264Encoder(10000000) + +# audio filter takes the left channel and copies it to the right channel +# below example copies c0 (left channel) to c1 (right channel) - convert mono to stereo + +# or add audio delay on left channel like this: audio_filter="pan=stereo|adelay=1500|0" +# source for more examples: https://ffmpeg.org/ffmpeg-filters.html#Examples-2 +output = FfmpegOutput( + 'ffmpeg_audio_filter_test.mp4', + audio=True, + audio_filter="pan=stereo|c0=c0|c1=c0" +) + +picam2.start_recording(encoder, output) +time.sleep(10) +picam2.stop_recording() diff --git a/picamera2/outputs/ffmpegoutput.py b/picamera2/outputs/ffmpegoutput.py index ac431530..1f35c763 100644 --- a/picamera2/outputs/ffmpegoutput.py +++ b/picamera2/outputs/ffmpegoutput.py @@ -34,12 +34,13 @@ class FfmpegOutput(Output): """ def __init__(self, output_filename, audio=False, audio_device="default", audio_sync=-0.3, - audio_samplerate=48000, audio_codec="aac", audio_bitrate=128000, pts=None): + audio_samplerate=48000, audio_codec="aac", audio_bitrate=128000, audio_filter=None, pts=None): super().__init__(pts=pts) self.ffmpeg = None self.output_filename = output_filename self.audio = audio self.audio_device = audio_device + self.audio_filter = audio_filter self.audio_sync = audio_sync self.audio_samplerate = audio_samplerate self.audio_codec = audio_codec @@ -72,6 +73,8 @@ def start(self): '-i', self.audio_device] audio_codec = ['-b:a', str(self.audio_bitrate), '-c:a', self.audio_codec] + if self.audio_filter: # Check if audio_filter is not empty or None + audio_codec.extend(['-af', self.audio_filter]) command = ['ffmpeg'] + general_options + audio_input + video_input + \ audio_codec + video_codec + self.output_filename.split()