Move probe function to util

Update comment
This commit is contained in:
Justin Wong 2022-08-18 21:59:09 +08:00
parent 3f374e1a0e
commit 139e1c57df
2 changed files with 35 additions and 32 deletions

View File

@ -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/<camera>/start/<int:start_ts>/end/<int:end_ts>")
@bp.route("/vod/<camera>/start/<float:start_ts>/end/<float:end_ts>")
def vod_ts(camera, start_ts, end_ts):

View File

@ -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