]> jfr.im git - yt-dlp.git/blob - youtube_dl/downloader/fragment.py
Merge branch 'dcn' of https://github.com/remitamine/youtube-dl into remitamine-dcn
[yt-dlp.git] / youtube_dl / downloader / fragment.py
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 (
9 encodeFilename,
10 sanitize_open,
11 )
12
13
14 class HttpQuietDownloader(HttpFD):
15 def to_screen(self, *args, **kargs):
16 pass
17
18
19 class 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),
38 'test': self.params.get('test', False),
39 }
40 )
41 tmpfilename = self.temp_name(ctx['filename'])
42 dest_stream, tmpfilename = sanitize_open(tmpfilename, 'wb')
43 ctx.update({
44 'dl': dl,
45 'dest_stream': dest_stream,
46 'tmpfilename': tmpfilename,
47 })
48
49 def _start_frag_download(self, ctx):
50 total_frags = ctx['total_frags']
51 # This dict stores the download progress, it's updated by the progress
52 # hook
53 state = {
54 'status': 'downloading',
55 'downloaded_bytes': 0,
56 'frag_index': 0,
57 'frag_count': total_frags,
58 'filename': ctx['filename'],
59 'tmpfilename': ctx['tmpfilename'],
60 }
61 start = time.time()
62 ctx['started'] = start
63
64 def frag_progress_hook(s):
65 if s['status'] not in ('downloading', 'finished'):
66 return
67
68 frag_total_bytes = s.get('total_bytes', 0)
69 if s['status'] == 'finished':
70 state['downloaded_bytes'] += frag_total_bytes
71 state['frag_index'] += 1
72
73 estimated_size = (
74 (state['downloaded_bytes'] + frag_total_bytes) /
75 (state['frag_index'] + 1) * total_frags)
76 time_now = time.time()
77 state['total_bytes_estimate'] = estimated_size
78 state['elapsed'] = time_now - start
79
80 if s['status'] == 'finished':
81 progress = self.calc_percent(state['frag_index'], total_frags)
82 else:
83 frag_downloaded_bytes = s['downloaded_bytes']
84 frag_progress = self.calc_percent(frag_downloaded_bytes,
85 frag_total_bytes)
86 progress = self.calc_percent(state['frag_index'], total_frags)
87 progress += frag_progress / float(total_frags)
88
89 state['eta'] = self.calc_eta(
90 start, time_now, estimated_size, state['downloaded_bytes'] + frag_downloaded_bytes)
91 state['speed'] = s.get('speed')
92 self._hook_progress(state)
93
94 ctx['dl'].add_progress_hook(frag_progress_hook)
95
96 return start
97
98 def _finish_frag_download(self, ctx):
99 ctx['dest_stream'].close()
100 elapsed = time.time() - ctx['started']
101 self.try_rename(ctx['tmpfilename'], ctx['filename'])
102 fsize = os.path.getsize(encodeFilename(ctx['filename']))
103
104 self._hook_progress({
105 'downloaded_bytes': fsize,
106 'total_bytes': fsize,
107 'filename': ctx['filename'],
108 'status': 'finished',
109 'elapsed': elapsed,
110 })