From 139e1c57dfdf6c34ea07781ee842ebaa1cd35db8 Mon Sep 17 00:00:00 2001 From: Justin Wong <46082645+uvjustin@users.noreply.github.com> Date: Thu, 18 Aug 2022 21:59:09 +0800 Subject: [PATCH] Move probe function to util Update comment --- frigate/http.py | 33 +-------------------------------- frigate/util.py | 34 ++++++++++++++++++++++++++++++++++ 2 files changed, 35 insertions(+), 32 deletions(-) diff --git a/frigate/http.py b/frigate/http.py index 02cf12919..0325a5578 100644 --- a/frigate/http.py +++ b/frigate/http.py @@ -28,6 +28,7 @@ from playhouse.shortcuts import model_to_dict from frigate.const import CLIPS_DIR from frigate.models import Event, Recordings from frigate.stats import stats_snapshot +from frigate.util import get_keyframe_timestamps_and_adjusted_offset from frigate.version import VERSION logger = logging.getLogger(__name__) @@ -825,38 +826,6 @@ def recording_clip(camera, start_ts, end_ts): return response -def get_keyframe_timestamps_and_adjusted_offset( - source: str, target_offset: int -) -> tuple[list[int], int]: - """Get the keyframe timestamp locations for this source. - - Also return the timestamp of the nearest keyframe before start_ts. - This is useful for codec variants with long or variable keyframe intervals.""" - ffprobe_cmd = [ - "ffprobe", - "-skip_frame", - "nokey", - "-select_streams", - "v:0", - "-show_entries", - "frame=pts_time", - "-v", - "quiet", - "-of", - "default=noprint_wrappers=1:nokey=1", - source, - ] - p = sp.run(ffprobe_cmd, capture_output=True) - keyframe_timestamps = [int(1000 * float(pts)) for pts in p.stdout.split()] - if target_offset == 0: - return keyframe_timestamps, 0 - for ts in reversed(keyframe_timestamps): - if ts <= target_offset: - return keyframe_timestamps, ts - logger.warning("Couldn't find starting keyframe for VOD clip") - return keyframe_timestamps, 0 - - @bp.route("/vod//start//end/") @bp.route("/vod//start//end/") def vod_ts(camera, start_ts, end_ts): diff --git a/frigate/util.py b/frigate/util.py index 49bda8620..075edc3a6 100755 --- a/frigate/util.py +++ b/frigate/util.py @@ -690,3 +690,37 @@ class SharedMemoryFrameManager(FrameManager): self.shm_store[name].close() self.shm_store[name].unlink() del self.shm_store[name] + + +def get_keyframe_timestamps_and_adjusted_offset( + source: str, target_offset: int +) -> tuple[list[int], int]: + """Get timestamp metadata from the source. + + This is used to pass information to the VOD module and is useful for codec variants + with long or variable keyframe intervals. + Returns a tuple of: 1) the keyframe timestamp locations for this source + and 2) the timestamp of the nearest keyframe before target_offset.""" + ffprobe_cmd = [ + "ffprobe", + "-skip_frame", + "nokey", + "-select_streams", + "v:0", + "-show_entries", + "frame=pts_time", + "-v", + "quiet", + "-of", + "default=noprint_wrappers=1:nokey=1", + source, + ] + p = sp.run(ffprobe_cmd, capture_output=True) + keyframe_timestamps = [int(1000 * float(pts)) for pts in p.stdout.split()] + if target_offset == 0: + return keyframe_timestamps, 0 + for ts in reversed(keyframe_timestamps): + if ts <= target_offset: + return keyframe_timestamps, ts + logger.warning("Couldn't find starting keyframe for VOD clip") + return keyframe_timestamps, 0