-
Notifications
You must be signed in to change notification settings - Fork 0
/
mympd.py
85 lines (67 loc) · 2.86 KB
/
mympd.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
76
77
78
79
80
81
82
83
import xml.etree.ElementTree
ns = {'mpd' : 'urn:mpeg:dash:schema:mpd:2011'}
class MPDRepresentation:
def __init__(self, representation_xml):
# Extract representation details
self._id = representation_xml.attrib['id']
self._bandwidth = int(representation_xml.attrib['bandwidth'])
self._width = int(representation_xml.attrib['width'])
self._height = int(representation_xml.attrib['height'])
# Extract base URL
base_url = representation_xml.find('mpd:BaseURL', namespaces=ns)
self._base_url = base_url.text
# Extract timescale
segment_list = representation_xml.find('mpd:SegmentList', namespaces=ns)
self._segment_duration = int(segment_list.attrib['duration'])
# Extract ranges
segment_urls = segment_list.findall('mpd:SegmentURL', namespaces=ns)
self._segment_ranges = []
for segment in segment_urls:
self._segment_ranges.append(tuple(
[int(x) for x in segment.attrib['mediaRange'].split('-')]))
@property
def id(self):
return self._id
@property
def base_url(self):
return self._base_url
@property
def segment_ranges(self):
return self._segment_ranges
def segment_range(self, index):
return self._segment_ranges[index]
def __str__(self):
return ('Representation:\n\t' + '\n\t'.join([
'Id: %s' % self._id,
'Width: %d' % self._width,
'Height: %d' % self._height,
'Bandwidth: %d' % self._bandwidth,
'BaseURL: %s' % self._base_url,
'Segment duration: %d' % self._segment_duration,
'Segment ranges: %s' % self._segment_ranges]))
class MPDFile:
def __init__(self, xml_string):
# Parse XML
root = xml.etree.ElementTree.fromstring(xml_string)
# Extract Initialization
period = root.find('mpd:Period', namespaces=ns)
adaptation_set = period.find('mpd:AdaptationSet', namespaces=ns)
segment_list = adaptation_set.find('mpd:SegmentList', namespaces=ns)
initialization = segment_list.find('mpd:Initialization', namespaces=ns)
self._initialization_url = initialization.attrib['sourceURL']
# Extract representations
representations = adaptation_set.findall('mpd:Representation',
namespaces=ns)
self._representations = []
for representation in representations:
self._representations.append(MPDRepresentation(representation))
@property
def initialization_url(self):
return self._initialization_url
@property
def representations(self):
return self._representations
def __str__(self):
return ('\n'.join(['MPD:',
'InitializationtURL: %s' % self._initialization_url,
'\n'.join([str(r) for r in self._representations])]))