-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathffprobe.py
40 lines (34 loc) · 1.43 KB
/
ffprobe.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
33
34
35
36
37
38
39
40
import json
import subprocess
def _get_json(path):
result = subprocess.run(['ffprobe', path, '-loglevel', 'quiet', '-print_format', 'json', '-show_streams'], stdout=subprocess.PIPE)
result.check_returncode()
return json.loads(result.stdout)
def get_resolution(path):
for stream in _get_json(path)['streams']:
if stream['codec_type'] == 'video':
return stream['width'], stream['height']
def get_frames(path):
for stream in _get_json(path)['streams']:
if stream['codec_type'] == 'video':
if 'nb_frames' in stream:
return int(stream['nb_frames'])
def get_duration(path):
for stream in _get_json(path)['streams']:
if stream['codec_type'] == 'video':
if 'duration' in stream:
return float(stream['duration'])
else:
parts = stream['tags']['DURATION'].split(':')
assert len(parts) == 3
return float(parts[0]) * 3600 + float(parts[1]) * 60 + float(parts[2])
def get_frame_rate(path):
for stream in _get_json(path)['streams']:
if stream['codec_type'] == 'video':
if 'avg_frame_rate' in stream:
assert stream['avg_frame_rate'].count('/') <= 1
parts = stream['avg_frame_rate'].split('/')
result = float(parts[0])
if len(parts) == 2:
result /= float(parts[1])
return result