-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.py
32 lines (26 loc) Β· 1.07 KB
/
utils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
import os
import cv2
import subprocess
from constants import STORAGE_DIR
def get_fs_data(id: str):
files = [os.path.join(STORAGE_DIR, id, file) for file in ['video.asr', 'video.dsc', 'video.vtt']]
return [open(file).read() if os.path.exists(file) else None for file in files]
def split_lines(string: str):
if string:
return string.strip().split('\n')
def try_open(file_name: str, default):
try:
return open(file_name).read()
except:
return default
def get_duration_ffprobe(input_video):
result = subprocess.run(['ffprobe', '-v', 'error', '-show_entries', 'format=duration', '-of', 'default=noprint_wrappers=1:nokey=1', input_video], stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
return round(float(result.stdout), 2)
def get_video_duration(path: str) -> float:
video = cv2.VideoCapture(path)
fps = video.get(cv2.CAP_PROP_FPS)
frm = video.get(cv2.CAP_PROP_FRAME_COUNT)
if fps == 0 or frm == 0:
# Fall back to ffprobe (for audio-only files)
return get_duration_ffprobe(path)
return round(frm/fps, 2)