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  <[email protected]>
  • Loading branch information
StephenGrable1 committed Nov 19, 2024
1 parent a89eb1d commit 3f09d35
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 1 deletion.
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()
5 changes: 4 additions & 1 deletion picamera2/outputs/ffmpegoutput.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,14 @@ class FfmpegOutput(Output):
audio_samplerate, audio_codec, audio_bitrate - the usual audio parameters.
"""

def __init__(self, output_filename, audio=False, audio_device="default", audio_sync=-0.3,
def __init__(self, output_filename, audio_filter, audio=False, audio_device="default", audio_sync=-0.3,
audio_samplerate=48000, audio_codec="aac", audio_bitrate=128000, 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 @@ -71,6 +72,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()
Expand Down

0 comments on commit 3f09d35

Please sign in to comment.