]> jfr.im git - yt-dlp.git/blame - youtube_dl/downloader/fragment.py
[downloader/dash] Add fragment retry capability
[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).
22 """
23
721f26b8
S
24 def report_retry_fragment(self, fragment_name, count, retries):
25 self.to_screen(
26 '[download] Got server HTTP error. Retrying fragment %s (attempt %d of %.0f)...'
27 % (fragment_name, count, retries))
28
95d8f7ea
S
29 def _prepare_and_start_frag_download(self, ctx):
30 self._prepare_frag_download(ctx)
31 self._start_frag_download(ctx)
32
33 def _prepare_frag_download(self, ctx):
5fa1702c
S
34 if 'live' not in ctx:
35 ctx['live'] = False
36 self.to_screen(
37 '[%s] Total fragments: %s'
38 % (self.FD_NAME, ctx['total_frags'] if not ctx['live'] else 'unknown (live)'))
95d8f7ea
S
39 self.report_destination(ctx['filename'])
40 dl = HttpQuietDownloader(
41 self.ydl,
42 {
43 'continuedl': True,
44 'quiet': True,
45 'noprogress': True,
d800609c 46 'ratelimit': self.params.get('ratelimit'),
6828c809 47 'retries': self.params.get('retries', 0),
95d8f7ea
S
48 'test': self.params.get('test', False),
49 }
50 )
51 tmpfilename = self.temp_name(ctx['filename'])
52 dest_stream, tmpfilename = sanitize_open(tmpfilename, 'wb')
53 ctx.update({
54 'dl': dl,
55 'dest_stream': dest_stream,
56 'tmpfilename': tmpfilename,
57 })
58
59 def _start_frag_download(self, ctx):
60 total_frags = ctx['total_frags']
61 # This dict stores the download progress, it's updated by the progress
62 # hook
63 state = {
64 'status': 'downloading',
65 'downloaded_bytes': 0,
66 'frag_index': 0,
67 'frag_count': total_frags,
68 'filename': ctx['filename'],
69 'tmpfilename': ctx['tmpfilename'],
b83b782d
S
70 }
71
72 start = time.time()
73 ctx.update({
74 'started': start,
709185a2 75 # Total complete fragments downloaded so far in bytes
b83b782d 76 'complete_frags_downloaded_bytes': 0,
709185a2
S
77 # Amount of fragment's bytes downloaded by the time of the previous
78 # frag progress hook invocation
b83b782d
S
79 'prev_frag_downloaded_bytes': 0,
80 })
95d8f7ea
S
81
82 def frag_progress_hook(s):
83 if s['status'] not in ('downloading', 'finished'):
84 return
85
5fa1702c 86 time_now = time.time()
2c2f1efd 87 state['elapsed'] = time_now - start
3c91e416 88 frag_total_bytes = s.get('total_bytes') or 0
5fa1702c
S
89 if not ctx['live']:
90 estimated_size = (
91 (ctx['complete_frags_downloaded_bytes'] + frag_total_bytes) /
92 (state['frag_index'] + 1) * total_frags)
93 state['total_bytes_estimate'] = estimated_size
95d8f7ea 94
709185a2
S
95 if s['status'] == 'finished':
96 state['frag_index'] += 1
b83b782d
S
97 state['downloaded_bytes'] += frag_total_bytes - ctx['prev_frag_downloaded_bytes']
98 ctx['complete_frags_downloaded_bytes'] = state['downloaded_bytes']
99 ctx['prev_frag_downloaded_bytes'] = 0
709185a2
S
100 else:
101 frag_downloaded_bytes = s['downloaded_bytes']
b83b782d 102 state['downloaded_bytes'] += frag_downloaded_bytes - ctx['prev_frag_downloaded_bytes']
5fa1702c
S
103 if not ctx['live']:
104 state['eta'] = self.calc_eta(
105 start, time_now, estimated_size,
106 state['downloaded_bytes'])
1b5284b1
S
107 state['speed'] = s.get('speed') or ctx.get('speed')
108 ctx['speed'] = state['speed']
b83b782d 109 ctx['prev_frag_downloaded_bytes'] = frag_downloaded_bytes
95d8f7ea
S
110 self._hook_progress(state)
111
112 ctx['dl'].add_progress_hook(frag_progress_hook)
113
114 return start
115
116 def _finish_frag_download(self, ctx):
117 ctx['dest_stream'].close()
118 elapsed = time.time() - ctx['started']
119 self.try_rename(ctx['tmpfilename'], ctx['filename'])
120 fsize = os.path.getsize(encodeFilename(ctx['filename']))
121
122 self._hook_progress({
123 'downloaded_bytes': fsize,
124 'total_bytes': fsize,
125 'filename': ctx['filename'],
126 'status': 'finished',
127 'elapsed': elapsed,
128 })