]>
jfr.im git - yt-dlp.git/blob - youtube_dl/downloader/fragment.py
1 from __future__
import division
, unicode_literals
6 from .common
import FileDownloader
7 from .http
import HttpFD
14 class HttpQuietDownloader(HttpFD
):
15 def to_screen(self
, *args
, **kargs
):
19 class FragmentFD(FileDownloader
):
21 A base file downloader class for fragmented media (e.g. f4m/m3u8 manifests).
24 def _prepare_and_start_frag_download(self
, ctx
):
25 self
._prepare
_frag
_download
(ctx
)
26 self
._start
_frag
_download
(ctx
)
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(
37 'ratelimit': self
.params
.get('ratelimit', None),
38 'retries': self
.params
.get('retries', 0),
39 'test': self
.params
.get('test', False),
42 tmpfilename
= self
.temp_name(ctx
['filename'])
43 dest_stream
, tmpfilename
= sanitize_open(tmpfilename
, 'wb')
46 'dest_stream': dest_stream
,
47 'tmpfilename': tmpfilename
,
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
55 'status': 'downloading',
56 'downloaded_bytes': 0,
58 'frag_count': total_frags
,
59 'filename': ctx
['filename'],
60 'tmpfilename': ctx
['tmpfilename'],
63 ctx
['started'] = start
65 def frag_progress_hook(s
):
66 if s
['status'] not in ('downloading', 'finished'):
69 frag_total_bytes
= s
.get('total_bytes', 0)
70 if s
['status'] == 'finished':
71 state
['downloaded_bytes'] += frag_total_bytes
72 state
['frag_index'] += 1
75 (state
['downloaded_bytes'] + frag_total_bytes
) /
76 (state
['frag_index'] + 1) * total_frags
)
77 time_now
= time
.time()
78 state
['total_bytes_estimate'] = estimated_size
79 state
['elapsed'] = time_now
- start
81 if s
['status'] == 'finished':
82 progress
= self
.calc_percent(state
['frag_index'], total_frags
)
84 frag_downloaded_bytes
= s
['downloaded_bytes']
85 frag_progress
= self
.calc_percent(frag_downloaded_bytes
,
87 progress
= self
.calc_percent(state
['frag_index'], total_frags
)
88 progress
+= frag_progress
/ float(total_frags
)
90 state
['eta'] = self
.calc_eta(
91 start
, time_now
, estimated_size
, state
['downloaded_bytes'] + frag_downloaded_bytes
)
92 state
['speed'] = s
.get('speed')
93 self
._hook
_progress
(state
)
95 ctx
['dl'].add_progress_hook(frag_progress_hook
)
99 def _finish_frag_download(self
, ctx
):
100 ctx
['dest_stream'].close()
101 elapsed
= time
.time() - ctx
['started']
102 self
.try_rename(ctx
['tmpfilename'], ctx
['filename'])
103 fsize
= os
.path
.getsize(encodeFilename(ctx
['filename']))
105 self
._hook
_progress
({
106 'downloaded_bytes': fsize
,
107 'total_bytes': fsize
,
108 'filename': ctx
['filename'],
109 'status': 'finished',