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