]>
Commit | Line | Data |
---|---|---|
95d8f7ea S |
1 | from __future__ import division, unicode_literals |
2 | ||
3 | import os | |
4 | import time | |
ea0c2f21 | 5 | import json |
95d8f7ea S |
6 | |
7 | from .common import FileDownloader | |
8 | from .http import HttpFD | |
9 | from ..utils import ( | |
2e99cd30 | 10 | error_to_compat_str, |
95d8f7ea S |
11 | encodeFilename, |
12 | sanitize_open, | |
69035555 | 13 | sanitized_Request, |
95d8f7ea S |
14 | ) |
15 | ||
16 | ||
17 | class HttpQuietDownloader(HttpFD): | |
18 | def to_screen(self, *args, **kargs): | |
19 | pass | |
20 | ||
21 | ||
22 | class FragmentFD(FileDownloader): | |
23 | """ | |
24 | A base file downloader class for fragmented media (e.g. f4m/m3u8 manifests). | |
16a8b798 S |
25 | |
26 | Available options: | |
27 | ||
9603b660 S |
28 | fragment_retries: Number of times to retry a fragment for HTTP error (DASH |
29 | and hlsnative only) | |
30 | skip_unavailable_fragments: | |
31 | Skip unavailable fragments (DASH and hlsnative only) | |
0eee52f3 S |
32 | keep_fragments: Keep downloaded fragments on disk after downloading is |
33 | finished | |
95d8f7ea S |
34 | """ |
35 | ||
75a24854 | 36 | def report_retry_fragment(self, err, frag_index, count, retries): |
721f26b8 | 37 | self.to_screen( |
75a24854 RA |
38 | '[download] Got server HTTP error: %s. Retrying fragment %d (attempt %d of %s)...' |
39 | % (error_to_compat_str(err), frag_index, count, self.format_retries(retries))) | |
721f26b8 | 40 | |
75a24854 RA |
41 | def report_skip_fragment(self, frag_index): |
42 | self.to_screen('[download] Skipping fragment %d...' % frag_index) | |
9603b660 | 43 | |
69035555 S |
44 | def _prepare_url(self, info_dict, url): |
45 | headers = info_dict.get('http_headers') | |
46 | return sanitized_Request(url, None, headers) if headers else url | |
47 | ||
95d8f7ea S |
48 | def _prepare_and_start_frag_download(self, ctx): |
49 | self._prepare_frag_download(ctx) | |
50 | self._start_frag_download(ctx) | |
51 | ||
adb4b03c S |
52 | @staticmethod |
53 | def __do_ytdl_file(ctx): | |
54 | return not ctx['live'] and not ctx['tmpfilename'] == '-' | |
55 | ||
d3f0687c S |
56 | def _read_ytdl_file(self, ctx): |
57 | stream, _ = sanitize_open(self.ytdl_filename(ctx['filename']), 'r') | |
58 | ctx['fragment_index'] = json.loads(stream.read())['download']['current_fragment_index'] | |
59 | stream.close() | |
60 | ||
61 | def _write_ytdl_file(self, ctx): | |
62 | frag_index_stream, _ = sanitize_open(self.ytdl_filename(ctx['filename']), 'w') | |
63 | frag_index_stream.write(json.dumps({ | |
64 | 'download': { | |
65 | 'current_fragment_index': ctx['fragment_index'] | |
66 | }, | |
67 | })) | |
68 | frag_index_stream.close() | |
69 | ||
75a24854 | 70 | def _download_fragment(self, ctx, frag_url, info_dict, headers=None): |
d3f0687c S |
71 | fragment_filename = '%s-Frag%d' % (ctx['tmpfilename'], ctx['fragment_index']) |
72 | success = ctx['dl'].download(fragment_filename, { | |
75a24854 RA |
73 | 'url': frag_url, |
74 | 'http_headers': headers or info_dict.get('http_headers'), | |
75 | }) | |
76 | if not success: | |
77 | return False, None | |
d3f0687c S |
78 | down, frag_sanitized = sanitize_open(fragment_filename, 'rb') |
79 | ctx['fragment_filename_sanitized'] = frag_sanitized | |
80 | frag_content = down.read() | |
75a24854 RA |
81 | down.close() |
82 | return True, frag_content | |
83 | ||
84 | def _append_fragment(self, ctx, frag_content): | |
d3f0687c S |
85 | try: |
86 | ctx['dest_stream'].write(frag_content) | |
87 | finally: | |
adb4b03c | 88 | if self.__do_ytdl_file(ctx): |
d3f0687c | 89 | self._write_ytdl_file(ctx) |
0eee52f3 S |
90 | if not self.params.get('keep_fragments', False): |
91 | os.remove(ctx['fragment_filename_sanitized']) | |
d3f0687c | 92 | del ctx['fragment_filename_sanitized'] |
75a24854 | 93 | |
95d8f7ea | 94 | def _prepare_frag_download(self, ctx): |
5fa1702c S |
95 | if 'live' not in ctx: |
96 | ctx['live'] = False | |
97 | self.to_screen( | |
98 | '[%s] Total fragments: %s' | |
99 | % (self.FD_NAME, ctx['total_frags'] if not ctx['live'] else 'unknown (live)')) | |
95d8f7ea S |
100 | self.report_destination(ctx['filename']) |
101 | dl = HttpQuietDownloader( | |
102 | self.ydl, | |
103 | { | |
104 | 'continuedl': True, | |
105 | 'quiet': True, | |
106 | 'noprogress': True, | |
d800609c | 107 | 'ratelimit': self.params.get('ratelimit'), |
6828c809 | 108 | 'retries': self.params.get('retries', 0), |
7097bffb | 109 | 'nopart': self.params.get('nopart', False), |
95d8f7ea S |
110 | 'test': self.params.get('test', False), |
111 | } | |
112 | ) | |
113 | tmpfilename = self.temp_name(ctx['filename']) | |
75a24854 RA |
114 | open_mode = 'wb' |
115 | resume_len = 0 | |
d3f0687c | 116 | |
75a24854 RA |
117 | # Establish possible resume length |
118 | if os.path.isfile(encodeFilename(tmpfilename)): | |
119 | open_mode = 'ab' | |
120 | resume_len = os.path.getsize(encodeFilename(tmpfilename)) | |
d3f0687c | 121 | |
adb4b03c S |
122 | # Should be initialized before ytdl file check |
123 | ctx.update({ | |
124 | 'tmpfilename': tmpfilename, | |
125 | 'fragment_index': 0, | |
126 | }) | |
d3f0687c | 127 | |
adb4b03c S |
128 | if self.__do_ytdl_file(ctx): |
129 | if os.path.isfile(encodeFilename(self.ytdl_filename(ctx['filename']))): | |
130 | self._read_ytdl_file(ctx) | |
131 | else: | |
132 | self._write_ytdl_file(ctx) | |
133 | if ctx['fragment_index'] > 0: | |
134 | assert resume_len > 0 | |
135 | else: | |
136 | assert resume_len == 0 | |
d3f0687c | 137 | |
75a24854 RA |
138 | dest_stream, tmpfilename = sanitize_open(tmpfilename, open_mode) |
139 | ||
95d8f7ea S |
140 | ctx.update({ |
141 | 'dl': dl, | |
142 | 'dest_stream': dest_stream, | |
143 | 'tmpfilename': tmpfilename, | |
75a24854 RA |
144 | # Total complete fragments downloaded so far in bytes |
145 | 'complete_frags_downloaded_bytes': resume_len, | |
95d8f7ea S |
146 | }) |
147 | ||
148 | def _start_frag_download(self, ctx): | |
149 | total_frags = ctx['total_frags'] | |
150 | # This dict stores the download progress, it's updated by the progress | |
151 | # hook | |
152 | state = { | |
153 | 'status': 'downloading', | |
75a24854 | 154 | 'downloaded_bytes': ctx['complete_frags_downloaded_bytes'], |
3e0304fe RA |
155 | 'fragment_index': ctx['fragment_index'], |
156 | 'fragment_count': total_frags, | |
95d8f7ea S |
157 | 'filename': ctx['filename'], |
158 | 'tmpfilename': ctx['tmpfilename'], | |
b83b782d S |
159 | } |
160 | ||
161 | start = time.time() | |
162 | ctx.update({ | |
163 | 'started': start, | |
709185a2 S |
164 | # Amount of fragment's bytes downloaded by the time of the previous |
165 | # frag progress hook invocation | |
b83b782d S |
166 | 'prev_frag_downloaded_bytes': 0, |
167 | }) | |
95d8f7ea S |
168 | |
169 | def frag_progress_hook(s): | |
170 | if s['status'] not in ('downloading', 'finished'): | |
171 | return | |
172 | ||
5fa1702c | 173 | time_now = time.time() |
2c2f1efd | 174 | state['elapsed'] = time_now - start |
3c91e416 | 175 | frag_total_bytes = s.get('total_bytes') or 0 |
5fa1702c S |
176 | if not ctx['live']: |
177 | estimated_size = ( | |
178 | (ctx['complete_frags_downloaded_bytes'] + frag_total_bytes) / | |
3e0304fe | 179 | (state['fragment_index'] + 1) * total_frags) |
5fa1702c | 180 | state['total_bytes_estimate'] = estimated_size |
95d8f7ea | 181 | |
709185a2 | 182 | if s['status'] == 'finished': |
3e0304fe RA |
183 | state['fragment_index'] += 1 |
184 | ctx['fragment_index'] = state['fragment_index'] | |
b83b782d S |
185 | state['downloaded_bytes'] += frag_total_bytes - ctx['prev_frag_downloaded_bytes'] |
186 | ctx['complete_frags_downloaded_bytes'] = state['downloaded_bytes'] | |
187 | ctx['prev_frag_downloaded_bytes'] = 0 | |
709185a2 S |
188 | else: |
189 | frag_downloaded_bytes = s['downloaded_bytes'] | |
b83b782d | 190 | state['downloaded_bytes'] += frag_downloaded_bytes - ctx['prev_frag_downloaded_bytes'] |
5fa1702c S |
191 | if not ctx['live']: |
192 | state['eta'] = self.calc_eta( | |
193 | start, time_now, estimated_size, | |
194 | state['downloaded_bytes']) | |
1b5284b1 S |
195 | state['speed'] = s.get('speed') or ctx.get('speed') |
196 | ctx['speed'] = state['speed'] | |
b83b782d | 197 | ctx['prev_frag_downloaded_bytes'] = frag_downloaded_bytes |
95d8f7ea S |
198 | self._hook_progress(state) |
199 | ||
200 | ctx['dl'].add_progress_hook(frag_progress_hook) | |
201 | ||
202 | return start | |
203 | ||
204 | def _finish_frag_download(self, ctx): | |
205 | ctx['dest_stream'].close() | |
adb4b03c S |
206 | if self.__do_ytdl_file(ctx): |
207 | ytdl_filename = encodeFilename(self.ytdl_filename(ctx['filename'])) | |
208 | if os.path.isfile(ytdl_filename): | |
209 | os.remove(ytdl_filename) | |
95d8f7ea S |
210 | elapsed = time.time() - ctx['started'] |
211 | self.try_rename(ctx['tmpfilename'], ctx['filename']) | |
212 | fsize = os.path.getsize(encodeFilename(ctx['filename'])) | |
213 | ||
214 | self._hook_progress({ | |
215 | 'downloaded_bytes': fsize, | |
216 | 'total_bytes': fsize, | |
217 | 'filename': ctx['filename'], | |
218 | 'status': 'finished', | |
219 | 'elapsed': elapsed, | |
220 | }) |