]>
Commit | Line | Data |
---|---|---|
8dec03ec S |
1 | from __future__ import unicode_literals |
2 | ||
3bc2ddcc JMF |
3 | import os |
4 | import re | |
5 | import subprocess | |
3bc2ddcc JMF |
6 | import time |
7 | ||
8 | from .common import FileDownloader | |
1cc79574 | 9 | from ..compat import compat_str |
3bc2ddcc | 10 | from ..utils import ( |
7798fad5 | 11 | check_executable, |
3bc2ddcc | 12 | encodeFilename, |
9e105a85 | 13 | encodeArgument, |
4c83c967 | 14 | get_exe_version, |
3bc2ddcc JMF |
15 | ) |
16 | ||
17 | ||
4c83c967 PH |
18 | def rtmpdump_version(): |
19 | return get_exe_version( | |
20 | 'rtmpdump', ['--help'], r'(?i)RTMPDump\s*v?([0-9a-zA-Z._-]+)') | |
21 | ||
22 | ||
3bc2ddcc JMF |
23 | class RtmpFD(FileDownloader): |
24 | def real_download(self, filename, info_dict): | |
25 | def run_rtmpdump(args): | |
26 | start = time.time() | |
9b0b6275 S |
27 | resume_percent = None |
28 | resume_downloaded_data_len = None | |
3bc2ddcc JMF |
29 | proc = subprocess.Popen(args, stderr=subprocess.PIPE) |
30 | cursor_in_new_line = True | |
9b0b6275 S |
31 | proc_stderr_closed = False |
32 | try: | |
ddd8486a S |
33 | while not proc_stderr_closed: |
34 | # read line from stderr | |
35 | line = '' | |
36 | while True: | |
37 | char = proc.stderr.read(1) | |
38 | if not char: | |
39 | proc_stderr_closed = True | |
40 | break | |
41 | if char in [b'\r', b'\n']: | |
42 | break | |
43 | line += char.decode('ascii', 'replace') | |
44 | if not line: | |
45 | # proc_stderr_closed is True | |
46 | continue | |
47 | mobj = re.search(r'([0-9]+\.[0-9]{3}) kB / [0-9]+\.[0-9]{2} sec \(([0-9]{1,2}\.[0-9])%\)', line) | |
3bc2ddcc | 48 | if mobj: |
2514d263 | 49 | downloaded_data_len = int(float(mobj.group(1)) * 1024) |
ddd8486a S |
50 | percent = float(mobj.group(2)) |
51 | if not resume_percent: | |
52 | resume_percent = percent | |
53 | resume_downloaded_data_len = downloaded_data_len | |
3bc2ddcc | 54 | time_now = time.time() |
ddd8486a S |
55 | eta = self.calc_eta(start, time_now, 100 - resume_percent, percent - resume_percent) |
56 | speed = self.calc_speed(start, time_now, downloaded_data_len - resume_downloaded_data_len) | |
57 | data_len = None | |
58 | if percent > 0: | |
59 | data_len = int(downloaded_data_len * 100 / percent) | |
3bc2ddcc | 60 | self._hook_progress({ |
ddd8486a | 61 | 'status': 'downloading', |
3bc2ddcc | 62 | 'downloaded_bytes': downloaded_data_len, |
ddd8486a | 63 | 'total_bytes_estimate': data_len, |
3bc2ddcc JMF |
64 | 'tmpfilename': tmpfilename, |
65 | 'filename': filename, | |
ddd8486a | 66 | 'eta': eta, |
5cda4eda | 67 | 'elapsed': time_now - start, |
3bc2ddcc JMF |
68 | 'speed': speed, |
69 | }) | |
5cda4eda | 70 | cursor_in_new_line = False |
ddd8486a S |
71 | else: |
72 | # no percent for live streams | |
73 | mobj = re.search(r'([0-9]+\.[0-9]{3}) kB / [0-9]+\.[0-9]{2} sec', line) | |
74 | if mobj: | |
75 | downloaded_data_len = int(float(mobj.group(1)) * 1024) | |
76 | time_now = time.time() | |
77 | speed = self.calc_speed(start, time_now, downloaded_data_len) | |
78 | self._hook_progress({ | |
79 | 'downloaded_bytes': downloaded_data_len, | |
80 | 'tmpfilename': tmpfilename, | |
81 | 'filename': filename, | |
82 | 'status': 'downloading', | |
83 | 'elapsed': time_now - start, | |
84 | 'speed': speed, | |
85 | }) | |
86 | cursor_in_new_line = False | |
87 | elif self.params.get('verbose', False): | |
88 | if not cursor_in_new_line: | |
89 | self.to_screen('') | |
90 | cursor_in_new_line = True | |
91 | self.to_screen('[rtmpdump] ' + line) | |
f5b1bca9 | 92 | if not cursor_in_new_line: |
93 | self.to_screen('') | |
94 | return proc.wait() | |
95 | except BaseException: # Including KeyboardInterrupt | |
96 | proc.kill() | |
ddd8486a | 97 | proc.wait() |
f5b1bca9 | 98 | raise |
3bc2ddcc JMF |
99 | |
100 | url = info_dict['url'] | |
d800609c S |
101 | player_url = info_dict.get('player_url') |
102 | page_url = info_dict.get('page_url') | |
103 | app = info_dict.get('app') | |
104 | play_path = info_dict.get('play_path') | |
105 | tc_url = info_dict.get('tc_url') | |
106 | flash_version = info_dict.get('flash_version') | |
3bc2ddcc | 107 | live = info_dict.get('rtmp_live', False) |
d800609c S |
108 | conn = info_dict.get('rtmp_conn') |
109 | protocol = info_dict.get('rtmp_protocol') | |
7bb3ceb4 | 110 | real_time = info_dict.get('rtmp_real_time', False) |
7906d199 | 111 | no_resume = info_dict.get('no_resume', False) |
f101079a | 112 | continue_dl = self.params.get('continuedl', True) |
3dee7826 | 113 | |
3bc2ddcc JMF |
114 | self.report_destination(filename) |
115 | tmpfilename = self.temp_name(filename) | |
116 | test = self.params.get('test', False) | |
117 | ||
118 | # Check for rtmpdump first | |
7798fad5 | 119 | if not check_executable('rtmpdump', ['-h']): |
23566e0d | 120 | self.report_error('RTMP download detected but "rtmpdump" could not be run. Please install it.') |
3bc2ddcc JMF |
121 | return False |
122 | ||
123 | # Download using rtmpdump. rtmpdump returns exit code 2 when | |
17cc1534 | 124 | # the connection was interrupted and resuming appears to be |
3bc2ddcc | 125 | # possible. This is part of rtmpdump's normal usage, AFAIK. |
2a15a98a PH |
126 | basic_args = [ |
127 | 'rtmpdump', '--verbose', '-r', url, | |
9e105a85 | 128 | '-o', tmpfilename] |
3bc2ddcc JMF |
129 | if player_url is not None: |
130 | basic_args += ['--swfVfy', player_url] | |
131 | if page_url is not None: | |
132 | basic_args += ['--pageUrl', page_url] | |
082c6c86 S |
133 | if app is not None: |
134 | basic_args += ['--app', app] | |
3bc2ddcc JMF |
135 | if play_path is not None: |
136 | basic_args += ['--playpath', play_path] | |
137 | if tc_url is not None: | |
156fc83a | 138 | basic_args += ['--tcUrl', tc_url] |
3bc2ddcc JMF |
139 | if test: |
140 | basic_args += ['--stop', '1'] | |
082c6c86 S |
141 | if flash_version is not None: |
142 | basic_args += ['--flashVer', flash_version] | |
3bc2ddcc JMF |
143 | if live: |
144 | basic_args += ['--live'] | |
eb451334 S |
145 | if isinstance(conn, list): |
146 | for entry in conn: | |
147 | basic_args += ['--conn', entry] | |
148 | elif isinstance(conn, compat_str): | |
3bc2ddcc | 149 | basic_args += ['--conn', conn] |
087ca2cb JMF |
150 | if protocol is not None: |
151 | basic_args += ['--protocol', protocol] | |
0865f397 PH |
152 | if real_time: |
153 | basic_args += ['--realtime'] | |
3dee7826 PH |
154 | |
155 | args = basic_args | |
156 | if not no_resume and continue_dl and not live: | |
157 | args += ['--resume'] | |
158 | if not live and continue_dl: | |
159 | args += ['--skip', '1'] | |
3bc2ddcc | 160 | |
9e105a85 | 161 | args = [encodeArgument(a) for a in args] |
3bc2ddcc | 162 | |
9e105a85 | 163 | self._debug_cmd(args, exe='rtmpdump') |
3bc2ddcc | 164 | |
35241756 S |
165 | RD_SUCCESS = 0 |
166 | RD_FAILED = 1 | |
167 | RD_INCOMPLETE = 2 | |
52d6a9a6 | 168 | RD_NO_CONNECT = 3 |
35241756 | 169 | |
f16f4877 S |
170 | started = time.time() |
171 | ||
ddd8486a S |
172 | try: |
173 | retval = run_rtmpdump(args) | |
174 | except KeyboardInterrupt: | |
175 | if not info_dict.get('is_live'): | |
176 | raise | |
177 | retval = RD_SUCCESS | |
178 | self.to_screen('\n[rtmpdump] Interrupted by user') | |
3bc2ddcc | 179 | |
52d6a9a6 | 180 | if retval == RD_NO_CONNECT: |
8dec03ec | 181 | self.report_error('[rtmpdump] Could not connect to RTMP server.') |
52d6a9a6 S |
182 | return False |
183 | ||
40fcba5e | 184 | while retval in (RD_INCOMPLETE, RD_FAILED) and not test and not live: |
3bc2ddcc | 185 | prevsize = os.path.getsize(encodeFilename(tmpfilename)) |
f16f4877 | 186 | self.to_screen('[rtmpdump] Downloaded %s bytes' % prevsize) |
5f6a1245 | 187 | time.sleep(5.0) # This seems to be needed |
9e105a85 S |
188 | args = basic_args + ['--resume'] |
189 | if retval == RD_FAILED: | |
190 | args += ['--skip', '1'] | |
191 | args = [encodeArgument(a) for a in args] | |
192 | retval = run_rtmpdump(args) | |
3bc2ddcc | 193 | cursize = os.path.getsize(encodeFilename(tmpfilename)) |
35241756 | 194 | if prevsize == cursize and retval == RD_FAILED: |
3bc2ddcc | 195 | break |
7af808a5 | 196 | # Some rtmp streams seem abort after ~ 99.8%. Don't complain for those |
35241756 | 197 | if prevsize == cursize and retval == RD_INCOMPLETE and cursize > 1024: |
8dec03ec | 198 | self.to_screen('[rtmpdump] Could not download the whole video. This can happen for some advertisements.') |
35241756 | 199 | retval = RD_SUCCESS |
3bc2ddcc | 200 | break |
35241756 | 201 | if retval == RD_SUCCESS or (test and retval == RD_INCOMPLETE): |
3bc2ddcc | 202 | fsize = os.path.getsize(encodeFilename(tmpfilename)) |
f16f4877 | 203 | self.to_screen('[rtmpdump] Downloaded %s bytes' % fsize) |
3bc2ddcc JMF |
204 | self.try_rename(tmpfilename, filename) |
205 | self._hook_progress({ | |
206 | 'downloaded_bytes': fsize, | |
207 | 'total_bytes': fsize, | |
208 | 'filename': filename, | |
209 | 'status': 'finished', | |
f16f4877 | 210 | 'elapsed': time.time() - started, |
3bc2ddcc JMF |
211 | }) |
212 | return True | |
213 | else: | |
8dec03ec S |
214 | self.to_stderr('\n') |
215 | self.report_error('rtmpdump exited with code %d' % retval) | |
3bc2ddcc | 216 | return False |