From 0c29c059adbda17d33bb519cadde1c50d01e5142 Mon Sep 17 00:00:00 2001 From: Nicolas Mowen Date: Wed, 29 May 2024 07:05:23 -0600 Subject: [PATCH 1/3] Cache camera stream info to speed up future config generations --- frigate/config.py | 10 ++++++---- frigate/util/config.py | 17 +++++++++++++++++ 2 files changed, 23 insertions(+), 4 deletions(-) diff --git a/frigate/config.py b/frigate/config.py index 5cf8b2da48..be1cddf002 100644 --- a/frigate/config.py +++ b/frigate/config.py @@ -1,6 +1,5 @@ from __future__ import annotations -import asyncio import json import logging import os @@ -46,9 +45,9 @@ get_ffmpeg_arg_list, load_config_with_no_duplicates, ) -from frigate.util.config import get_relative_coordinates +from frigate.util.config import StreamInfoRetriever, get_relative_coordinates from frigate.util.image import create_mask -from frigate.util.services import auto_detect_hwaccel, get_video_properties +from frigate.util.services import auto_detect_hwaccel logger = logging.getLogger(__name__) @@ -73,6 +72,9 @@ DEFAULT_DETECT_DIMENSIONS = {"width": 1280, "height": 720} DEFAULT_TIME_LAPSE_FFMPEG_ARGS = "-vf setpts=0.04*PTS -r 30" +# stream info handler +stream_info_retriever = StreamInfoRetriever() + class FrigateBaseModel(BaseModel): model_config = ConfigDict(extra="forbid", protected_namespaces=()) @@ -1416,7 +1418,7 @@ def runtime_config(self, plus_api: PlusApi = None) -> FrigateConfig: if need_detect_dimensions or need_record_fourcc: stream_info = {"width": 0, "height": 0, "fourcc": None} try: - stream_info = asyncio.run(get_video_properties(input.path)) + stream_info = stream_info_retriever.get_resolution(input.path) except Exception: logger.warn( f"Error detecting stream parameters automatically for {input.path} Applying default values." diff --git a/frigate/util/config.py b/frigate/util/config.py index df2d0c1f1b..eee037a87b 100644 --- a/frigate/util/config.py +++ b/frigate/util/config.py @@ -1,5 +1,6 @@ """configuration utils.""" +import asyncio import logging import os import shutil @@ -8,6 +9,7 @@ from ruamel.yaml import YAML from frigate.const import CONFIG_DIR, EXPORT_DIR +from frigate.util.services import get_video_properties logger = logging.getLogger(__name__) @@ -194,3 +196,18 @@ def get_relative_coordinates( return mask return mask + +class StreamInfoRetriever(): + + def __init__(self) -> None: + self.stream_cache: dict[str, tuple[int, int]] = {} + + def get_resolution(self, path: str) -> str: + if path in self.stream_cache: + return self.stream_cache[path] + + info = asyncio.run(get_video_properties(input.path)) + self.stream_cache[path] = info + return info + + From dd502cdfdc70281773b4ef86512cdb07cc1a33be Mon Sep 17 00:00:00 2001 From: Nicolas Mowen Date: Wed, 29 May 2024 07:09:39 -0600 Subject: [PATCH 2/3] Formatting --- frigate/util/config.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/frigate/util/config.py b/frigate/util/config.py index eee037a87b..2e66854a25 100644 --- a/frigate/util/config.py +++ b/frigate/util/config.py @@ -197,8 +197,8 @@ def get_relative_coordinates( return mask -class StreamInfoRetriever(): +class StreamInfoRetriever: def __init__(self) -> None: self.stream_cache: dict[str, tuple[int, int]] = {} @@ -209,5 +209,3 @@ def get_resolution(self, path: str) -> str: info = asyncio.run(get_video_properties(input.path)) self.stream_cache[path] = info return info - - From 6b031d33bfcadfe133376032635b05112eb6bd77 Mon Sep 17 00:00:00 2001 From: Nicolas Mowen Date: Wed, 29 May 2024 07:13:37 -0600 Subject: [PATCH 3/3] fix --- frigate/config.py | 2 +- frigate/util/config.py | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/frigate/config.py b/frigate/config.py index be1cddf002..6d0342d983 100644 --- a/frigate/config.py +++ b/frigate/config.py @@ -1418,7 +1418,7 @@ def runtime_config(self, plus_api: PlusApi = None) -> FrigateConfig: if need_detect_dimensions or need_record_fourcc: stream_info = {"width": 0, "height": 0, "fourcc": None} try: - stream_info = stream_info_retriever.get_resolution(input.path) + stream_info = stream_info_retriever.get_stream_info(input.path) except Exception: logger.warn( f"Error detecting stream parameters automatically for {input.path} Applying default values." diff --git a/frigate/util/config.py b/frigate/util/config.py index 2e66854a25..6f66a59922 100644 --- a/frigate/util/config.py +++ b/frigate/util/config.py @@ -202,10 +202,10 @@ class StreamInfoRetriever: def __init__(self) -> None: self.stream_cache: dict[str, tuple[int, int]] = {} - def get_resolution(self, path: str) -> str: + def get_stream_info(self, path: str) -> str: if path in self.stream_cache: return self.stream_cache[path] - info = asyncio.run(get_video_properties(input.path)) + info = asyncio.run(get_video_properties(path)) self.stream_cache[path] = info return info