]> Raphaƫl G. Git Repositories - youtubedl/blob - youtube_dl/downloader/dash.py
Add note about mplayer2 and backporters.
[youtubedl] / youtube_dl / downloader / dash.py
1 from __future__ import unicode_literals
2
3 import re
4
5 from .common import FileDownloader
6 from ..compat import compat_urllib_request
7
8
9 class DashSegmentsFD(FileDownloader):
10 """
11 Download segments in a DASH manifest
12 """
13 def real_download(self, filename, info_dict):
14 self.report_destination(filename)
15 tmpfilename = self.temp_name(filename)
16 base_url = info_dict['url']
17 segment_urls = info_dict['segment_urls']
18
19 is_test = self.params.get('test', False)
20 remaining_bytes = self._TEST_FILE_SIZE if is_test else None
21 byte_counter = 0
22
23 def append_url_to_file(outf, target_url, target_name, remaining_bytes=None):
24 self.to_screen('[DashSegments] %s: Downloading %s' % (info_dict['id'], target_name))
25 req = compat_urllib_request.Request(target_url)
26 if remaining_bytes is not None:
27 req.add_header('Range', 'bytes=0-%d' % (remaining_bytes - 1))
28
29 data = self.ydl.urlopen(req).read()
30
31 if remaining_bytes is not None:
32 data = data[:remaining_bytes]
33
34 outf.write(data)
35 return len(data)
36
37 def combine_url(base_url, target_url):
38 if re.match(r'^https?://', target_url):
39 return target_url
40 return '%s%s%s' % (base_url, '' if base_url.endswith('/') else '/', target_url)
41
42 with open(tmpfilename, 'wb') as outf:
43 append_url_to_file(
44 outf, combine_url(base_url, info_dict['initialization_url']),
45 'initialization segment')
46 for i, segment_url in enumerate(segment_urls):
47 segment_len = append_url_to_file(
48 outf, combine_url(base_url, segment_url),
49 'segment %d / %d' % (i + 1, len(segment_urls)),
50 remaining_bytes)
51 byte_counter += segment_len
52 if remaining_bytes is not None:
53 remaining_bytes -= segment_len
54 if remaining_bytes <= 0:
55 break
56
57 self.try_rename(tmpfilename, filename)
58
59 self._hook_progress({
60 'downloaded_bytes': byte_counter,
61 'total_bytes': byte_counter,
62 'filename': filename,
63 'status': 'finished',
64 })
65
66 return True