]> jfr.im git - yt-dlp.git/blob - youtube_dl/downloader/rtmp.py
Do not resume live streams
[yt-dlp.git] / youtube_dl / downloader / rtmp.py
1 from __future__ import unicode_literals
2
3 import os
4 import re
5 import subprocess
6 import sys
7 import time
8
9 from .common import FileDownloader
10 from ..utils import (
11 encodeFilename,
12 format_bytes,
13 )
14
15
16 class RtmpFD(FileDownloader):
17 def real_download(self, filename, info_dict):
18 def run_rtmpdump(args):
19 start = time.time()
20 resume_percent = None
21 resume_downloaded_data_len = None
22 proc = subprocess.Popen(args, stderr=subprocess.PIPE)
23 cursor_in_new_line = True
24 proc_stderr_closed = False
25 while not proc_stderr_closed:
26 # read line from stderr
27 line = ''
28 while True:
29 char = proc.stderr.read(1)
30 if not char:
31 proc_stderr_closed = True
32 break
33 if char in [b'\r', b'\n']:
34 break
35 line += char.decode('ascii', 'replace')
36 if not line:
37 # proc_stderr_closed is True
38 continue
39 mobj = re.search(r'([0-9]+\.[0-9]{3}) kB / [0-9]+\.[0-9]{2} sec \(([0-9]{1,2}\.[0-9])%\)', line)
40 if mobj:
41 downloaded_data_len = int(float(mobj.group(1))*1024)
42 percent = float(mobj.group(2))
43 if not resume_percent:
44 resume_percent = percent
45 resume_downloaded_data_len = downloaded_data_len
46 eta = self.calc_eta(start, time.time(), 100-resume_percent, percent-resume_percent)
47 speed = self.calc_speed(start, time.time(), downloaded_data_len-resume_downloaded_data_len)
48 data_len = None
49 if percent > 0:
50 data_len = int(downloaded_data_len * 100 / percent)
51 data_len_str = '~' + format_bytes(data_len)
52 self.report_progress(percent, data_len_str, speed, eta)
53 cursor_in_new_line = False
54 self._hook_progress({
55 'downloaded_bytes': downloaded_data_len,
56 'total_bytes': data_len,
57 'tmpfilename': tmpfilename,
58 'filename': filename,
59 'status': 'downloading',
60 'eta': eta,
61 'speed': speed,
62 })
63 else:
64 # no percent for live streams
65 mobj = re.search(r'([0-9]+\.[0-9]{3}) kB / [0-9]+\.[0-9]{2} sec', line)
66 if mobj:
67 downloaded_data_len = int(float(mobj.group(1))*1024)
68 time_now = time.time()
69 speed = self.calc_speed(start, time_now, downloaded_data_len)
70 self.report_progress_live_stream(downloaded_data_len, speed, time_now - start)
71 cursor_in_new_line = False
72 self._hook_progress({
73 'downloaded_bytes': downloaded_data_len,
74 'tmpfilename': tmpfilename,
75 'filename': filename,
76 'status': 'downloading',
77 'speed': speed,
78 })
79 elif self.params.get('verbose', False):
80 if not cursor_in_new_line:
81 self.to_screen('')
82 cursor_in_new_line = True
83 self.to_screen('[rtmpdump] '+line)
84 proc.wait()
85 if not cursor_in_new_line:
86 self.to_screen('')
87 return proc.returncode
88
89 url = info_dict['url']
90 player_url = info_dict.get('player_url', None)
91 page_url = info_dict.get('page_url', None)
92 app = info_dict.get('app', None)
93 play_path = info_dict.get('play_path', None)
94 tc_url = info_dict.get('tc_url', None)
95 flash_version = info_dict.get('flash_version', None)
96 live = info_dict.get('rtmp_live', False)
97 conn = info_dict.get('rtmp_conn', None)
98
99 self.report_destination(filename)
100 tmpfilename = self.temp_name(filename)
101 test = self.params.get('test', False)
102
103 # Check for rtmpdump first
104 try:
105 subprocess.call(['rtmpdump', '-h'], stdout=(open(os.path.devnull, 'w')), stderr=subprocess.STDOUT)
106 except (OSError, IOError):
107 self.report_error('RTMP download detected but "rtmpdump" could not be run')
108 return False
109
110 # Download using rtmpdump. rtmpdump returns exit code 2 when
111 # the connection was interrumpted and resuming appears to be
112 # possible. This is part of rtmpdump's normal usage, AFAIK.
113 basic_args = ['rtmpdump', '--verbose', '-r', url, '-o', tmpfilename]
114 if player_url is not None:
115 basic_args += ['--swfVfy', player_url]
116 if page_url is not None:
117 basic_args += ['--pageUrl', page_url]
118 if app is not None:
119 basic_args += ['--app', app]
120 if play_path is not None:
121 basic_args += ['--playpath', play_path]
122 if tc_url is not None:
123 basic_args += ['--tcUrl', url]
124 if test:
125 basic_args += ['--stop', '1']
126 if flash_version is not None:
127 basic_args += ['--flashVer', flash_version]
128 if live:
129 basic_args += ['--live']
130 if conn:
131 basic_args += ['--conn', conn]
132 args = basic_args + [[], ['--resume', '--skip', '1']][not live and self.params.get('continuedl', False)]
133
134 if sys.platform == 'win32' and sys.version_info < (3, 0):
135 # Windows subprocess module does not actually support Unicode
136 # on Python 2.x
137 # See http://stackoverflow.com/a/9951851/35070
138 subprocess_encoding = sys.getfilesystemencoding()
139 args = [a.encode(subprocess_encoding, 'ignore') for a in args]
140 else:
141 subprocess_encoding = None
142
143 if self.params.get('verbose', False):
144 if subprocess_encoding:
145 str_args = [
146 a.decode(subprocess_encoding) if isinstance(a, bytes) else a
147 for a in args]
148 else:
149 str_args = args
150 try:
151 import pipes
152 shell_quote = lambda args: ' '.join(map(pipes.quote, str_args))
153 except ImportError:
154 shell_quote = repr
155 self.to_screen('[debug] rtmpdump command line: ' + shell_quote(str_args))
156
157 RD_SUCCESS = 0
158 RD_FAILED = 1
159 RD_INCOMPLETE = 2
160 RD_NO_CONNECT = 3
161
162 retval = run_rtmpdump(args)
163
164 if retval == RD_NO_CONNECT:
165 self.report_error('[rtmpdump] Could not connect to RTMP server.')
166 return False
167
168 while (retval == RD_INCOMPLETE or retval == RD_FAILED) and not test and not live:
169 prevsize = os.path.getsize(encodeFilename(tmpfilename))
170 self.to_screen('[rtmpdump] %s bytes' % prevsize)
171 time.sleep(5.0) # This seems to be needed
172 retval = run_rtmpdump(basic_args + ['-e'] + [[], ['-k', '1']][retval == RD_FAILED])
173 cursize = os.path.getsize(encodeFilename(tmpfilename))
174 if prevsize == cursize and retval == RD_FAILED:
175 break
176 # Some rtmp streams seem abort after ~ 99.8%. Don't complain for those
177 if prevsize == cursize and retval == RD_INCOMPLETE and cursize > 1024:
178 self.to_screen('[rtmpdump] Could not download the whole video. This can happen for some advertisements.')
179 retval = RD_SUCCESS
180 break
181 if retval == RD_SUCCESS or (test and retval == RD_INCOMPLETE):
182 fsize = os.path.getsize(encodeFilename(tmpfilename))
183 self.to_screen('[rtmpdump] %s bytes' % fsize)
184 self.try_rename(tmpfilename, filename)
185 self._hook_progress({
186 'downloaded_bytes': fsize,
187 'total_bytes': fsize,
188 'filename': filename,
189 'status': 'finished',
190 })
191 return True
192 else:
193 self.to_stderr('\n')
194 self.report_error('rtmpdump exited with code %d' % retval)
195 return False