mirror of
https://github.com/blakeblackshear/frigate.git
synced 2026-02-05 18:55:23 +03:00
Clean up exception handling
This commit is contained in:
parent
4f78e877cc
commit
0f3bc345e2
@ -1148,14 +1148,18 @@ def to_relative_box(
|
|||||||
|
|
||||||
def get_video_properties(url, get_duration=False):
|
def get_video_properties(url, get_duration=False):
|
||||||
def calculate_duration(video: Optional[any]) -> float:
|
def calculate_duration(video: Optional[any]) -> float:
|
||||||
|
duration = None
|
||||||
|
|
||||||
|
if video is not None:
|
||||||
# Get the frames per second (fps) of the video stream
|
# Get the frames per second (fps) of the video stream
|
||||||
fps = video.get(cv2.CAP_PROP_FPS)
|
fps = video.get(cv2.CAP_PROP_FPS)
|
||||||
total_frames = int(video.get(cv2.CAP_PROP_FRAME_COUNT))
|
total_frames = int(video.get(cv2.CAP_PROP_FRAME_COUNT))
|
||||||
|
|
||||||
if fps and total_frames:
|
if fps and total_frames:
|
||||||
duration = total_frames / fps
|
duration = total_frames / fps
|
||||||
else:
|
|
||||||
# if cv2 failed need to use ffprobe
|
# if cv2 failed need to use ffprobe
|
||||||
|
if duration is None:
|
||||||
ffprobe_cmd = [
|
ffprobe_cmd = [
|
||||||
"ffprobe",
|
"ffprobe",
|
||||||
"-v",
|
"-v",
|
||||||
@ -1167,6 +1171,7 @@ def get_video_properties(url, get_duration=False):
|
|||||||
f"{url}",
|
f"{url}",
|
||||||
]
|
]
|
||||||
p = sp.run(ffprobe_cmd, capture_output=True)
|
p = sp.run(ffprobe_cmd, capture_output=True)
|
||||||
|
|
||||||
if p.returncode == 0 and p.stdout.decode():
|
if p.returncode == 0 and p.stdout.decode():
|
||||||
duration = float(p.stdout.decode().strip())
|
duration = float(p.stdout.decode().strip())
|
||||||
else:
|
else:
|
||||||
@ -1182,14 +1187,16 @@ def get_video_properties(url, get_duration=False):
|
|||||||
|
|
||||||
# Check if the video stream was opened successfully
|
# Check if the video stream was opened successfully
|
||||||
if not video.isOpened():
|
if not video.isOpened():
|
||||||
logger.debug(f"Error opening video stream {url}.")
|
video = None
|
||||||
raise Exception()
|
|
||||||
except Exception:
|
except Exception:
|
||||||
|
video = None
|
||||||
|
|
||||||
|
result = {}
|
||||||
|
|
||||||
if get_duration:
|
if get_duration:
|
||||||
return {"duration": calculate_duration({})}
|
result["duration"] = calculate_duration(video)
|
||||||
|
|
||||||
return {}
|
|
||||||
|
|
||||||
|
if video is not None:
|
||||||
# Get the width of frames in the video stream
|
# Get the width of frames in the video stream
|
||||||
width = video.get(cv2.CAP_PROP_FRAME_WIDTH)
|
width = video.get(cv2.CAP_PROP_FRAME_WIDTH)
|
||||||
|
|
||||||
@ -1201,7 +1208,4 @@ def get_video_properties(url, get_duration=False):
|
|||||||
|
|
||||||
result = {"width": round(width), "height": round(height)}
|
result = {"width": round(width), "height": round(height)}
|
||||||
|
|
||||||
if get_duration:
|
|
||||||
result["duration"] = calculate_duration(video)
|
|
||||||
|
|
||||||
return result
|
return result
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user