-
Notifications
You must be signed in to change notification settings - Fork 0
/
ffprobe.py
75 lines (60 loc) · 1.6 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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
import json
from sh import ffprobe as program
import utils
from log import log, warn
def ffprobe(filename):
log('probing %s' % (filename))
probe = program.bake('-show_error', '-show_streams', '-print_format', 'json')
try:
output = probe(filename)
except Exception as e:
err = json.loads(e.stdout.decode('utf-8'))
warn('error on %s: %s' % (filename, err['error']['string']))
return None
output = json.loads(output.stdout.decode('utf-8'))
result = {}
for i in output['streams']:
#Shitty stuff
try:
if i['codec_type'] == 'data':
continue
except KeyError:
pass
try:
if i['codec_name'] in ('mjpeg', 'png', 'pgssub', 'dvd_subtitle', 'hdmv_pgs_subtitle'):
continue
except KeyError:
pass
stream = {
'type': i['codec_type'],
}
try:
i['tags'] = utils.CaseInsensCreepyDict(i['tags'])
stream['lang'] = i['tags']['language']
except KeyError:
pass
if i['codec_type'] == 'subtitle' and 'lang' not in stream:
#unknown language, drop subs
continue
result[i['index']] = stream
return result
def get_duration(filename):
try:
output = program('-show_programs', '-show_streams', '-print_format', 'json', filename)
output = json.loads(output.stdout.decode('utf-8'))
for i in output['programs']:
try:
end = i['end_time']
start = i['start_time']
return int(float(end) - float(start))
except KeyError:
pass
# cannot found duration based on programs.. let's try streams
for stream in output['streams']:
try:
return int(float(stream['duration']))
except KeyError:
pass
except:
pass
raise Exception('Cannot find duration')