Use ffprobe if cv2 failed

This commit is contained in:
Nick Mowen 2023-06-15 07:05:53 -06:00
parent d7831ddf9f
commit c58b8a883e
2 changed files with 25 additions and 2 deletions

View File

@ -123,9 +123,11 @@ class RecordingMaintainer(threading.Thread):
.order_by(Event.start_time)
)
start = datetime.datetime.now().timestamp()
await asyncio.gather(
*(self.validate_segment(camera, events, r) for r in recordings)
)
logger.error(f"The time took {datetime.datetime.now().timestamp() - start} seconds")
async def validate_segment(
self, camera: str, events: Event, recording: dict[str, any]
@ -145,7 +147,7 @@ class RecordingMaintainer(threading.Thread):
if cache_path in self.end_time_cache:
end_time, duration = self.end_time_cache[cache_path]
else:
segment_info = get_video_properties(cache_path, duration=True)
segment_info = get_video_properties(cache_path, get_duration=True)
if segment_info["duration"]:
duration = float(segment_info["duration"])

View File

@ -1154,11 +1154,32 @@ def get_video_properties(url, get_duration=False):
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))
logger.error(f"Got values {total_frames} / {fps} == {total_frames / max(fps, 1)}")
if fps and total_frames:
duration = total_frames / fps
else:
# if cv2 failed need to use ffprobe
ffprobe_cmd = [
"ffprobe",
"-v",
"error",
"-show_entries",
"format=duration",
"-of",
"default=noprint_wrappers=1:nokey=1",
f"{url}",
]
p = sp.run(ffprobe_cmd, capture_output=True)
if p.returncode == 0 and p.stdout.decode():
duration = float(p.stdout.decode().strip())
else:
duration = -1
result["duration"] = duration