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