]>
Raphaƫl G. Git Repositories - youtubedl/blob - youtube_dl/downloader/dash.py
1 from __future__
import unicode_literals
5 from .common
import FileDownloader
6 from ..compat
import compat_urllib_request
9 class DashSegmentsFD(FileDownloader
):
11 Download segments in a DASH manifest
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']
19 is_test
= self
.params
.get('test', False)
20 remaining_bytes
= self
._TEST
_FILE
_SIZE
if is_test
else None
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))
29 data
= self
.ydl
.urlopen(req
).read()
31 if remaining_bytes
is not None:
32 data
= data
[:remaining_bytes
]
37 def combine_url(base_url
, target_url
):
38 if re
.match(r
'^https?://', target_url
):
40 return '%s%s%s' % (base_url
, '' if base_url
.endswith('/') else '/', target_url
)
42 with open(tmpfilename
, 'wb') as outf
:
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
)),
51 byte_counter
+= segment_len
52 if remaining_bytes
is not None:
53 remaining_bytes
-= segment_len
54 if remaining_bytes
<= 0:
57 self
.try_rename(tmpfilename
, filename
)
60 'downloaded_bytes': byte_counter
,
61 'total_bytes': byte_counter
,