From c58b8a883e0f3f86c8f530a1d162f39e9999bc85 Mon Sep 17 00:00:00 2001 From: Nick Mowen Date: Thu, 15 Jun 2023 07:05:53 -0600 Subject: [PATCH] Use ffprobe if cv2 failed --- frigate/record/maintainer.py | 4 +++- frigate/util.py | 23 ++++++++++++++++++++++- 2 files changed, 25 insertions(+), 2 deletions(-) diff --git a/frigate/record/maintainer.py b/frigate/record/maintainer.py index fedde55d5..d2aeea4b9 100644 --- a/frigate/record/maintainer.py +++ b/frigate/record/maintainer.py @@ -123,9 +123,11 @@ class RecordingMaintainer(threading.Thread): .order_by(Event.start_time) ) + start = datetime.datetime.now().timestamp() await asyncio.gather( *(self.validate_segment(camera, events, r) for r in recordings) ) + logger.error(f"The time took {datetime.datetime.now().timestamp() - start} seconds") async def validate_segment( self, camera: str, events: Event, recording: dict[str, any] @@ -145,7 +147,7 @@ class RecordingMaintainer(threading.Thread): if cache_path in self.end_time_cache: end_time, duration = self.end_time_cache[cache_path] else: - segment_info = get_video_properties(cache_path, duration=True) + segment_info = get_video_properties(cache_path, get_duration=True) if segment_info["duration"]: duration = float(segment_info["duration"]) diff --git a/frigate/util.py b/frigate/util.py index 4e8b08062..51d5a5433 100755 --- a/frigate/util.py +++ b/frigate/util.py @@ -1154,11 +1154,32 @@ def get_video_properties(url, get_duration=False): video.release() result = {"width": round(width), "height": round(height)} + if get_duration: # Get the frames per second (fps) of the video stream fps = video.get(cv2.CAP_PROP_FPS) total_frames = int(video.get(cv2.CAP_PROP_FRAME_COUNT)) - duration = total_frames / fps + logger.error(f"Got values {total_frames} / {fps} == {total_frames / max(fps, 1)}") + + if fps and total_frames: + duration = total_frames / fps + else: + # if cv2 failed need to use ffprobe + ffprobe_cmd = [ + "ffprobe", + "-v", + "error", + "-show_entries", + "format=duration", + "-of", + "default=noprint_wrappers=1:nokey=1", + f"{url}", + ] + p = sp.run(ffprobe_cmd, capture_output=True) + if p.returncode == 0 and p.stdout.decode(): + duration = float(p.stdout.decode().strip()) + else: + duration = -1 result["duration"] = duration