1 from __future__ import unicode_literals
9 from .common import FileDownloader
16 class RtmpFD(FileDownloader):
17 def real_download(self, filename, info_dict):
18 def run_rtmpdump(args):
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
29 char = proc.stderr.read(1)
31 proc_stderr_closed = True
33 if char in [b'\r', b'\n']:
35 line += char.decode('ascii', 'replace')
37 # proc_stderr_closed is True
39 mobj = re.search(r'([0-9]+\.[0-9]{3}) kB / [0-9]+\.[0-9]{2} sec \(([0-9]{1,2}\.[0-9])%\)', line)
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)
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
55 'downloaded_bytes': downloaded_data_len,
56 'total_bytes': data_len,
57 'tmpfilename': tmpfilename,
59 'status': 'downloading',
64 # no percent for live streams
65 mobj = re.search(r'([0-9]+\.[0-9]{3}) kB / [0-9]+\.[0-9]{2} sec', line)
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
73 'downloaded_bytes': downloaded_data_len,
74 'tmpfilename': tmpfilename,
76 'status': 'downloading',
79 elif self.params.get('verbose', False):
80 if not cursor_in_new_line:
82 cursor_in_new_line = True
83 self.to_screen('[rtmpdump] '+line)
85 if not cursor_in_new_line:
87 return proc.returncode
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)
99 self.report_destination(filename)
100 tmpfilename = self.temp_name(filename)
101 test = self.params.get('test', False)
103 # Check for rtmpdump first
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')
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]
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]
125 basic_args += ['--stop', '1']
126 if flash_version is not None:
127 basic_args += ['--flashVer', flash_version]
129 basic_args += ['--live']
131 basic_args += ['--conn', conn]
132 args = basic_args + [[], ['--resume', '--skip', '1']][not live and self.params.get('continuedl', False)]
134 if sys.platform == 'win32' and sys.version_info < (3, 0):
135 # Windows subprocess module does not actually support Unicode
137 # See http://stackoverflow.com/a/9951851/35070
138 subprocess_encoding = sys.getfilesystemencoding()
139 args = [a.encode(subprocess_encoding, 'ignore') for a in args]
141 subprocess_encoding = None
143 if self.params.get('verbose', False):
144 if subprocess_encoding:
146 a.decode(subprocess_encoding) if isinstance(a, bytes) else a
152 shell_quote = lambda args: ' '.join(map(pipes.quote, str_args))
155 self.to_screen('[debug] rtmpdump command line: ' + shell_quote(str_args))
162 retval = run_rtmpdump(args)
164 if retval == RD_NO_CONNECT:
165 self.report_error('[rtmpdump] Could not connect to RTMP server.')
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:
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.')
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',
194 self.report_error('rtmpdump exited with code %d' % retval)