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