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

This commit is contained in:
Sergey Krashevich 2023-06-15 03:01:03 +03:00
parent 63cd8e54c5
commit a53fc32d1a
No known key found for this signature in database
GPG Key ID: 625171324E7D3856
2 changed files with 15 additions and 10 deletions

View File

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

View File

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