From a53fc32d1aaa567e7cbd8b46cb76a652017ff96d Mon Sep 17 00:00:00 2001 From: Sergey Krashevich Date: Thu, 15 Jun 2023 03:01:03 +0300 Subject: [PATCH] Refactor code to retrieve video properties from input stream in CameraConfig class and add optional parameter to retrieve video duration in get_video_properties function --- frigate/config.py | 9 ++++----- frigate/util.py | 16 +++++++++++----- 2 files changed, 15 insertions(+), 10 deletions(-) diff --git a/frigate/config.py b/frigate/config.py index 1506b8a9b..f781e2854 100644 --- a/frigate/config.py +++ b/frigate/config.py @@ -676,11 +676,10 @@ class CameraConfig(FrigateBaseModel): "detect" in input.get("roles", []) ): try: - ( - config["detect"]["width"], - config["detect"]["height"], - # config["detect"]["fps"], - ) = get_video_properties(input.get("path")) + + streamInfo = get_video_properties(input.get("path")) + config["detect"]["width"] = streamInfo["width"] + config["detect"]["height"] = streamInfo["height"] break except Exception: logger.debug("Error autoconf url " + input.get("path")) diff --git a/frigate/util.py b/frigate/util.py index e6ce81c52..c7d27bfe8 100755 --- a/frigate/util.py +++ b/frigate/util.py @@ -1146,7 +1146,7 @@ def to_relative_box( ) -def get_video_properties(url): +def get_video_properties(url, get_duration=False): width = height = 0 # Open the video stream video = cv2.VideoCapture(url) @@ -1162,10 +1162,16 @@ def get_video_properties(url): # Get the height of frames in the video stream height = video.get(cv2.CAP_PROP_FRAME_HEIGHT) - # Get the frames per second (fps) of the video stream - # fps = min(24.0, video.get(cv2.CAP_PROP_FPS)) - # Release the video stream video.release() - return width, height # , fps + result = {"width": width, "height": 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 + + result["duration"] = duration + + return result