Skip to content

Commit

Permalink
add ffmpeg audio filter support enable -af flag pass through
Browse files Browse the repository at this point in the history
Signed-off-by: Stephen Grable  <stephengrable@gmail.com>
  • Loading branch information
StephenGrable1 committed Nov 19, 2024
1 parent a89eb1d commit 12ac8a5
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
## Unreleased (on "next" branch)

### Added
* FfmpegOutput support custom audio filter

### Changed

Expand Down
26 changes: 26 additions & 0 deletions examples/ffmpeg_audio_filter.py
Original file line number Diff line number Diff line change
@@ -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()
6 changes: 4 additions & 2 deletions picamera2/outputs/ffmpegoutput.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,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='pan=stereo', 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
Expand Down Expand Up @@ -70,7 +71,8 @@ def start(self):
'-thread_queue_size', '1024', # necessary to prevent warnings
'-i', self.audio_device]
audio_codec = ['-b:a', str(self.audio_bitrate),
'-c:a', self.audio_codec]
'-c:a', self.audio_codec,
-'af', self.audio_filter]

command = ['ffmpeg'] + general_options + audio_input + video_input + \
audio_codec + video_codec + self.output_filename.split()
Expand Down

0 comments on commit 12ac8a5

Please sign in to comment.