]>
Commit | Line | Data |
---|---|---|
95d8f7ea S |
1 | from __future__ import division, unicode_literals |
2 | ||
3 | import os | |
4 | import time | |
5 | ||
6 | from .common import FileDownloader | |
7 | from .http import HttpFD | |
8 | from ..utils import ( | |
2e99cd30 | 9 | error_to_compat_str, |
95d8f7ea S |
10 | encodeFilename, |
11 | sanitize_open, | |
12 | ) | |
13 | ||
14 | ||
15 | class HttpQuietDownloader(HttpFD): | |
16 | def to_screen(self, *args, **kargs): | |
17 | pass | |
18 | ||
19 | ||
20 | class FragmentFD(FileDownloader): | |
21 | """ | |
22 | A base file downloader class for fragmented media (e.g. f4m/m3u8 manifests). | |
16a8b798 S |
23 | |
24 | Available options: | |
25 | ||
9603b660 S |
26 | fragment_retries: Number of times to retry a fragment for HTTP error (DASH |
27 | and hlsnative only) | |
28 | skip_unavailable_fragments: | |
29 | Skip unavailable fragments (DASH and hlsnative only) | |
95d8f7ea S |
30 | """ |
31 | ||
2e99cd30 | 32 | def report_retry_fragment(self, err, fragment_name, count, retries): |
721f26b8 | 33 | self.to_screen( |
9603b660 | 34 | '[download] Got server HTTP error: %s. Retrying fragment %s (attempt %d of %s)...' |
2e99cd30 | 35 | % (error_to_compat_str(err), fragment_name, count, self.format_retries(retries))) |
721f26b8 | 36 | |
9603b660 S |
37 | def report_skip_fragment(self, fragment_name): |
38 | self.to_screen('[download] Skipping fragment %s...' % fragment_name) | |
39 | ||
95d8f7ea S |
40 | def _prepare_and_start_frag_download(self, ctx): |
41 | self._prepare_frag_download(ctx) | |
42 | self._start_frag_download(ctx) | |
43 | ||
44 | def _prepare_frag_download(self, ctx): | |
5fa1702c S |
45 | if 'live' not in ctx: |
46 | ctx['live'] = False | |
47 | self.to_screen( | |
48 | '[%s] Total fragments: %s' | |
49 | % (self.FD_NAME, ctx['total_frags'] if not ctx['live'] else 'unknown (live)')) | |
95d8f7ea S |
50 | self.report_destination(ctx['filename']) |
51 | dl = HttpQuietDownloader( | |
52 | self.ydl, | |
53 | { | |
54 | 'continuedl': True, | |
55 | 'quiet': True, | |
56 | 'noprogress': True, | |
d800609c | 57 | 'ratelimit': self.params.get('ratelimit'), |
6828c809 | 58 | 'retries': self.params.get('retries', 0), |
95d8f7ea S |
59 | 'test': self.params.get('test', False), |
60 | } | |
61 | ) | |
62 | tmpfilename = self.temp_name(ctx['filename']) | |
63 | dest_stream, tmpfilename = sanitize_open(tmpfilename, 'wb') | |
64 | ctx.update({ | |
65 | 'dl': dl, | |
66 | 'dest_stream': dest_stream, | |
67 | 'tmpfilename': tmpfilename, | |
68 | }) | |
69 | ||
70 | def _start_frag_download(self, ctx): | |
71 | total_frags = ctx['total_frags'] | |
72 | # This dict stores the download progress, it's updated by the progress | |
73 | # hook | |
74 | state = { | |
75 | 'status': 'downloading', | |
76 | 'downloaded_bytes': 0, | |
77 | 'frag_index': 0, | |
78 | 'frag_count': total_frags, | |
79 | 'filename': ctx['filename'], | |
80 | 'tmpfilename': ctx['tmpfilename'], | |
b83b782d S |
81 | } |
82 | ||
83 | start = time.time() | |
84 | ctx.update({ | |
85 | 'started': start, | |
709185a2 | 86 | # Total complete fragments downloaded so far in bytes |
b83b782d | 87 | 'complete_frags_downloaded_bytes': 0, |
709185a2 S |
88 | # Amount of fragment's bytes downloaded by the time of the previous |
89 | # frag progress hook invocation | |
b83b782d S |
90 | 'prev_frag_downloaded_bytes': 0, |
91 | }) | |
95d8f7ea S |
92 | |
93 | def frag_progress_hook(s): | |
94 | if s['status'] not in ('downloading', 'finished'): | |
95 | return | |
96 | ||
5fa1702c | 97 | time_now = time.time() |
2c2f1efd | 98 | state['elapsed'] = time_now - start |
3c91e416 | 99 | frag_total_bytes = s.get('total_bytes') or 0 |
5fa1702c S |
100 | if not ctx['live']: |
101 | estimated_size = ( | |
102 | (ctx['complete_frags_downloaded_bytes'] + frag_total_bytes) / | |
103 | (state['frag_index'] + 1) * total_frags) | |
104 | state['total_bytes_estimate'] = estimated_size | |
95d8f7ea | 105 | |
709185a2 S |
106 | if s['status'] == 'finished': |
107 | state['frag_index'] += 1 | |
b83b782d S |
108 | state['downloaded_bytes'] += frag_total_bytes - ctx['prev_frag_downloaded_bytes'] |
109 | ctx['complete_frags_downloaded_bytes'] = state['downloaded_bytes'] | |
110 | ctx['prev_frag_downloaded_bytes'] = 0 | |
709185a2 S |
111 | else: |
112 | frag_downloaded_bytes = s['downloaded_bytes'] | |
b83b782d | 113 | state['downloaded_bytes'] += frag_downloaded_bytes - ctx['prev_frag_downloaded_bytes'] |
5fa1702c S |
114 | if not ctx['live']: |
115 | state['eta'] = self.calc_eta( | |
116 | start, time_now, estimated_size, | |
117 | state['downloaded_bytes']) | |
1b5284b1 S |
118 | state['speed'] = s.get('speed') or ctx.get('speed') |
119 | ctx['speed'] = state['speed'] | |
b83b782d | 120 | ctx['prev_frag_downloaded_bytes'] = frag_downloaded_bytes |
95d8f7ea S |
121 | self._hook_progress(state) |
122 | ||
123 | ctx['dl'].add_progress_hook(frag_progress_hook) | |
124 | ||
125 | return start | |
126 | ||
127 | def _finish_frag_download(self, ctx): | |
128 | ctx['dest_stream'].close() | |
129 | elapsed = time.time() - ctx['started'] | |
130 | self.try_rename(ctx['tmpfilename'], ctx['filename']) | |
131 | fsize = os.path.getsize(encodeFilename(ctx['filename'])) | |
132 | ||
133 | self._hook_progress({ | |
134 | 'downloaded_bytes': fsize, | |
135 | 'total_bytes': fsize, | |
136 | 'filename': ctx['filename'], | |
137 | 'status': 'finished', | |
138 | 'elapsed': elapsed, | |
139 | }) |