Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cache camera stream info to speed up future config generations #11614

Merged
merged 3 commits into from
May 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 6 additions & 4 deletions frigate/config.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
from __future__ import annotations

import asyncio
import json
import logging
import os
Expand Down Expand Up @@ -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__)

Expand All @@ -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=())
Expand Down Expand Up @@ -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_stream_info(input.path)
except Exception:
logger.warn(
f"Error detecting stream parameters automatically for {input.path} Applying default values."
Expand Down
15 changes: 15 additions & 0 deletions frigate/util/config.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
"""configuration utils."""

import asyncio
import logging
import os
import shutil
Expand All @@ -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__)

Expand Down Expand Up @@ -194,3 +196,16 @@ def get_relative_coordinates(
return mask

return mask


class StreamInfoRetriever:
def __init__(self) -> None:
self.stream_cache: dict[str, tuple[int, int]] = {}

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(path))
self.stream_cache[path] = info
return info