forked from Viblast/dash-proxy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dashproxy.py
executable file
·289 lines (226 loc) · 9.76 KB
/
dashproxy.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
#!/usr/bin/env python
import os.path
import time
import logging
import argparse
import requests
import xml.etree.ElementTree
import copy
from termcolor import colored
logging.VERBOSE = (logging.INFO + logging.DEBUG) // 2
logger = logging.getLogger('dash-proxy')
ns = {'mpd':'urn:mpeg:dash:schema:mpd:2011'}
class Formatter(logging.Formatter):
def __init__(self, fmt=None, datefmt=None):
super(Formatter, self).__init__(fmt, datefmt)
def format(self, record):
color = None
if record.levelno == logging.ERROR:
color = 'red'
if record.levelno == logging.INFO:
color = 'green'
if record.levelno == logging.WARNING:
color = 'yellow'
if color:
return colored(record.msg, color)
else:
return record.msg
ch = logging.StreamHandler()
ch.setLevel(logging.DEBUG)
formatter = Formatter()
ch.setFormatter(formatter)
logger.addHandler(ch)
def baseUrl(url):
idx = url.rfind('/')
if idx >= 0:
return url[:idx+1]
else:
return url
class RepAddr(object):
def __init__(self, period_idx, adaptation_set_idx, representation_idx):
self.period_idx = period_idx
self.adaptation_set_idx = adaptation_set_idx
self.representation_idx = representation_idx
def __str__(self):
return 'Representation (period=%d adaptation-set=%d representation=%d)' % (self.period_idx, self.adaptation_set_idx, self.representation_idx)
class MpdLocator(object):
def __init__(self, mpd):
self.mpd = mpd
def representation(self, rep_addr):
return self.adaptation_set(rep_addr).findall('mpd:Representation', ns)[rep_addr.representation_idx]
def segment_template(self, rep_addr):
rep_st = self.representation(rep_addr).find('mpd:SegmentTemplate', ns)
if rep_st is not None:
return rep_st
else:
return self.adaptation_set(rep_addr).find('mpd:SegmentTemplate', ns)
def segment_timeline(self, rep_addr):
return self.segment_template(rep_addr).find('mpd:SegmentTimeline', ns)
def adaptation_set(self, rep_addr):
return self.mpd.findall('mpd:Period', ns)[rep_addr.period_idx].findall('mpd:AdaptationSet', ns)[rep_addr.adaptation_set_idx]
class HasLogger(object):
def verbose(self, msg):
self.logger.log(logging.VERBOSE, msg)
def info(self, msg):
self.logger.log(logging.INFO, msg)
def debug(self, msg):
self.logger.log(logging.DEBUG, msg)
def warning(self, msg):
self.logger.log(logging.WARNING, msg)
def error(self, msg):
self.logger.log(logging.ERROR, msg)
class DashProxy(HasLogger):
retry_interval = 10
def __init__(self, mpd, output_dir, download, save_mpds=False):
self.logger = logger
self.mpd = mpd
self.output_dir = output_dir
self.download = download
self.save_mpds = save_mpds
self.i_refresh = 0
self.downloaders = {}
def run(self):
logger.log(logging.INFO, 'Running dash proxy for stream %s. Output goes in %s' % (self.mpd, self.output_dir))
self.refresh_mpd()
def refresh_mpd(self, after=0):
self.i_refresh += 1
if after>0:
time.sleep(after)
r = requests.get(self.mpd)
if r.status_code < 200 or r.status_code >= 300:
logger.log(logging.WARNING, 'Cannot GET the MPD. Server returned %s. Retrying after %ds' % (r.status_code, retry_interval))
self.refresh_mpd(after=retry_interval)
xml.etree.ElementTree.register_namespace('', ns['mpd'])
mpd = xml.etree.ElementTree.fromstring(r.text)
self.handle_mpd(mpd)
def get_base_url(self, mpd):
base_url = baseUrl(self.mpd)
location = mpd.find('mpd:Location', ns)
if location is not None:
base_url = baseUrl(location.text)
baseUrlNode = mpd.find('mpd:BaseUrl', ns)
if baseUrlNode:
if baseUrlNode.text.startswith('http://') or baseUrlNode.text.startswith('https://'):
base_url = baseUrl(baseUrlNode.text)
else:
base_url = base_url + baseUrlNode.text
return base_url
def handle_mpd(self, mpd):
original_mpd = copy.deepcopy(mpd)
periods = mpd.findall('mpd:Period', ns)
logger.log(logging.INFO, 'mpd=%s' % (periods,))
logger.log(logging.VERBOSE, 'Found %d periods choosing the 1st one' % (len(periods),))
period = periods[0]
for as_idx, adaptation_set in enumerate( period.findall('mpd:AdaptationSet', ns) ):
for rep_idx, representation in enumerate( adaptation_set.findall('mpd:Representation', ns) ):
self.verbose('Found representation with id %s' % (representation.attrib.get('id', 'UKN'),))
rep_addr = RepAddr(0, as_idx, rep_idx)
self.ensure_downloader(mpd, rep_addr)
self.write_output_mpd(original_mpd)
minimum_update_period = mpd.attrib.get('minimumUpdatePeriod', '')
if minimum_update_period:
# TODO parse minimum_update_period
self.refresh_mpd(after=10)
else:
self.info('VOD MPD. Nothing more to do. Stopping...')
def ensure_downloader(self, mpd, rep_addr):
if rep_addr in self.downloaders:
self.verbose('A downloader for %s already started' % (rep_addr,))
else:
self.info('Starting a downloader for %s' % (rep_addr,))
downloader = DashDownloader(self, rep_addr)
self.downloaders[rep_addr] = downloader
downloader.handle_mpd(mpd, self.get_base_url(mpd))
def write_output_mpd(self, mpd):
self.info('Writing the update MPD file')
content = xml.etree.ElementTree.tostring(mpd, encoding="utf-8").decode("utf-8")
dest = os.path.join(self.output_dir, 'manifest.mpd')
with open(dest, 'wt') as f:
f.write(content)
if self.save_mpds:
dest = os.path.join(self.output_dir, 'manifest.{}.mpd'.format(self.i_refresh))
with open(dest, 'wt') as f:
f.write(content)
class DashDownloader(HasLogger):
def __init__(self, proxy, rep_addr):
self.logger = logger
self.proxy = proxy
self.rep_addr = rep_addr
self.mpd_base_url = ''
self.initialization_downloaded = False
def handle_mpd(self, mpd, base_url):
self.mpd_base_url = base_url
self.mpd = MpdLocator(mpd)
rep = self.mpd.representation(self.rep_addr)
segment_template = self.mpd.segment_template(self.rep_addr)
segment_timeline = self.mpd.segment_timeline(self.rep_addr)
initialization_template = segment_template.attrib.get('initialization', '')
if initialization_template and not self.initialization_downloaded:
self.initialization_downloaded = True
self.download_template(initialization_template, rep)
segments = copy.deepcopy(segment_timeline.findall('mpd:S', ns))
idx = 0
for segment in segments:
duration = int( segment.attrib.get('d', '0') )
repeat = int( segment.attrib.get('r', '0') )
idx = idx + 1
for _ in range(0, repeat):
elem = xml.etree.ElementTree.Element('{urn:mpeg:dash:schema:mpd:2011}S', attrib={'d':duration})
segment_timeline.insert(idx, elem)
self.verbose('appding a new elem')
idx = idx + 1
media_template = segment_template.attrib.get('media', '')
nex_time = 0
for segment in segment_timeline.findall('mpd:S', ns):
current_time = int(segment.attrib.get('t', '-1'))
if current_time == -1:
segment.attrib['t'] = next_time
else:
next_time = current_time
next_time += int(segment.attrib.get('d', '0'))
self.download_template(media_template, rep, segment)
def download_template(self, template, representation=None, segment=None):
dest = self.render_template(template, representation, segment)
dest_url = self.full_url(dest)
self.info('requesting %s from %s' % (dest, dest_url))
r = requests.get(dest_url)
if r.status_code >= 200 and r.status_code < 300:
self.write(dest, r.content)
else:
self.error('cannot download %s server returned %d' % (dest_url, r.status_code))
def render_template(self, template, representation=None, segment=None):
template = template.replace('$RepresentationID$', '{representation_id}')
template = template.replace('$Time$', '{time}')
args = {}
if representation is not None:
args['representation_id'] = representation.attrib.get('id', '')
if segment is not None:
args['time'] = segment.attrib.get('t', '')
template = template.format(**args)
return template
def full_url(self, dest):
return self.mpd_base_url + dest # TODO remove hardcoded arrd
def write(self, dest, content):
dest = dest[0:dest.rfind('?')]
dest = os.path.join(self.proxy.output_dir, dest)
f = open(dest, 'wb')
f.write(content)
f.close()
def run(args):
logger.setLevel(logging.VERBOSE if args.v else logging.INFO)
proxy = DashProxy(mpd=args.mpd,
output_dir=args.o,
download=args.d,
save_mpds=args.save_individual_mpds)
return proxy.run()
def main():
parser = argparse.ArgumentParser()
parser.add_argument("mpd")
parser.add_argument("-v", action="store_true")
parser.add_argument("-d", action="store_true")
parser.add_argument("-o", default='.')
parser.add_argument("--save-individual-mpds", action="store_true")
args = parser.parse_args()
run(args)
if __name__ == '__main__':
main()