[downloader/fragment] Don't process ytdl file when it's not needed yet
[youtube-dl] / youtube_dl / downloader / fragment.py
1 from __future__ import division, unicode_literals
2
3 import os
4 import time
5 import json
6
7 from .common import FileDownloader
8 from .http import HttpFD
9 from ..utils import (
10     error_to_compat_str,
11     encodeFilename,
12     sanitize_open,
13     sanitized_Request,
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).
25
26     Available options:
27
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)
32     keep_fragments:     Keep downloaded fragments on disk after downloading is
33                         finished
34     """
35
36     def report_retry_fragment(self, err, frag_index, count, retries):
37         self.to_screen(
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)))
40
41     def report_skip_fragment(self, frag_index):
42         self.to_screen('[download] Skipping fragment %d...' % frag_index)
43
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
48     def _prepare_and_start_frag_download(self, ctx):
49         self._prepare_frag_download(ctx)
50         self._start_frag_download(ctx)
51
52     @staticmethod
53     def __do_ytdl_file(ctx):
54         return not ctx['live'] and not ctx['tmpfilename'] == '-'
55
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
70     def _download_fragment(self, ctx, frag_url, info_dict, headers=None):
71         fragment_filename = '%s-Frag%d' % (ctx['tmpfilename'], ctx['fragment_index'])
72         success = ctx['dl'].download(fragment_filename, {
73             'url': frag_url,
74             'http_headers': headers or info_dict.get('http_headers'),
75         })
76         if not success:
77             return False, None
78         down, frag_sanitized = sanitize_open(fragment_filename, 'rb')
79         ctx['fragment_filename_sanitized'] = frag_sanitized
80         frag_content = down.read()
81         down.close()
82         return True, frag_content
83
84     def _append_fragment(self, ctx, frag_content):
85         try:
86             ctx['dest_stream'].write(frag_content)
87         finally:
88             if self.__do_ytdl_file(ctx):
89                 self._write_ytdl_file(ctx)
90             if not self.params.get('keep_fragments', False):
91                 os.remove(ctx['fragment_filename_sanitized'])
92             del ctx['fragment_filename_sanitized']
93
94     def _prepare_frag_download(self, ctx):
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)'))
100         self.report_destination(ctx['filename'])
101         dl = HttpQuietDownloader(
102             self.ydl,
103             {
104                 'continuedl': True,
105                 'quiet': True,
106                 'noprogress': True,
107                 'ratelimit': self.params.get('ratelimit'),
108                 'retries': self.params.get('retries', 0),
109                 'nopart': self.params.get('nopart', False),
110                 'test': self.params.get('test', False),
111             }
112         )
113         tmpfilename = self.temp_name(ctx['filename'])
114         open_mode = 'wb'
115         resume_len = 0
116
117         # Establish possible resume length
118         if os.path.isfile(encodeFilename(tmpfilename)):
119             open_mode = 'ab'
120             resume_len = os.path.getsize(encodeFilename(tmpfilename))
121
122         # Should be initialized before ytdl file check
123         ctx.update({
124             'tmpfilename': tmpfilename,
125             'fragment_index': 0,
126         })
127
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
137
138         dest_stream, tmpfilename = sanitize_open(tmpfilename, open_mode)
139
140         ctx.update({
141             'dl': dl,
142             'dest_stream': dest_stream,
143             'tmpfilename': tmpfilename,
144             # Total complete fragments downloaded so far in bytes
145             'complete_frags_downloaded_bytes': resume_len,
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',
154             'downloaded_bytes': ctx['complete_frags_downloaded_bytes'],
155             'fragment_index': ctx['fragment_index'],
156             'fragment_count': total_frags,
157             'filename': ctx['filename'],
158             'tmpfilename': ctx['tmpfilename'],
159         }
160
161         start = time.time()
162         ctx.update({
163             'started': start,
164             # Amount of fragment's bytes downloaded by the time of the previous
165             # frag progress hook invocation
166             'prev_frag_downloaded_bytes': 0,
167         })
168
169         def frag_progress_hook(s):
170             if s['status'] not in ('downloading', 'finished'):
171                 return
172
173             time_now = time.time()
174             state['elapsed'] = time_now - start
175             frag_total_bytes = s.get('total_bytes') or 0
176             if not ctx['live']:
177                 estimated_size = (
178                     (ctx['complete_frags_downloaded_bytes'] + frag_total_bytes) /
179                     (state['fragment_index'] + 1) * total_frags)
180                 state['total_bytes_estimate'] = estimated_size
181
182             if s['status'] == 'finished':
183                 state['fragment_index'] += 1
184                 ctx['fragment_index'] = state['fragment_index']
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
188             else:
189                 frag_downloaded_bytes = s['downloaded_bytes']
190                 state['downloaded_bytes'] += frag_downloaded_bytes - ctx['prev_frag_downloaded_bytes']
191                 if not ctx['live']:
192                     state['eta'] = self.calc_eta(
193                         start, time_now, estimated_size,
194                         state['downloaded_bytes'])
195                 state['speed'] = s.get('speed') or ctx.get('speed')
196                 ctx['speed'] = state['speed']
197                 ctx['prev_frag_downloaded_bytes'] = frag_downloaded_bytes
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()
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)
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         })