From 4f78e877cccb77818e7e97dffe3cfcd17f3eb4f3 Mon Sep 17 00:00:00 2001 From: Nick Mowen Date: Fri, 16 Jun 2023 12:24:49 -0600 Subject: [PATCH] Don't fail when cv2 fails --- frigate/util.py | 54 +++++++++++++++++++++++++++++-------------------- 1 file changed, 32 insertions(+), 22 deletions(-) diff --git a/frigate/util.py b/frigate/util.py index f18d3279e..cb5a0ae20 100755 --- a/frigate/util.py +++ b/frigate/util.py @@ -1147,27 +1147,7 @@ def to_relative_box( def get_video_properties(url, get_duration=False): - width = height = 0 - # Open the video stream - video = cv2.VideoCapture(url) - - # Check if the video stream was opened successfully - if not video.isOpened(): - logger.debug(f"Error opening video stream {url}.") - return None - - # Get the width of frames in the video stream - width = video.get(cv2.CAP_PROP_FRAME_WIDTH) - - # Get the height of frames in the video stream - height = video.get(cv2.CAP_PROP_FRAME_HEIGHT) - - # Release the video stream - video.release() - - result = {"width": round(width), "height": round(height)} - - if get_duration: + def calculate_duration(video: Optional[any]) -> float: # 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)) @@ -1192,6 +1172,36 @@ def get_video_properties(url, get_duration=False): else: duration = -1 - result["duration"] = duration + return duration + + width = height = 0 + + try: + # Open the video stream + video = cv2.VideoCapture(url) + + # Check if the video stream was opened successfully + if not video.isOpened(): + logger.debug(f"Error opening video stream {url}.") + raise Exception() + except Exception: + if get_duration: + return {"duration": calculate_duration({})} + + return {} + + # Get the width of frames in the video stream + width = video.get(cv2.CAP_PROP_FRAME_WIDTH) + + # Get the height of frames in the video stream + height = video.get(cv2.CAP_PROP_FRAME_HEIGHT) + + # Release the video stream + video.release() + + result = {"width": round(width), "height": round(height)} + + if get_duration: + result["duration"] = calculate_duration(video) return result