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