]> jfr.im git - yt-dlp.git/blame - youtube_dlc/downloader/external.py
[skip travis] renaming
[yt-dlp.git] / youtube_dlc / downloader / external.py
CommitLineData
222516d9
PH
1from __future__ import unicode_literals
2
3import os.path
f0298f65 4import re
222516d9 5import subprocess
12b84ac8 6import sys
f0298f65 7import time
222516d9
PH
8
9from .common import FileDownloader
a50862b7
S
10from ..compat import (
11 compat_setenv,
12 compat_str,
13)
a755f825 14from ..postprocessor.ffmpeg import FFmpegPostProcessor, EXT_TO_OUT_FORMATS
222516d9 15from ..utils import (
1195a38f
S
16 cli_option,
17 cli_valueless_option,
18 cli_bool_option,
19 cli_configuration_args,
222516d9 20 encodeFilename,
74f8654a 21 encodeArgument,
12b84ac8 22 handle_youtubedl_headers,
99cbe98c 23 check_executable,
8bdc1494 24 is_outdated_version,
222516d9
PH
25)
26
27
28class ExternalFD(FileDownloader):
29 def real_download(self, filename, info_dict):
30 self.report_destination(filename)
31 tmpfilename = self.temp_name(filename)
32
e7db6759 33 try:
f0298f65 34 started = time.time()
e7db6759
S
35 retval = self._call_downloader(tmpfilename, info_dict)
36 except KeyboardInterrupt:
37 if not info_dict.get('is_live'):
38 raise
39 # Live stream downloading cancellation should be considered as
40 # correct and expected termination thus all postprocessing
41 # should take place
42 retval = 0
43 self.to_screen('[%s] Interrupted by user' % self.get_basename())
44
222516d9 45 if retval == 0:
f0298f65
S
46 status = {
47 'filename': filename,
48 'status': 'finished',
49 'elapsed': time.time() - started,
50 }
51 if filename != '-':
80aa2460
JH
52 fsize = os.path.getsize(encodeFilename(tmpfilename))
53 self.to_screen('\r[%s] Downloaded %s bytes' % (self.get_basename(), fsize))
54 self.try_rename(tmpfilename, filename)
f0298f65 55 status.update({
80aa2460
JH
56 'downloaded_bytes': fsize,
57 'total_bytes': fsize,
80aa2460 58 })
f0298f65 59 self._hook_progress(status)
222516d9
PH
60 return True
61 else:
62 self.to_stderr('\n')
63 self.report_error('%s exited with code %d' % (
64 self.get_basename(), retval))
65 return False
66
67 @classmethod
68 def get_basename(cls):
69 return cls.__name__[:-2].lower()
70
71 @property
72 def exe(self):
73 return self.params.get('external_downloader')
74
99cbe98c 75 @classmethod
76 def available(cls):
91ee320b 77 return check_executable(cls.get_basename(), [cls.AVAILABLE_OPT])
99cbe98c 78
222516d9
PH
79 @classmethod
80 def supports(cls, info_dict):
81 return info_dict['protocol'] in ('http', 'https', 'ftp', 'ftps')
82
2cb99ebb 83 @classmethod
84 def can_download(cls, info_dict):
85 return cls.available() and cls.supports(info_dict)
86
bf812ef7 87 def _option(self, command_option, param):
1195a38f 88 return cli_option(self.params, command_option, param)
bf812ef7 89
266b0ad6 90 def _bool_option(self, command_option, param, true_value='true', false_value='false', separator=None):
1195a38f 91 return cli_bool_option(self.params, command_option, param, true_value, false_value, separator)
266b0ad6 92
dc534b67 93 def _valueless_option(self, command_option, param, expected_value=True):
1195a38f 94 return cli_valueless_option(self.params, command_option, param, expected_value)
f30c2e8e 95
c75f0b36 96 def _configuration_args(self, default=[]):
1195a38f 97 return cli_configuration_args(self.params, 'external_downloader_args', default)
c75f0b36 98
222516d9
PH
99 def _call_downloader(self, tmpfilename, info_dict):
100 """ Either overwrite this or implement _make_cmd """
74f8654a 101 cmd = [encodeArgument(a) for a in self._make_cmd(tmpfilename, info_dict)]
222516d9 102
74f8654a 103 self._debug_cmd(cmd)
222516d9
PH
104
105 p = subprocess.Popen(
384b6202
PH
106 cmd, stderr=subprocess.PIPE)
107 _, stderr = p.communicate()
222516d9 108 if p.returncode != 0:
e69f9f5d 109 self.to_stderr(stderr.decode('utf-8', 'replace'))
222516d9
PH
110 return p.returncode
111
112
384b6202 113class CurlFD(ExternalFD):
91ee320b 114 AVAILABLE_OPT = '-V'
99cbe98c 115
384b6202 116 def _make_cmd(self, tmpfilename, info_dict):
163d9667 117 cmd = [self.exe, '--location', '-o', tmpfilename]
e5660ee6 118 for key, val in info_dict['http_headers'].items():
384b6202 119 cmd += ['--header', '%s: %s' % (key, val)]
98e698f1
RA
120 cmd += self._bool_option('--continue-at', 'continuedl', '-', '0')
121 cmd += self._valueless_option('--silent', 'noprogress')
122 cmd += self._valueless_option('--verbose', 'verbose')
123 cmd += self._option('--limit-rate', 'ratelimit')
37b239b3
S
124 retry = self._option('--retry', 'retries')
125 if len(retry) == 2:
126 if retry[1] in ('inf', 'infinite'):
127 retry[1] = '2147483647'
128 cmd += retry
98e698f1 129 cmd += self._option('--max-filesize', 'max_filesize')
9f3da138 130 cmd += self._option('--interface', 'source_address')
e7a8c303 131 cmd += self._option('--proxy', 'proxy')
dc534b67 132 cmd += self._valueless_option('--insecure', 'nocheckcertificate')
c75f0b36 133 cmd += self._configuration_args()
384b6202
PH
134 cmd += ['--', info_dict['url']]
135 return cmd
136
98e698f1
RA
137 def _call_downloader(self, tmpfilename, info_dict):
138 cmd = [encodeArgument(a) for a in self._make_cmd(tmpfilename, info_dict)]
139
140 self._debug_cmd(cmd)
141
acfccaca 142 # curl writes the progress to stderr so don't capture it.
98e698f1
RA
143 p = subprocess.Popen(cmd)
144 p.communicate()
145 return p.returncode
146
384b6202 147
e0ac5214 148class AxelFD(ExternalFD):
91ee320b 149 AVAILABLE_OPT = '-V'
99cbe98c 150
e0ac5214 151 def _make_cmd(self, tmpfilename, info_dict):
152 cmd = [self.exe, '-o', tmpfilename]
153 for key, val in info_dict['http_headers'].items():
154 cmd += ['-H', '%s: %s' % (key, val)]
155 cmd += self._configuration_args()
156 cmd += ['--', info_dict['url']]
157 return cmd
158
159
222516d9 160class WgetFD(ExternalFD):
91ee320b 161 AVAILABLE_OPT = '--version'
99cbe98c 162
222516d9
PH
163 def _make_cmd(self, tmpfilename, info_dict):
164 cmd = [self.exe, '-O', tmpfilename, '-nv', '--no-cookies']
e5660ee6 165 for key, val in info_dict['http_headers'].items():
222516d9 166 cmd += ['--header', '%s: %s' % (key, val)]
8c80603f
S
167 cmd += self._option('--limit-rate', 'ratelimit')
168 retry = self._option('--tries', 'retries')
169 if len(retry) == 2:
170 if retry[1] in ('inf', 'infinite'):
171 retry[1] = '0'
172 cmd += retry
9f3da138 173 cmd += self._option('--bind-address', 'source_address')
bf812ef7 174 cmd += self._option('--proxy', 'proxy')
dc534b67 175 cmd += self._valueless_option('--no-check-certificate', 'nocheckcertificate')
c75f0b36 176 cmd += self._configuration_args()
222516d9
PH
177 cmd += ['--', info_dict['url']]
178 return cmd
179
180
384b6202 181class Aria2cFD(ExternalFD):
91ee320b 182 AVAILABLE_OPT = '-v'
99cbe98c 183
384b6202 184 def _make_cmd(self, tmpfilename, info_dict):
c75f0b36
PH
185 cmd = [self.exe, '-c']
186 cmd += self._configuration_args([
187 '--min-split-size', '1M', '--max-connection-per-server', '4'])
384b6202
PH
188 dn = os.path.dirname(tmpfilename)
189 if dn:
190 cmd += ['--dir', dn]
191 cmd += ['--out', os.path.basename(tmpfilename)]
e5660ee6 192 for key, val in info_dict['http_headers'].items():
384b6202 193 cmd += ['--header', '%s: %s' % (key, val)]
9f3da138 194 cmd += self._option('--interface', 'source_address')
bf812ef7 195 cmd += self._option('--all-proxy', 'proxy')
266b0ad6 196 cmd += self._bool_option('--check-certificate', 'nocheckcertificate', 'false', 'true', '=')
71f47617 197 cmd += self._bool_option('--remote-time', 'updatetime', 'true', 'false', '=')
384b6202
PH
198 cmd += ['--', info_dict['url']]
199 return cmd
200
906e2f0e
JMF
201
202class HttpieFD(ExternalFD):
99cbe98c 203 @classmethod
204 def available(cls):
205 return check_executable('http', ['--version'])
206
906e2f0e
JMF
207 def _make_cmd(self, tmpfilename, info_dict):
208 cmd = ['http', '--download', '--output', tmpfilename, info_dict['url']]
209 for key, val in info_dict['http_headers'].items():
210 cmd += ['%s:%s' % (key, val)]
211 return cmd
212
12b84ac8 213
214class FFmpegFD(ExternalFD):
215 @classmethod
216 def supports(cls, info_dict):
6ae27bed 217 return info_dict['protocol'] in ('http', 'https', 'ftp', 'ftps', 'm3u8', 'rtsp', 'rtmp', 'mms')
12b84ac8 218
99cbe98c 219 @classmethod
220 def available(cls):
221 return FFmpegPostProcessor().available
222
12b84ac8 223 def _call_downloader(self, tmpfilename, info_dict):
224 url = info_dict['url']
225 ffpp = FFmpegPostProcessor(downloader=self)
77dea16a 226 if not ffpp.available:
227 self.report_error('m3u8 download detected but ffmpeg or avconv could not be found. Please install one.')
228 return False
12b84ac8 229 ffpp.check_version()
230
231 args = [ffpp.executable, '-y']
232
a609e61a
S
233 for log_level in ('quiet', 'verbose'):
234 if self.params.get(log_level, False):
235 args += ['-loglevel', log_level]
236 break
237
36fce548
RA
238 seekable = info_dict.get('_seekable')
239 if seekable is not None:
240 # setting -seekable prevents ffmpeg from guessing if the server
241 # supports seeking(by adding the header `Range: bytes=0-`), which
242 # can cause problems in some cases
067aa17e 243 # https://github.com/ytdl-org/youtube-dl/issues/11800#issuecomment-275037127
36fce548
RA
244 # http://trac.ffmpeg.org/ticket/6125#comment:10
245 args += ['-seekable', '1' if seekable else '0']
246
d8515fd4 247 args += self._configuration_args()
248
694c47b2 249 # start_time = info_dict.get('start_time') or 0
250 # if start_time:
251 # args += ['-ss', compat_str(start_time)]
252 # end_time = info_dict.get('end_time')
253 # if end_time:
254 # args += ['-t', compat_str(end_time - start_time)]
12b84ac8 255
256 if info_dict['http_headers'] and re.match(r'^https?://', url):
257 # Trailing \r\n after each HTTP header is important to prevent warning from ffmpeg/avconv:
258 # [http @ 00000000003d2fa0] No trailing CRLF found in HTTP header.
259 headers = handle_youtubedl_headers(info_dict['http_headers'])
260 args += [
261 '-headers',
262 ''.join('%s: %s\r\n' % (key, val) for key, val in headers.items())]
263
e62d9c5c
S
264 env = None
265 proxy = self.params.get('proxy')
266 if proxy:
267 if not re.match(r'^[\da-zA-Z]+://', proxy):
268 proxy = 'http://%s' % proxy
20bad91d
YCH
269
270 if proxy.startswith('socks'):
271 self.report_warning(
6c9b71bc
YCH
272 '%s does not support SOCKS proxies. Downloading is likely to fail. '
273 'Consider adding --hls-prefer-native to your command.' % self.get_basename())
20bad91d 274
e62d9c5c
S
275 # Since December 2015 ffmpeg supports -http_proxy option (see
276 # http://git.videolan.org/?p=ffmpeg.git;a=commit;h=b4eb1f29ebddd60c41a2eb39f5af701e38e0d3fd)
277 # We could switch to the following code if we are able to detect version properly
278 # args += ['-http_proxy', proxy]
279 env = os.environ.copy()
280 compat_setenv('HTTP_PROXY', proxy, env=env)
50ce1c33 281 compat_setenv('http_proxy', proxy, env=env)
e62d9c5c 282
4230c489 283 protocol = info_dict.get('protocol')
284
285 if protocol == 'rtmp':
286 player_url = info_dict.get('player_url')
287 page_url = info_dict.get('page_url')
288 app = info_dict.get('app')
289 play_path = info_dict.get('play_path')
290 tc_url = info_dict.get('tc_url')
291 flash_version = info_dict.get('flash_version')
292 live = info_dict.get('rtmp_live', False)
d7d86fdd 293 conn = info_dict.get('rtmp_conn')
4230c489 294 if player_url is not None:
295 args += ['-rtmp_swfverify', player_url]
296 if page_url is not None:
297 args += ['-rtmp_pageurl', page_url]
298 if app is not None:
299 args += ['-rtmp_app', app]
300 if play_path is not None:
301 args += ['-rtmp_playpath', play_path]
302 if tc_url is not None:
303 args += ['-rtmp_tcurl', tc_url]
304 if flash_version is not None:
305 args += ['-rtmp_flashver', flash_version]
306 if live:
307 args += ['-rtmp_live', 'live']
d7d86fdd
RA
308 if isinstance(conn, list):
309 for entry in conn:
310 args += ['-rtmp_conn', entry]
311 elif isinstance(conn, compat_str):
312 args += ['-rtmp_conn', conn]
4230c489 313
12b84ac8 314 args += ['-i', url, '-c', 'copy']
6d0fe752
JH
315
316 if self.params.get('test', False):
a50862b7 317 args += ['-fs', compat_str(self._TEST_FILE_SIZE)]
6d0fe752 318
f5436c5d 319 if protocol in ('m3u8', 'm3u8_native'):
ce599d5a 320 if self.params.get('hls_use_mpegts', False) or tmpfilename == '-':
12b84ac8 321 args += ['-f', 'mpegts']
322 else:
8bdc1494 323 args += ['-f', 'mp4']
be670b8e 324 if (ffpp.basename == 'ffmpeg' and is_outdated_version(ffpp._versions['ffmpeg'], '3.2', False)) and (not info_dict.get('acodec') or info_dict['acodec'].split('.')[0] in ('aac', 'mp4a')):
8bdc1494 325 args += ['-bsf:a', 'aac_adtstoasc']
4230c489 326 elif protocol == 'rtmp':
327 args += ['-f', 'flv']
12b84ac8 328 else:
a755f825 329 args += ['-f', EXT_TO_OUT_FORMATS.get(info_dict['ext'], info_dict['ext'])]
12b84ac8 330
331 args = [encodeArgument(opt) for opt in args]
d868f43c 332 args.append(encodeFilename(ffpp._ffmpeg_filename_argument(tmpfilename), True))
12b84ac8 333
334 self._debug_cmd(args)
335
e62d9c5c 336 proc = subprocess.Popen(args, stdin=subprocess.PIPE, env=env)
12b84ac8 337 try:
338 retval = proc.wait()
339 except KeyboardInterrupt:
340 # subprocces.run would send the SIGKILL signal to ffmpeg and the
341 # mp4 file couldn't be played, but if we ask ffmpeg to quit it
342 # produces a file that is playable (this is mostly useful for live
343 # streams). Note that Windows is not affected and produces playable
067aa17e 344 # files (see https://github.com/ytdl-org/youtube-dl/issues/8300).
12b84ac8 345 if sys.platform != 'win32':
346 proc.communicate(b'q')
347 raise
348 return retval
349
350
351class AVconvFD(FFmpegFD):
352 pass
353
582be358 354
222516d9
PH
355_BY_NAME = dict(
356 (klass.get_basename(), klass)
357 for name, klass in globals().items()
358 if name.endswith('FD') and name != 'ExternalFD'
359)
360
361
362def list_external_downloaders():
363 return sorted(_BY_NAME.keys())
364
365
366def get_external_downloader(external_downloader):
367 """ Given the name of the executable, see whether we support the given
368 downloader . """
6c4d20cd
S
369 # Drop .exe extension on Windows
370 bn = os.path.splitext(os.path.basename(external_downloader))[0]
222516d9 371 return _BY_NAME[bn]