diff --git a/frigate/record/maintainer.py b/frigate/record/maintainer.py index 1159ae5bd..fedde55d5 100644 --- a/frigate/record/maintainer.py +++ b/frigate/record/maintainer.py @@ -21,7 +21,7 @@ from frigate.config import FrigateConfig, RetainModeEnum from frigate.const import CACHE_DIR, MAX_SEGMENT_DURATION, RECORD_DIR from frigate.models import Event, Recordings from frigate.types import RecordMetricsTypes -from frigate.util import area +from frigate.util import area, get_video_properties logger = logging.getLogger(__name__) @@ -145,19 +145,10 @@ class RecordingMaintainer(threading.Thread): if cache_path in self.end_time_cache: end_time, duration = self.end_time_cache[cache_path] else: - ffprobe_cmd = [ - "ffprobe", - "-v", - "error", - "-show_entries", - "format=duration", - "-of", - "default=noprint_wrappers=1:nokey=1", - f"{cache_path}", - ] - p = sp.run(ffprobe_cmd, capture_output=True) - if p.returncode == 0 and p.stdout.decode(): - duration = float(p.stdout.decode().strip()) + segment_info = get_video_properties(cache_path, duration=True) + + if segment_info["duration"]: + duration = float(segment_info["duration"]) else: duration = -1 diff --git a/frigate/util.py b/frigate/util.py index 897aa8d21..4e8b08062 100755 --- a/frigate/util.py +++ b/frigate/util.py @@ -1132,3 +1132,34 @@ def to_relative_box( (box[2] - box[0]) / width, # w (box[3] - box[1]) / height, # h ) + + +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: + # 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