]> Raphaƫl G. Git Repositories - youtubedl/blob - youtube_dl/downloader/f4m.py
debian/gbp.conf: Change to new git-buildpackage syntax.
[youtubedl] / youtube_dl / downloader / f4m.py
1 from __future__ import division, unicode_literals
2
3 import base64
4 import io
5 import itertools
6 import os
7 import time
8 import xml.etree.ElementTree as etree
9
10 from .common import FileDownloader
11 from .http import HttpFD
12 from ..compat import (
13 compat_urlparse,
14 compat_urllib_error,
15 )
16 from ..utils import (
17 struct_pack,
18 struct_unpack,
19 encodeFilename,
20 sanitize_open,
21 xpath_text,
22 )
23
24
25 class FlvReader(io.BytesIO):
26 """
27 Reader for Flv files
28 The file format is documented in https://www.adobe.com/devnet/f4v.html
29 """
30
31 # Utility functions for reading numbers and strings
32 def read_unsigned_long_long(self):
33 return struct_unpack('!Q', self.read(8))[0]
34
35 def read_unsigned_int(self):
36 return struct_unpack('!I', self.read(4))[0]
37
38 def read_unsigned_char(self):
39 return struct_unpack('!B', self.read(1))[0]
40
41 def read_string(self):
42 res = b''
43 while True:
44 char = self.read(1)
45 if char == b'\x00':
46 break
47 res += char
48 return res
49
50 def read_box_info(self):
51 """
52 Read a box and return the info as a tuple: (box_size, box_type, box_data)
53 """
54 real_size = size = self.read_unsigned_int()
55 box_type = self.read(4)
56 header_end = 8
57 if size == 1:
58 real_size = self.read_unsigned_long_long()
59 header_end = 16
60 return real_size, box_type, self.read(real_size - header_end)
61
62 def read_asrt(self):
63 # version
64 self.read_unsigned_char()
65 # flags
66 self.read(3)
67 quality_entry_count = self.read_unsigned_char()
68 # QualityEntryCount
69 for i in range(quality_entry_count):
70 self.read_string()
71
72 segment_run_count = self.read_unsigned_int()
73 segments = []
74 for i in range(segment_run_count):
75 first_segment = self.read_unsigned_int()
76 fragments_per_segment = self.read_unsigned_int()
77 segments.append((first_segment, fragments_per_segment))
78
79 return {
80 'segment_run': segments,
81 }
82
83 def read_afrt(self):
84 # version
85 self.read_unsigned_char()
86 # flags
87 self.read(3)
88 # time scale
89 self.read_unsigned_int()
90
91 quality_entry_count = self.read_unsigned_char()
92 # QualitySegmentUrlModifiers
93 for i in range(quality_entry_count):
94 self.read_string()
95
96 fragments_count = self.read_unsigned_int()
97 fragments = []
98 for i in range(fragments_count):
99 first = self.read_unsigned_int()
100 first_ts = self.read_unsigned_long_long()
101 duration = self.read_unsigned_int()
102 if duration == 0:
103 discontinuity_indicator = self.read_unsigned_char()
104 else:
105 discontinuity_indicator = None
106 fragments.append({
107 'first': first,
108 'ts': first_ts,
109 'duration': duration,
110 'discontinuity_indicator': discontinuity_indicator,
111 })
112
113 return {
114 'fragments': fragments,
115 }
116
117 def read_abst(self):
118 # version
119 self.read_unsigned_char()
120 # flags
121 self.read(3)
122
123 self.read_unsigned_int() # BootstrapinfoVersion
124 # Profile,Live,Update,Reserved
125 flags = self.read_unsigned_char()
126 live = flags & 0x20 != 0
127 # time scale
128 self.read_unsigned_int()
129 # CurrentMediaTime
130 self.read_unsigned_long_long()
131 # SmpteTimeCodeOffset
132 self.read_unsigned_long_long()
133
134 self.read_string() # MovieIdentifier
135 server_count = self.read_unsigned_char()
136 # ServerEntryTable
137 for i in range(server_count):
138 self.read_string()
139 quality_count = self.read_unsigned_char()
140 # QualityEntryTable
141 for i in range(quality_count):
142 self.read_string()
143 # DrmData
144 self.read_string()
145 # MetaData
146 self.read_string()
147
148 segments_count = self.read_unsigned_char()
149 segments = []
150 for i in range(segments_count):
151 box_size, box_type, box_data = self.read_box_info()
152 assert box_type == b'asrt'
153 segment = FlvReader(box_data).read_asrt()
154 segments.append(segment)
155 fragments_run_count = self.read_unsigned_char()
156 fragments = []
157 for i in range(fragments_run_count):
158 box_size, box_type, box_data = self.read_box_info()
159 assert box_type == b'afrt'
160 fragments.append(FlvReader(box_data).read_afrt())
161
162 return {
163 'segments': segments,
164 'fragments': fragments,
165 'live': live,
166 }
167
168 def read_bootstrap_info(self):
169 total_size, box_type, box_data = self.read_box_info()
170 assert box_type == b'abst'
171 return FlvReader(box_data).read_abst()
172
173
174 def read_bootstrap_info(bootstrap_bytes):
175 return FlvReader(bootstrap_bytes).read_bootstrap_info()
176
177
178 def build_fragments_list(boot_info):
179 """ Return a list of (segment, fragment) for each fragment in the video """
180 res = []
181 segment_run_table = boot_info['segments'][0]
182 fragment_run_entry_table = boot_info['fragments'][0]['fragments']
183 first_frag_number = fragment_run_entry_table[0]['first']
184 fragments_counter = itertools.count(first_frag_number)
185 for segment, fragments_count in segment_run_table['segment_run']:
186 for _ in range(fragments_count):
187 res.append((segment, next(fragments_counter)))
188
189 if boot_info['live']:
190 res = res[-2:]
191
192 return res
193
194
195 def write_unsigned_int(stream, val):
196 stream.write(struct_pack('!I', val))
197
198
199 def write_unsigned_int_24(stream, val):
200 stream.write(struct_pack('!I', val)[1:])
201
202
203 def write_flv_header(stream):
204 """Writes the FLV header to stream"""
205 # FLV header
206 stream.write(b'FLV\x01')
207 stream.write(b'\x05')
208 stream.write(b'\x00\x00\x00\x09')
209 stream.write(b'\x00\x00\x00\x00')
210
211
212 def write_metadata_tag(stream, metadata):
213 """Writes optional metadata tag to stream"""
214 SCRIPT_TAG = b'\x12'
215 FLV_TAG_HEADER_LEN = 11
216
217 if metadata:
218 stream.write(SCRIPT_TAG)
219 write_unsigned_int_24(stream, len(metadata))
220 stream.write(b'\x00\x00\x00\x00\x00\x00\x00')
221 stream.write(metadata)
222 write_unsigned_int(stream, FLV_TAG_HEADER_LEN + len(metadata))
223
224
225 def _add_ns(prop):
226 return '{http://ns.adobe.com/f4m/1.0}%s' % prop
227
228
229 class HttpQuietDownloader(HttpFD):
230 def to_screen(self, *args, **kargs):
231 pass
232
233
234 class F4mFD(FileDownloader):
235 """
236 A downloader for f4m manifests or AdobeHDS.
237 """
238
239 def _get_unencrypted_media(self, doc):
240 media = doc.findall(_add_ns('media'))
241 if not media:
242 self.report_error('No media found')
243 for e in (doc.findall(_add_ns('drmAdditionalHeader')) +
244 doc.findall(_add_ns('drmAdditionalHeaderSet'))):
245 # If id attribute is missing it's valid for all media nodes
246 # without drmAdditionalHeaderId or drmAdditionalHeaderSetId attribute
247 if 'id' not in e.attrib:
248 self.report_error('Missing ID in f4m DRM')
249 media = list(filter(lambda e: 'drmAdditionalHeaderId' not in e.attrib and
250 'drmAdditionalHeaderSetId' not in e.attrib,
251 media))
252 if not media:
253 self.report_error('Unsupported DRM')
254 return media
255
256 def _get_bootstrap_from_url(self, bootstrap_url):
257 bootstrap = self.ydl.urlopen(bootstrap_url).read()
258 return read_bootstrap_info(bootstrap)
259
260 def _update_live_fragments(self, bootstrap_url, latest_fragment):
261 fragments_list = []
262 retries = 30
263 while (not fragments_list) and (retries > 0):
264 boot_info = self._get_bootstrap_from_url(bootstrap_url)
265 fragments_list = build_fragments_list(boot_info)
266 fragments_list = [f for f in fragments_list if f[1] > latest_fragment]
267 if not fragments_list:
268 # Retry after a while
269 time.sleep(5.0)
270 retries -= 1
271
272 if not fragments_list:
273 self.report_error('Failed to update fragments')
274
275 return fragments_list
276
277 def _parse_bootstrap_node(self, node, base_url):
278 if node.text is None:
279 bootstrap_url = compat_urlparse.urljoin(
280 base_url, node.attrib['url'])
281 boot_info = self._get_bootstrap_from_url(bootstrap_url)
282 else:
283 bootstrap_url = None
284 bootstrap = base64.b64decode(node.text)
285 boot_info = read_bootstrap_info(bootstrap)
286 return (boot_info, bootstrap_url)
287
288 def real_download(self, filename, info_dict):
289 man_url = info_dict['url']
290 requested_bitrate = info_dict.get('tbr')
291 self.to_screen('[download] Downloading f4m manifest')
292 manifest = self.ydl.urlopen(man_url).read()
293
294 doc = etree.fromstring(manifest)
295 formats = [(int(f.attrib.get('bitrate', -1)), f)
296 for f in self._get_unencrypted_media(doc)]
297 if requested_bitrate is None:
298 # get the best format
299 formats = sorted(formats, key=lambda f: f[0])
300 rate, media = formats[-1]
301 else:
302 rate, media = list(filter(
303 lambda f: int(f[0]) == requested_bitrate, formats))[0]
304
305 base_url = compat_urlparse.urljoin(man_url, media.attrib['url'])
306 bootstrap_node = doc.find(_add_ns('bootstrapInfo'))
307 boot_info, bootstrap_url = self._parse_bootstrap_node(bootstrap_node, base_url)
308 live = boot_info['live']
309 metadata_node = media.find(_add_ns('metadata'))
310 if metadata_node is not None:
311 metadata = base64.b64decode(metadata_node.text)
312 else:
313 metadata = None
314
315 fragments_list = build_fragments_list(boot_info)
316 if self.params.get('test', False):
317 # We only download the first fragment
318 fragments_list = fragments_list[:1]
319 total_frags = len(fragments_list)
320 # For some akamai manifests we'll need to add a query to the fragment url
321 akamai_pv = xpath_text(doc, _add_ns('pv-2.0'))
322
323 self.report_destination(filename)
324 http_dl = HttpQuietDownloader(
325 self.ydl,
326 {
327 'continuedl': True,
328 'quiet': True,
329 'noprogress': True,
330 'ratelimit': self.params.get('ratelimit', None),
331 'test': self.params.get('test', False),
332 }
333 )
334 tmpfilename = self.temp_name(filename)
335 (dest_stream, tmpfilename) = sanitize_open(tmpfilename, 'wb')
336
337 write_flv_header(dest_stream)
338 if not live:
339 write_metadata_tag(dest_stream, metadata)
340
341 # This dict stores the download progress, it's updated by the progress
342 # hook
343 state = {
344 'status': 'downloading',
345 'downloaded_bytes': 0,
346 'frag_index': 0,
347 'frag_count': total_frags,
348 'filename': filename,
349 'tmpfilename': tmpfilename,
350 }
351 start = time.time()
352
353 def frag_progress_hook(s):
354 if s['status'] not in ('downloading', 'finished'):
355 return
356
357 frag_total_bytes = s.get('total_bytes', 0)
358 if s['status'] == 'finished':
359 state['downloaded_bytes'] += frag_total_bytes
360 state['frag_index'] += 1
361
362 estimated_size = (
363 (state['downloaded_bytes'] + frag_total_bytes) /
364 (state['frag_index'] + 1) * total_frags)
365 time_now = time.time()
366 state['total_bytes_estimate'] = estimated_size
367 state['elapsed'] = time_now - start
368
369 if s['status'] == 'finished':
370 progress = self.calc_percent(state['frag_index'], total_frags)
371 else:
372 frag_downloaded_bytes = s['downloaded_bytes']
373 frag_progress = self.calc_percent(frag_downloaded_bytes,
374 frag_total_bytes)
375 progress = self.calc_percent(state['frag_index'], total_frags)
376 progress += frag_progress / float(total_frags)
377
378 state['eta'] = self.calc_eta(
379 start, time_now, estimated_size, state['downloaded_bytes'] + frag_downloaded_bytes)
380 state['speed'] = s.get('speed')
381 self._hook_progress(state)
382
383 http_dl.add_progress_hook(frag_progress_hook)
384
385 frags_filenames = []
386 while fragments_list:
387 seg_i, frag_i = fragments_list.pop(0)
388 name = 'Seg%d-Frag%d' % (seg_i, frag_i)
389 url = base_url + name
390 if akamai_pv:
391 url += '?' + akamai_pv.strip(';')
392 frag_filename = '%s-%s' % (tmpfilename, name)
393 try:
394 success = http_dl.download(frag_filename, {'url': url})
395 if not success:
396 return False
397 with open(frag_filename, 'rb') as down:
398 down_data = down.read()
399 reader = FlvReader(down_data)
400 while True:
401 _, box_type, box_data = reader.read_box_info()
402 if box_type == b'mdat':
403 dest_stream.write(box_data)
404 break
405 if live:
406 os.remove(frag_filename)
407 else:
408 frags_filenames.append(frag_filename)
409 except (compat_urllib_error.HTTPError, ) as err:
410 if live and (err.code == 404 or err.code == 410):
411 # We didn't keep up with the live window. Continue
412 # with the next available fragment.
413 msg = 'Fragment %d unavailable' % frag_i
414 self.report_warning(msg)
415 fragments_list = []
416 else:
417 raise
418
419 if not fragments_list and live and bootstrap_url:
420 fragments_list = self._update_live_fragments(bootstrap_url, frag_i)
421 total_frags += len(fragments_list)
422 if fragments_list and (fragments_list[0][1] > frag_i + 1):
423 msg = 'Missed %d fragments' % (fragments_list[0][1] - (frag_i + 1))
424 self.report_warning(msg)
425
426 dest_stream.close()
427
428 elapsed = time.time() - start
429 self.try_rename(tmpfilename, filename)
430 for frag_file in frags_filenames:
431 os.remove(frag_file)
432
433 fsize = os.path.getsize(encodeFilename(filename))
434 self._hook_progress({
435 'downloaded_bytes': fsize,
436 'total_bytes': fsize,
437 'filename': filename,
438 'status': 'finished',
439 'elapsed': elapsed,
440 })
441
442 return True