From 1b5b4219c71151898fbc798216c8aeec669cdd1d Mon Sep 17 00:00:00 2001 From: Nick Mowen Date: Sat, 14 Jan 2023 15:04:43 -0700 Subject: [PATCH] Add audio encoding field and simplify stream creation --- frigate/config.py | 16 +++++++++++++--- frigate/restream.py | 35 ++++++++++++++++++++++------------- 2 files changed, 35 insertions(+), 16 deletions(-) diff --git a/frigate/config.py b/frigate/config.py index 44c1472e1..bcf5e320c 100644 --- a/frigate/config.py +++ b/frigate/config.py @@ -524,16 +524,26 @@ class JsmpegStreamConfig(FrigateBaseModel): quality: int = Field(default=8, ge=1, le=31, title="Live camera view quality.") -class RestreamCodecEnum(str, Enum): +class RestreamVideoCodecEnum(str, Enum): copy = "copy" h264 = "h264" h265 = "h265" +class RestreamAudioCodecEnum(str, Enum): + aac = "aac" + copy = "copy" + opus = "opus" + + class RestreamConfig(FrigateBaseModel): enabled: bool = Field(default=True, title="Restreaming enabled.") - video_encoding: RestreamCodecEnum = Field( - default=RestreamCodecEnum.copy, title="Method for encoding the restream." + audio_encoding: list[RestreamAudioCodecEnum] = Field( + default=[RestreamAudioCodecEnum.aac, RestreamAudioCodecEnum.opus], + title="Codecs to supply for audio.", + ) + video_encoding: RestreamVideoCodecEnum = Field( + default=RestreamVideoCodecEnum.copy, title="Method for encoding the restream." ) force_audio: bool = Field( default=True, title="Force audio compatibility with the browser." diff --git a/frigate/restream.py b/frigate/restream.py index 0f99d4a8a..25eb3449c 100644 --- a/frigate/restream.py +++ b/frigate/restream.py @@ -6,7 +6,7 @@ import requests from typing import Optional -from frigate.config import FrigateConfig, RestreamCodecEnum +from frigate.config import FrigateConfig, RestreamAudioCodecEnum, RestreamVideoCodecEnum from frigate.const import BIRDSEYE_PIPE from frigate.ffmpeg_presets import ( parse_preset_hardware_acceleration_encode, @@ -18,18 +18,26 @@ logger = logging.getLogger(__name__) def get_manual_go2rtc_stream( - camera_url: str, codec: RestreamCodecEnum, engine: Optional[str] + camera_url: str, + aCodecs: list[RestreamAudioCodecEnum], + vCodec: RestreamVideoCodecEnum, + engine: Optional[str], ) -> str: """Get a manual stream for go2rtc.""" - if codec == RestreamCodecEnum.copy: - return f"ffmpeg:{camera_url}#video=copy#audio=aac#audio=opus" + stream = f"ffmpeg:{camera_url}" - if engine: - return ( - f"ffmpeg:{camera_url}#video={codec}#hardware={engine}#audio=aac#audio=opus" - ) + for aCodec in aCodecs: + stream += f"#audio={aCodec}" - return f"ffmpeg:{camera_url}#video={codec}#audio=aac#audio=opus" + if vCodec == RestreamVideoCodecEnum.copy: + stream += "#video=copy" + else: + stream += f"#video={vCodec}" + + if engine: + stream += f"#hardware={engine}" + + return stream class RestreamApi: @@ -48,10 +56,11 @@ class RestreamApi: for input in camera.ffmpeg.inputs: if "restream" in input.roles: - if ( - input.path.startswith("rtsp") - and not camera.restream.force_audio - ): + if input.path.startswith( + "rtsp" + ) and not camera.restream.audio_encoding == [ + RestreamAudioCodecEnum.copy + ]: self.relays[ cam_name ] = f"{escape_special_characters(input.path)}#backchannel=0"