From 150333b0cac46ea5e17ee8bebc6559bfcd8d5a7f Mon Sep 17 00:00:00 2001 From: Raphael Hehl <7577984+RaHehl@users.noreply.github.com> Date: Mon, 16 Nov 2026 15:07:44 +0100 Subject: [PATCH] Put the audio output of the ffmpeg source provider first With ffmpeg 6.1 the video RTP output dies with an I/O error after a few hundred packets when it comes before the audio output, while ffmpeg keeps running, so the session gets no video. Audio first avoids it. --- pyhap/media/ffmpeg.py | 14 +++++++++----- tests/test_media_ffmpeg_provider.py | 7 ++++--- 2 files changed, 13 insertions(+), 8 deletions(-) diff --git a/pyhap/media/ffmpeg.py b/pyhap/media/ffmpeg.py index c122957f..dddd8471 100644 --- a/pyhap/media/ffmpeg.py +++ b/pyhap/media/ffmpeg.py @@ -211,11 +211,9 @@ def command( *(self.ffmpeg, "-hide_banner", "-nostdin", "-loglevel", "warning"), *stats, *ffmpeg_input.args, - *("-map", video_map, "-c:v", "copy", "-bsf:v", "dump_extra"), - *("-payload_type", str(hksv3.VIDEO_PAYLOAD_TYPE)), - # The source drops RTCP, and port + 1 may be the audio input. - *("-rtpflags", "skip_rtcp", "-f", "rtp", f"rtp://{host}:{video_port}"), ] + # The audio output comes first: after the video output, ffmpeg 6.1 + # gives up on the video output with an I/O error and runs on. if audio_port: audio_map = ffmpeg_input.audio_map or self.audio_map command += [ @@ -225,7 +223,13 @@ def command( *("-payload_type", str(hksv3.AUDIO_PAYLOAD_TYPE)), *("-rtpflags", "skip_rtcp", "-f", "rtp", f"rtp://{host}:{audio_port}"), ] - return command + return [ + *command, + *("-map", video_map, "-c:v", "copy", "-bsf:v", "dump_extra"), + *("-payload_type", str(hksv3.VIDEO_PAYLOAD_TYPE)), + # The source drops RTCP, and port + 1 may be the audio input. + *("-rtpflags", "skip_rtcp", "-f", "rtp", f"rtp://{host}:{video_port}"), + ] async def __call__( self, diff --git a/tests/test_media_ffmpeg_provider.py b/tests/test_media_ffmpeg_provider.py index c11a85c3..e5ecab56 100644 --- a/tests/test_media_ffmpeg_provider.py +++ b/tests/test_media_ffmpeg_provider.py @@ -116,13 +116,14 @@ def test_ffmpeg_command() -> None: assert command == [ *("/bin/ffmpeg", "-hide_banner", "-nostdin", "-loglevel", "warning"), *("-stats", "-stats_period", "10", "-i", "cam"), - *("-map", "0:v:1", "-c:v", "copy", "-bsf:v", "dump_extra"), - *("-payload_type", "99", "-rtpflags", "skip_rtcp"), - *("-f", "rtp", "rtp://127.0.0.1:6000"), + # ffmpeg 6.1 loses the video output when it comes before the audio one. *("-map", "0:a:2", "-c:a", "libopus", "-ac", "1", "-ar", "48000"), *("-b:a", "24000", "-application", "lowdelay", "-frame_duration", "20"), *("-payload_type", "110", "-rtpflags", "skip_rtcp"), *("-f", "rtp", "rtp://127.0.0.1:6002"), + *("-map", "0:v:1", "-c:v", "copy", "-bsf:v", "dump_extra"), + *("-payload_type", "99", "-rtpflags", "skip_rtcp"), + *("-f", "rtp", "rtp://127.0.0.1:6000"), ]