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