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