]> jfr.im git - yt-dlp.git/blame - youtube_dlc/downloader/fragment.py
Merge pull request #224 from kyuyeunk/vlive
[yt-dlp.git] / youtube_dlc / downloader / fragment.py
CommitLineData
95d8f7ea
S
1from __future__ import division, unicode_literals
2
3import os
4import time
ea0c2f21 5import json
95d8f7ea
S
6
7from .common import FileDownloader
8from .http import HttpFD
9from ..utils import (
2e99cd30 10 error_to_compat_str,
95d8f7ea
S
11 encodeFilename,
12 sanitize_open,
69035555 13 sanitized_Request,
95d8f7ea
S
14)
15
16
17class HttpQuietDownloader(HttpFD):
18 def to_screen(self, *args, **kargs):
19 pass
20
21
22class 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
290f64db 34
cefecac1 35 For each incomplete fragment download youtube-dlc keeps on disk a special
290f64db 36 bookkeeping file with download state and metadata (in future such files will
cefecac1 37 be used for any incomplete download handled by youtube-dlc). This file is
290f64db
S
38 used to properly handle resuming, check download file consistency and detect
39 potential errors. The file has a .ytdl extension and represents a standard
40 JSON file of the following format:
41
42 extractor:
43 Dictionary of extractor related data. TBD.
44
45 downloader:
46 Dictionary of downloader related data. May contain following data:
47 current_fragment:
48 Dictionary with current (being downloaded) fragment data:
85f6de25 49 index: 0-based index of current fragment among all fragments
290f64db
S
50 fragment_count:
51 Total count of fragments
50534b71 52
85f6de25 53 This feature is experimental and file format may change in future.
95d8f7ea
S
54 """
55
75a24854 56 def report_retry_fragment(self, err, frag_index, count, retries):
721f26b8 57 self.to_screen(
75a24854
RA
58 '[download] Got server HTTP error: %s. Retrying fragment %d (attempt %d of %s)...'
59 % (error_to_compat_str(err), frag_index, count, self.format_retries(retries)))
721f26b8 60
75a24854
RA
61 def report_skip_fragment(self, frag_index):
62 self.to_screen('[download] Skipping fragment %d...' % frag_index)
9603b660 63
69035555
S
64 def _prepare_url(self, info_dict, url):
65 headers = info_dict.get('http_headers')
66 return sanitized_Request(url, None, headers) if headers else url
67
95d8f7ea
S
68 def _prepare_and_start_frag_download(self, ctx):
69 self._prepare_frag_download(ctx)
70 self._start_frag_download(ctx)
71
adb4b03c
S
72 @staticmethod
73 def __do_ytdl_file(ctx):
74 return not ctx['live'] and not ctx['tmpfilename'] == '-'
75
d3f0687c 76 def _read_ytdl_file(self, ctx):
500a86a5 77 assert 'ytdl_corrupt' not in ctx
d3f0687c 78 stream, _ = sanitize_open(self.ytdl_filename(ctx['filename']), 'r')
500a86a5
S
79 try:
80 ctx['fragment_index'] = json.loads(stream.read())['downloader']['current_fragment']['index']
81 except Exception:
82 ctx['ytdl_corrupt'] = True
83 finally:
84 stream.close()
d3f0687c
S
85
86 def _write_ytdl_file(self, ctx):
87 frag_index_stream, _ = sanitize_open(self.ytdl_filename(ctx['filename']), 'w')
290f64db
S
88 downloader = {
89 'current_fragment': {
90 'index': ctx['fragment_index'],
d3f0687c 91 },
290f64db
S
92 }
93 if ctx.get('fragment_count') is not None:
94 downloader['fragment_count'] = ctx['fragment_count']
95 frag_index_stream.write(json.dumps({'downloader': downloader}))
d3f0687c
S
96 frag_index_stream.close()
97
75a24854 98 def _download_fragment(self, ctx, frag_url, info_dict, headers=None):
d3f0687c 99 fragment_filename = '%s-Frag%d' % (ctx['tmpfilename'], ctx['fragment_index'])
38d70284 100 fragment_info_dict = {
75a24854
RA
101 'url': frag_url,
102 'http_headers': headers or info_dict.get('http_headers'),
38d70284 103 }
104 success = ctx['dl'].download(fragment_filename, fragment_info_dict)
75a24854
RA
105 if not success:
106 return False, None
38d70284 107 if fragment_info_dict.get('filetime'):
108 ctx['fragment_filetime'] = fragment_info_dict.get('filetime')
d3f0687c
S
109 down, frag_sanitized = sanitize_open(fragment_filename, 'rb')
110 ctx['fragment_filename_sanitized'] = frag_sanitized
111 frag_content = down.read()
75a24854
RA
112 down.close()
113 return True, frag_content
114
115 def _append_fragment(self, ctx, frag_content):
d3f0687c
S
116 try:
117 ctx['dest_stream'].write(frag_content)
593f2f79 118 ctx['dest_stream'].flush()
d3f0687c 119 finally:
adb4b03c 120 if self.__do_ytdl_file(ctx):
d3f0687c 121 self._write_ytdl_file(ctx)
0eee52f3 122 if not self.params.get('keep_fragments', False):
99081da9 123 os.remove(encodeFilename(ctx['fragment_filename_sanitized']))
d3f0687c 124 del ctx['fragment_filename_sanitized']
75a24854 125
95d8f7ea 126 def _prepare_frag_download(self, ctx):
5fa1702c
S
127 if 'live' not in ctx:
128 ctx['live'] = False
5efaf43c
S
129 if not ctx['live']:
130 total_frags_str = '%d' % ctx['total_frags']
131 ad_frags = ctx.get('ad_frags', 0)
132 if ad_frags:
133 total_frags_str += ' (not including %d ad)' % ad_frags
134 else:
135 total_frags_str = 'unknown (live)'
5fa1702c 136 self.to_screen(
5efaf43c 137 '[%s] Total fragments: %s' % (self.FD_NAME, total_frags_str))
95d8f7ea
S
138 self.report_destination(ctx['filename'])
139 dl = HttpQuietDownloader(
140 self.ydl,
141 {
142 'continuedl': True,
143 'quiet': True,
144 'noprogress': True,
d800609c 145 'ratelimit': self.params.get('ratelimit'),
6828c809 146 'retries': self.params.get('retries', 0),
7097bffb 147 'nopart': self.params.get('nopart', False),
95d8f7ea
S
148 'test': self.params.get('test', False),
149 }
150 )
151 tmpfilename = self.temp_name(ctx['filename'])
75a24854
RA
152 open_mode = 'wb'
153 resume_len = 0
d3f0687c 154
75a24854
RA
155 # Establish possible resume length
156 if os.path.isfile(encodeFilename(tmpfilename)):
157 open_mode = 'ab'
158 resume_len = os.path.getsize(encodeFilename(tmpfilename))
d3f0687c 159
adb4b03c
S
160 # Should be initialized before ytdl file check
161 ctx.update({
162 'tmpfilename': tmpfilename,
163 'fragment_index': 0,
164 })
d3f0687c 165
adb4b03c
S
166 if self.__do_ytdl_file(ctx):
167 if os.path.isfile(encodeFilename(self.ytdl_filename(ctx['filename']))):
168 self._read_ytdl_file(ctx)
500a86a5
S
169 is_corrupt = ctx.get('ytdl_corrupt') is True
170 is_inconsistent = ctx['fragment_index'] > 0 and resume_len == 0
171 if is_corrupt or is_inconsistent:
172 message = (
173 '.ytdl file is corrupt' if is_corrupt else
174 'Inconsistent state of incomplete fragment download')
6f3b4a98 175 self.report_warning(
500a86a5 176 '%s. Restarting from the beginning...' % message)
e7c3e334 177 ctx['fragment_index'] = resume_len = 0
500a86a5
S
178 if 'ytdl_corrupt' in ctx:
179 del ctx['ytdl_corrupt']
e7c3e334 180 self._write_ytdl_file(ctx)
adb4b03c
S
181 else:
182 self._write_ytdl_file(ctx)
e7c3e334 183 assert ctx['fragment_index'] == 0
d3f0687c 184
75a24854
RA
185 dest_stream, tmpfilename = sanitize_open(tmpfilename, open_mode)
186
95d8f7ea
S
187 ctx.update({
188 'dl': dl,
189 'dest_stream': dest_stream,
190 'tmpfilename': tmpfilename,
75a24854
RA
191 # Total complete fragments downloaded so far in bytes
192 'complete_frags_downloaded_bytes': resume_len,
95d8f7ea
S
193 })
194
195 def _start_frag_download(self, ctx):
3bce4ff7 196 resume_len = ctx['complete_frags_downloaded_bytes']
95d8f7ea
S
197 total_frags = ctx['total_frags']
198 # This dict stores the download progress, it's updated by the progress
199 # hook
200 state = {
201 'status': 'downloading',
3bce4ff7 202 'downloaded_bytes': resume_len,
3e0304fe
RA
203 'fragment_index': ctx['fragment_index'],
204 'fragment_count': total_frags,
95d8f7ea
S
205 'filename': ctx['filename'],
206 'tmpfilename': ctx['tmpfilename'],
b83b782d
S
207 }
208
209 start = time.time()
210 ctx.update({
211 'started': start,
709185a2
S
212 # Amount of fragment's bytes downloaded by the time of the previous
213 # frag progress hook invocation
b83b782d
S
214 'prev_frag_downloaded_bytes': 0,
215 })
95d8f7ea
S
216
217 def frag_progress_hook(s):
218 if s['status'] not in ('downloading', 'finished'):
219 return
220
5fa1702c 221 time_now = time.time()
2c2f1efd 222 state['elapsed'] = time_now - start
3c91e416 223 frag_total_bytes = s.get('total_bytes') or 0
5fa1702c
S
224 if not ctx['live']:
225 estimated_size = (
3089bc74
S
226 (ctx['complete_frags_downloaded_bytes'] + frag_total_bytes)
227 / (state['fragment_index'] + 1) * total_frags)
5fa1702c 228 state['total_bytes_estimate'] = estimated_size
95d8f7ea 229
709185a2 230 if s['status'] == 'finished':
3e0304fe
RA
231 state['fragment_index'] += 1
232 ctx['fragment_index'] = state['fragment_index']
b83b782d
S
233 state['downloaded_bytes'] += frag_total_bytes - ctx['prev_frag_downloaded_bytes']
234 ctx['complete_frags_downloaded_bytes'] = state['downloaded_bytes']
235 ctx['prev_frag_downloaded_bytes'] = 0
709185a2
S
236 else:
237 frag_downloaded_bytes = s['downloaded_bytes']
b83b782d 238 state['downloaded_bytes'] += frag_downloaded_bytes - ctx['prev_frag_downloaded_bytes']
5fa1702c
S
239 if not ctx['live']:
240 state['eta'] = self.calc_eta(
3bce4ff7 241 start, time_now, estimated_size - resume_len,
242 state['downloaded_bytes'] - resume_len)
1b5284b1
S
243 state['speed'] = s.get('speed') or ctx.get('speed')
244 ctx['speed'] = state['speed']
b83b782d 245 ctx['prev_frag_downloaded_bytes'] = frag_downloaded_bytes
95d8f7ea
S
246 self._hook_progress(state)
247
248 ctx['dl'].add_progress_hook(frag_progress_hook)
249
250 return start
251
252 def _finish_frag_download(self, ctx):
253 ctx['dest_stream'].close()
adb4b03c
S
254 if self.__do_ytdl_file(ctx):
255 ytdl_filename = encodeFilename(self.ytdl_filename(ctx['filename']))
256 if os.path.isfile(ytdl_filename):
257 os.remove(ytdl_filename)
95d8f7ea 258 elapsed = time.time() - ctx['started']
0ff2c1ec
S
259
260 if ctx['tmpfilename'] == '-':
261 downloaded_bytes = ctx['complete_frags_downloaded_bytes']
262 else:
263 self.try_rename(ctx['tmpfilename'], ctx['filename'])
38d70284 264 if self.params.get('updatetime', True):
265 filetime = ctx.get('fragment_filetime')
266 if filetime:
267 try:
268 os.utime(ctx['filename'], (time.time(), filetime))
269 except Exception:
270 pass
0ff2c1ec 271 downloaded_bytes = os.path.getsize(encodeFilename(ctx['filename']))
95d8f7ea
S
272
273 self._hook_progress({
0ff2c1ec
S
274 'downloaded_bytes': downloaded_bytes,
275 'total_bytes': downloaded_bytes,
95d8f7ea
S
276 'filename': ctx['filename'],
277 'status': 'finished',
278 'elapsed': elapsed,
279 })