-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmp4dump_parser.py
More file actions
68 lines (51 loc) · 2.19 KB
/
mp4dump_parser.py
File metadata and controls
68 lines (51 loc) · 2.19 KB
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
import subprocess
import json
import models
class MP4DumpCommand(object):
def __init__(self, executable='mp4dump', filename=None):
self._command = '"{mp4dump}" --format json {filename}'.format(mp4dump=executable, filename=filename)
print("Executing mp4dump to extract track and fragment information")
print(self._command)
print()
def call(self):
response = subprocess.check_output(self._command, shell=True, stderr=None)
jresponse = json.loads(response)
return MP4DumpResponse(jresponse)
class MP4DumpResponse(object):
def __init__(self, j):
self._json = j
# TODO: cater for multiple tracks
def get_info_for_track(self, track):
if isinstance(track, models.MP4Track):
for idx, box in enumerate(self._json):
if box['name'] == 'moov':
# TODO - crude! put more logic for multiple use cases (variable structure)
track.duration = box['children'][0]['duration'] / box['children'][0]['timescale']
# scale factor for decode timings
if box['name'] == 'sidx':
track.time_scale = box['timescale']
else:
raise Exception("'track' argument should be a MP4Track")
# TODO: cater for multiple tracks
def get_fragments_for_track(self, track):
fragments = []
moof = None
mdat = None
count = 1
if isinstance(track, models.MP4Track):
for box in self._json:
# start of a fragment
if box['name'] == 'moof':
moof = box
# data from segment
if box['name'] == 'mdat':
mdat = box
# mdat is the last element required to create a fragment
# no moof would mean non-fragmented MP4
if moof and mdat:
fragment = models.Fragment(moof=moof, mdat=mdat, track=track, position=count)
fragments.append(fragment)
count+=1
return fragments
else:
raise Exception("'track' argument should be a MP4Track")