src/util/UtilVideo.py
This commit is contained in:
parent
753053e586
commit
0dcebf88af
@ -1,21 +1,40 @@
|
|||||||
import struct
|
import struct
|
||||||
|
import logging
|
||||||
|
import subprocess
|
||||||
|
import json
|
||||||
|
from pymediainfo import MediaInfo
|
||||||
|
|
||||||
|
|
||||||
def get_video_metadata(filename):
|
def get_video_metadata(filename):
|
||||||
import subprocess, json
|
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
|
||||||
|
|
||||||
result = subprocess.check_output(
|
if 'tags' in fields and 'DURATION' in fields['tags']:
|
||||||
f'ffprobe -v quiet -show_streams -select_streams v:0 -of json "{filename}"',
|
duration = round(float(fields['tags']['DURATION']), 2)
|
||||||
shell=True).decode()
|
elif 'duration' in fields:
|
||||||
|
duration = round(float(fields['duration']), 2)
|
||||||
|
|
||||||
fields = json.loads(result)['streams'][0]
|
width = fields.get('width', 0)
|
||||||
duration = 0
|
height = fields.get('height', 0)
|
||||||
|
|
||||||
if 'tags' in fields and 'DURATION' in fields['tags']:
|
return width, height, duration
|
||||||
duration = round(float(fields['tags']['DURATION']), 2)
|
except (subprocess.CalledProcessError, FileNotFoundError):
|
||||||
elif 'duration' in fields:
|
logging.warn("ffprobe not found or an error occurred. Using pymediainfo instead.")
|
||||||
duration = round(float(fields['duration']), 2)
|
|
||||||
|
|
||||||
width = fields.get('width', 0)
|
try:
|
||||||
height = fields.get('height', 0)
|
media_info = MediaInfo.parse(filename)
|
||||||
return width, height, duration
|
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
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user