src/util/UtilVideo.py

This commit is contained in:
jr-k 2024-08-27 01:07:32 +02:00
parent 753053e586
commit 0dcebf88af

View File

@ -1,13 +1,13 @@
import struct
import logging
import subprocess
import json
from pymediainfo import MediaInfo
def get_video_metadata(filename):
import subprocess, json
result = subprocess.check_output(
f'ffprobe -v quiet -show_streams -select_streams v:0 -of json "{filename}"',
shell=True).decode()
try:
result = subprocess.check_output(f'ffprobe -v quiet -show_streams -select_streams v:0 -of json "{filename}"', shell=True).decode()
fields = json.loads(result)['streams'][0]
duration = 0
@ -18,4 +18,23 @@ def get_video_metadata(filename):
width = fields.get('width', 0)
height = fields.get('height', 0)
return width, height, duration
except (subprocess.CalledProcessError, FileNotFoundError):
logging.warn("ffprobe not found or an error occurred. Using pymediainfo instead.")
try:
media_info = MediaInfo.parse(filename)
for track in media_info.tracks:
if track.track_type == "Video":
duration = round(track.duration / 1000, 2) if track.duration else None
width = track.width
height = track.height
return width, height, duration
except OSError:
logging.warn("Fail to get video metadata from pymediainfo.")
except json.JSONDecodeError:
logging.warn("Fail to get video metadata from ffprobe.")
return 0, 0, 0