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