Skip to content

Commit

Permalink
FfmpegOutput was not stopping when there was an audio stream
Browse files Browse the repository at this point in the history
ffmpeg.wait() hangs indefinitely if there's an audio stream because no
one is stopping it. Instead, give the video a few moments to flush
itself out and then use ffmpeg.terminate().

Signed-off-by: David Plowman <[email protected]>
  • Loading branch information
davidplowman committed Oct 19, 2023
1 parent 792431a commit e5cd4b5
Showing 1 changed file with 12 additions and 1 deletion.
13 changes: 12 additions & 1 deletion picamera2/outputs/ffmpegoutput.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ def __init__(self, output_filename, audio=False, audio_device="default", audio_s
self.audio_samplerate = audio_samplerate
self.audio_codec = audio_codec
self.audio_bitrate = audio_bitrate
# If we run an audio stream, FFmpeg won't stop so we'll give the video stream a
# moment or two to flush stuff out, and then we'll have to terminate the process.
self.timeout = 1 if audio else None

def start(self):
general_options = ['-loglevel', 'warning',
Expand Down Expand Up @@ -76,7 +79,15 @@ def stop(self):
super().stop()
if self.ffmpeg is not None:
self.ffmpeg.stdin.close() # FFmpeg needs this to shut down tidily
self.ffmpeg.wait()
try:
# Give it a moment to flush out video frames, but after that make sure we terminate it.
self.ffmpeg.wait(timeout=self.timeout)
except subprocess.TimeoutExpired:
# We'll always end up here when there was an audio strema. Ignore any further errors.
try:
self.ffmpeg.terminate()
except Exception:
pass
self.ffmpeg = None

def outputframe(self, frame, keyframe=True, timestamp=None):
Expand Down

0 comments on commit e5cd4b5

Please sign in to comment.