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