]>
Commit | Line | Data |
---|---|---|
222516d9 PH |
1 | from __future__ import unicode_literals |
2 | ||
3 | import os.path | |
f0298f65 | 4 | import re |
222516d9 | 5 | import subprocess |
12b84ac8 | 6 | import sys |
f0298f65 | 7 | import time |
5219cb3e | 8 | |
9 | try: | |
10 | from Crypto.Cipher import AES | |
11 | can_decrypt_frag = True | |
12 | except ImportError: | |
13 | can_decrypt_frag = False | |
222516d9 PH |
14 | |
15 | from .common import FileDownloader | |
a50862b7 S |
16 | from ..compat import ( |
17 | compat_setenv, | |
18 | compat_str, | |
19 | ) | |
a755f825 | 20 | from ..postprocessor.ffmpeg import FFmpegPostProcessor, EXT_TO_OUT_FORMATS |
222516d9 | 21 | from ..utils import ( |
1195a38f S |
22 | cli_option, |
23 | cli_valueless_option, | |
24 | cli_bool_option, | |
25 | cli_configuration_args, | |
222516d9 | 26 | encodeFilename, |
74f8654a | 27 | encodeArgument, |
12b84ac8 | 28 | handle_youtubedl_headers, |
99cbe98c | 29 | check_executable, |
8bdc1494 | 30 | is_outdated_version, |
f5b1bca9 | 31 | process_communicate_or_kill, |
5219cb3e | 32 | sanitized_Request, |
539d158c | 33 | sanitize_open, |
222516d9 PH |
34 | ) |
35 | ||
36 | ||
37 | class ExternalFD(FileDownloader): | |
5219cb3e | 38 | SUPPORTED_PROTOCOLS = ('http', 'https', 'ftp', 'ftps') |
39 | ||
222516d9 PH |
40 | def real_download(self, filename, info_dict): |
41 | self.report_destination(filename) | |
42 | tmpfilename = self.temp_name(filename) | |
43 | ||
e7db6759 | 44 | try: |
f0298f65 | 45 | started = time.time() |
e7db6759 S |
46 | retval = self._call_downloader(tmpfilename, info_dict) |
47 | except KeyboardInterrupt: | |
48 | if not info_dict.get('is_live'): | |
49 | raise | |
50 | # Live stream downloading cancellation should be considered as | |
51 | # correct and expected termination thus all postprocessing | |
52 | # should take place | |
53 | retval = 0 | |
54 | self.to_screen('[%s] Interrupted by user' % self.get_basename()) | |
55 | ||
222516d9 | 56 | if retval == 0: |
f0298f65 S |
57 | status = { |
58 | 'filename': filename, | |
59 | 'status': 'finished', | |
60 | 'elapsed': time.time() - started, | |
61 | } | |
62 | if filename != '-': | |
80aa2460 JH |
63 | fsize = os.path.getsize(encodeFilename(tmpfilename)) |
64 | self.to_screen('\r[%s] Downloaded %s bytes' % (self.get_basename(), fsize)) | |
65 | self.try_rename(tmpfilename, filename) | |
f0298f65 | 66 | status.update({ |
80aa2460 JH |
67 | 'downloaded_bytes': fsize, |
68 | 'total_bytes': fsize, | |
80aa2460 | 69 | }) |
f0298f65 | 70 | self._hook_progress(status) |
222516d9 PH |
71 | return True |
72 | else: | |
73 | self.to_stderr('\n') | |
74 | self.report_error('%s exited with code %d' % ( | |
75 | self.get_basename(), retval)) | |
76 | return False | |
77 | ||
78 | @classmethod | |
79 | def get_basename(cls): | |
80 | return cls.__name__[:-2].lower() | |
81 | ||
82 | @property | |
83 | def exe(self): | |
52a8a1e1 | 84 | return self.get_basename() |
222516d9 | 85 | |
99cbe98c | 86 | @classmethod |
7f7de7f9 | 87 | def available(cls, path=None): |
52a8a1e1 | 88 | path = check_executable(path or cls.get_basename(), [cls.AVAILABLE_OPT]) |
89 | if path: | |
90 | cls.exe = path | |
91 | return path | |
92 | return False | |
99cbe98c | 93 | |
222516d9 PH |
94 | @classmethod |
95 | def supports(cls, info_dict): | |
5219cb3e | 96 | return info_dict['protocol'] in cls.SUPPORTED_PROTOCOLS |
222516d9 | 97 | |
2cb99ebb | 98 | @classmethod |
7f7de7f9 | 99 | def can_download(cls, info_dict, path=None): |
100 | return cls.available(path) and cls.supports(info_dict) | |
2cb99ebb | 101 | |
bf812ef7 | 102 | def _option(self, command_option, param): |
1195a38f | 103 | return cli_option(self.params, command_option, param) |
bf812ef7 | 104 | |
266b0ad6 | 105 | def _bool_option(self, command_option, param, true_value='true', false_value='false', separator=None): |
1195a38f | 106 | return cli_bool_option(self.params, command_option, param, true_value, false_value, separator) |
266b0ad6 | 107 | |
dc534b67 | 108 | def _valueless_option(self, command_option, param, expected_value=True): |
1195a38f | 109 | return cli_valueless_option(self.params, command_option, param, expected_value) |
f30c2e8e | 110 | |
5b1ecbb3 | 111 | def _configuration_args(self, *args, **kwargs): |
eab9b2bc | 112 | return cli_configuration_args( |
5b1ecbb3 | 113 | self.params.get('external_downloader_args'), |
e92caff5 | 114 | [self.get_basename(), 'default'], |
115 | *args, **kwargs) | |
c75f0b36 | 116 | |
222516d9 PH |
117 | def _call_downloader(self, tmpfilename, info_dict): |
118 | """ Either overwrite this or implement _make_cmd """ | |
74f8654a | 119 | cmd = [encodeArgument(a) for a in self._make_cmd(tmpfilename, info_dict)] |
222516d9 | 120 | |
74f8654a | 121 | self._debug_cmd(cmd) |
222516d9 | 122 | |
d7009caa | 123 | if 'fragments' in info_dict: |
fe845284 | 124 | fragment_retries = self.params.get('fragment_retries', 0) |
125 | skip_unavailable_fragments = self.params.get('skip_unavailable_fragments', True) | |
126 | ||
127 | count = 0 | |
128 | while count <= fragment_retries: | |
129 | p = subprocess.Popen( | |
130 | cmd, stderr=subprocess.PIPE) | |
131 | _, stderr = process_communicate_or_kill(p) | |
132 | if p.returncode == 0: | |
133 | break | |
134 | # TODO: Decide whether to retry based on error code | |
135 | # https://aria2.github.io/manual/en/html/aria2c.html#exit-status | |
136 | self.to_stderr(stderr.decode('utf-8', 'replace')) | |
137 | count += 1 | |
138 | if count <= fragment_retries: | |
139 | self.to_screen( | |
140 | '[%s] Got error. Retrying fragments (attempt %d of %s)...' | |
141 | % (self.get_basename(), count, self.format_retries(fragment_retries))) | |
142 | if count > fragment_retries: | |
143 | if not skip_unavailable_fragments: | |
144 | self.report_error('Giving up after %s fragment retries' % fragment_retries) | |
145 | return -1 | |
146 | ||
539d158c | 147 | dest, _ = sanitize_open(tmpfilename, 'wb') |
fe845284 | 148 | for frag_index, fragment in enumerate(info_dict['fragments']): |
149 | fragment_filename = '%s-Frag%d' % (tmpfilename, frag_index) | |
150 | try: | |
151 | src, _ = sanitize_open(fragment_filename, 'rb') | |
152 | except IOError: | |
153 | if skip_unavailable_fragments and frag_index > 1: | |
154 | self.to_screen('[%s] Skipping fragment %d ...' % (self.get_basename(), frag_index)) | |
155 | continue | |
156 | self.report_error('Unable to open fragment %d' % frag_index) | |
157 | return -1 | |
d7009caa | 158 | decrypt_info = fragment.get('decrypt_info') |
d7009caa | 159 | if decrypt_info: |
539d158c | 160 | if decrypt_info['METHOD'] == 'AES-128': |
161 | iv = decrypt_info.get('IV') | |
162 | decrypt_info['KEY'] = decrypt_info.get('KEY') or self.ydl.urlopen( | |
163 | self._prepare_url(info_dict, info_dict.get('_decryption_key_url') or decrypt_info['URI'])).read() | |
164 | encrypted_data = src.read() | |
165 | decrypted_data = AES.new( | |
166 | decrypt_info['KEY'], AES.MODE_CBC, iv).decrypt(encrypted_data) | |
167 | dest.write(decrypted_data) | |
5219cb3e | 168 | else: |
539d158c | 169 | fragment_data = src.read() |
170 | dest.write(fragment_data) | |
171 | else: | |
172 | fragment_data = src.read() | |
173 | dest.write(fragment_data) | |
174 | src.close() | |
fe845284 | 175 | if not self.params.get('keep_fragments', False): |
176 | os.remove(encodeFilename(fragment_filename)) | |
539d158c | 177 | dest.close() |
fe845284 | 178 | os.remove(encodeFilename('%s.frag.urls' % tmpfilename)) |
179 | else: | |
180 | p = subprocess.Popen( | |
181 | cmd, stderr=subprocess.PIPE) | |
182 | _, stderr = process_communicate_or_kill(p) | |
183 | if p.returncode != 0: | |
184 | self.to_stderr(stderr.decode('utf-8', 'replace')) | |
222516d9 PH |
185 | return p.returncode |
186 | ||
5219cb3e | 187 | def _prepare_url(self, info_dict, url): |
188 | headers = info_dict.get('http_headers') | |
189 | return sanitized_Request(url, None, headers) if headers else url | |
190 | ||
222516d9 | 191 | |
384b6202 | 192 | class CurlFD(ExternalFD): |
91ee320b | 193 | AVAILABLE_OPT = '-V' |
99cbe98c | 194 | |
384b6202 | 195 | def _make_cmd(self, tmpfilename, info_dict): |
163d9667 | 196 | cmd = [self.exe, '--location', '-o', tmpfilename] |
002ea8fe | 197 | if info_dict.get('http_headers') is not None: |
198 | for key, val in info_dict['http_headers'].items(): | |
199 | cmd += ['--header', '%s: %s' % (key, val)] | |
200 | ||
98e698f1 RA |
201 | cmd += self._bool_option('--continue-at', 'continuedl', '-', '0') |
202 | cmd += self._valueless_option('--silent', 'noprogress') | |
203 | cmd += self._valueless_option('--verbose', 'verbose') | |
204 | cmd += self._option('--limit-rate', 'ratelimit') | |
37b239b3 S |
205 | retry = self._option('--retry', 'retries') |
206 | if len(retry) == 2: | |
207 | if retry[1] in ('inf', 'infinite'): | |
208 | retry[1] = '2147483647' | |
209 | cmd += retry | |
98e698f1 | 210 | cmd += self._option('--max-filesize', 'max_filesize') |
9f3da138 | 211 | cmd += self._option('--interface', 'source_address') |
e7a8c303 | 212 | cmd += self._option('--proxy', 'proxy') |
dc534b67 | 213 | cmd += self._valueless_option('--insecure', 'nocheckcertificate') |
c75f0b36 | 214 | cmd += self._configuration_args() |
384b6202 PH |
215 | cmd += ['--', info_dict['url']] |
216 | return cmd | |
217 | ||
98e698f1 RA |
218 | def _call_downloader(self, tmpfilename, info_dict): |
219 | cmd = [encodeArgument(a) for a in self._make_cmd(tmpfilename, info_dict)] | |
220 | ||
221 | self._debug_cmd(cmd) | |
222 | ||
acfccaca | 223 | # curl writes the progress to stderr so don't capture it. |
98e698f1 | 224 | p = subprocess.Popen(cmd) |
f5b1bca9 | 225 | process_communicate_or_kill(p) |
98e698f1 RA |
226 | return p.returncode |
227 | ||
384b6202 | 228 | |
e0ac5214 | 229 | class AxelFD(ExternalFD): |
91ee320b | 230 | AVAILABLE_OPT = '-V' |
99cbe98c | 231 | |
e0ac5214 | 232 | def _make_cmd(self, tmpfilename, info_dict): |
233 | cmd = [self.exe, '-o', tmpfilename] | |
002ea8fe | 234 | if info_dict.get('http_headers') is not None: |
235 | for key, val in info_dict['http_headers'].items(): | |
236 | cmd += ['-H', '%s: %s' % (key, val)] | |
e0ac5214 | 237 | cmd += self._configuration_args() |
238 | cmd += ['--', info_dict['url']] | |
239 | return cmd | |
240 | ||
241 | ||
222516d9 | 242 | class WgetFD(ExternalFD): |
91ee320b | 243 | AVAILABLE_OPT = '--version' |
99cbe98c | 244 | |
222516d9 PH |
245 | def _make_cmd(self, tmpfilename, info_dict): |
246 | cmd = [self.exe, '-O', tmpfilename, '-nv', '--no-cookies'] | |
002ea8fe | 247 | if info_dict.get('http_headers') is not None: |
248 | for key, val in info_dict['http_headers'].items(): | |
249 | cmd += ['--header', '%s: %s' % (key, val)] | |
8c80603f S |
250 | cmd += self._option('--limit-rate', 'ratelimit') |
251 | retry = self._option('--tries', 'retries') | |
252 | if len(retry) == 2: | |
253 | if retry[1] in ('inf', 'infinite'): | |
254 | retry[1] = '0' | |
255 | cmd += retry | |
9f3da138 | 256 | cmd += self._option('--bind-address', 'source_address') |
bf812ef7 | 257 | cmd += self._option('--proxy', 'proxy') |
dc534b67 | 258 | cmd += self._valueless_option('--no-check-certificate', 'nocheckcertificate') |
c75f0b36 | 259 | cmd += self._configuration_args() |
222516d9 PH |
260 | cmd += ['--', info_dict['url']] |
261 | return cmd | |
262 | ||
263 | ||
384b6202 | 264 | class Aria2cFD(ExternalFD): |
91ee320b | 265 | AVAILABLE_OPT = '-v' |
52a8a1e1 | 266 | SUPPORTED_PROTOCOLS = ('http', 'https', 'ftp', 'ftps', 'dash_frag_urls', 'm3u8_frag_urls') |
99cbe98c | 267 | |
0a473f2f | 268 | @staticmethod |
269 | def supports_manifest(manifest): | |
270 | UNSUPPORTED_FEATURES = [ | |
271 | r'#EXT-X-BYTERANGE', # playlists composed of byte ranges of media files [1] | |
272 | # 1. https://tools.ietf.org/html/draft-pantos-http-live-streaming-17#section-4.3.2.2 | |
273 | ] | |
274 | check_results = (not re.search(feature, manifest) for feature in UNSUPPORTED_FEATURES) | |
275 | return all(check_results) | |
276 | ||
384b6202 | 277 | def _make_cmd(self, tmpfilename, info_dict): |
2b3bf01c | 278 | cmd = [self.exe, '-c', |
279 | '--console-log-level=warn', '--summary-interval=0', '--download-result=hide', | |
280 | '--file-allocation=none', '-x16', '-j16', '-s16'] | |
281 | if 'fragments' in info_dict: | |
282 | cmd += ['--allow-overwrite=true', '--allow-piece-length-change=true'] | |
283 | ||
002ea8fe | 284 | if info_dict.get('http_headers') is not None: |
285 | for key, val in info_dict['http_headers'].items(): | |
286 | cmd += ['--header', '%s: %s' % (key, val)] | |
9f3da138 | 287 | cmd += self._option('--interface', 'source_address') |
bf812ef7 | 288 | cmd += self._option('--all-proxy', 'proxy') |
266b0ad6 | 289 | cmd += self._bool_option('--check-certificate', 'nocheckcertificate', 'false', 'true', '=') |
71f47617 | 290 | cmd += self._bool_option('--remote-time', 'updatetime', 'true', 'false', '=') |
2b3bf01c | 291 | cmd += self._configuration_args() |
292 | ||
eb55bad5 | 293 | # aria2c strips out spaces from the beginning/end of filenames and paths. |
294 | # We work around this issue by adding a "./" to the beginning of the | |
295 | # filename and relative path, and adding a "/" at the end of the path. | |
296 | # See: https://github.com/yt-dlp/yt-dlp/issues/276 | |
297 | # https://github.com/ytdl-org/youtube-dl/issues/20312 | |
298 | # https://github.com/aria2/aria2/issues/1373 | |
2b3bf01c | 299 | dn = os.path.dirname(tmpfilename) |
300 | if dn: | |
eb55bad5 | 301 | if not os.path.isabs(dn): |
302 | dn = '.%s%s' % (os.path.sep, dn) | |
303 | cmd += ['--dir', dn + os.path.sep] | |
2b3bf01c | 304 | if 'fragments' not in info_dict: |
eb55bad5 | 305 | cmd += ['--out', '.%s%s' % (os.path.sep, os.path.basename(tmpfilename))] |
5219cb3e | 306 | cmd += ['--auto-file-renaming=false'] |
2b3bf01c | 307 | |
d7009caa | 308 | if 'fragments' in info_dict: |
fe845284 | 309 | cmd += ['--file-allocation=none', '--uri-selector=inorder'] |
5219cb3e | 310 | url_list_file = '%s.frag.urls' % tmpfilename |
311 | url_list = [] | |
fe845284 | 312 | for frag_index, fragment in enumerate(info_dict['fragments']): |
313 | fragment_filename = '%s-Frag%d' % (os.path.basename(tmpfilename), frag_index) | |
314 | url_list.append('%s\n\tout=%s' % (fragment['url'], fragment_filename)) | |
539d158c | 315 | stream, _ = sanitize_open(url_list_file, 'wb') |
316 | stream.write('\n'.join(url_list).encode('utf-8')) | |
317 | stream.close() | |
5219cb3e | 318 | cmd += ['-i', url_list_file] |
319 | else: | |
320 | cmd += ['--', info_dict['url']] | |
384b6202 PH |
321 | return cmd |
322 | ||
906e2f0e JMF |
323 | |
324 | class HttpieFD(ExternalFD): | |
52a8a1e1 | 325 | AVAILABLE_OPT = '--version' |
326 | ||
99cbe98c | 327 | @classmethod |
9e631877 | 328 | def available(cls, path=None): |
52a8a1e1 | 329 | return ExternalFD.available(cls, path or 'http') |
99cbe98c | 330 | |
906e2f0e JMF |
331 | def _make_cmd(self, tmpfilename, info_dict): |
332 | cmd = ['http', '--download', '--output', tmpfilename, info_dict['url']] | |
002ea8fe | 333 | |
334 | if info_dict.get('http_headers') is not None: | |
335 | for key, val in info_dict['http_headers'].items(): | |
336 | cmd += ['%s:%s' % (key, val)] | |
906e2f0e JMF |
337 | return cmd |
338 | ||
12b84ac8 | 339 | |
340 | class FFmpegFD(ExternalFD): | |
0fa9a1e2 | 341 | SUPPORTED_PROTOCOLS = ('http', 'https', 'ftp', 'ftps', 'm3u8', 'm3u8_native', 'rtsp', 'rtmp', 'rtmp_ffmpeg', 'mms') |
12b84ac8 | 342 | |
99cbe98c | 343 | @classmethod |
52a8a1e1 | 344 | def available(cls, path=None): |
345 | # TODO: Fix path for ffmpeg | |
99cbe98c | 346 | return FFmpegPostProcessor().available |
347 | ||
12b84ac8 | 348 | def _call_downloader(self, tmpfilename, info_dict): |
18e674b4 | 349 | urls = [f['url'] for f in info_dict.get('requested_formats', [])] or [info_dict['url']] |
12b84ac8 | 350 | ffpp = FFmpegPostProcessor(downloader=self) |
77dea16a | 351 | if not ffpp.available: |
e3b771a8 | 352 | self.report_error('m3u8 download detected but ffmpeg could not be found. Please install') |
77dea16a | 353 | return False |
12b84ac8 | 354 | ffpp.check_version() |
355 | ||
356 | args = [ffpp.executable, '-y'] | |
357 | ||
a609e61a S |
358 | for log_level in ('quiet', 'verbose'): |
359 | if self.params.get(log_level, False): | |
360 | args += ['-loglevel', log_level] | |
361 | break | |
362 | ||
36fce548 RA |
363 | seekable = info_dict.get('_seekable') |
364 | if seekable is not None: | |
365 | # setting -seekable prevents ffmpeg from guessing if the server | |
366 | # supports seeking(by adding the header `Range: bytes=0-`), which | |
367 | # can cause problems in some cases | |
067aa17e | 368 | # https://github.com/ytdl-org/youtube-dl/issues/11800#issuecomment-275037127 |
36fce548 RA |
369 | # http://trac.ffmpeg.org/ticket/6125#comment:10 |
370 | args += ['-seekable', '1' if seekable else '0'] | |
371 | ||
d8515fd4 | 372 | args += self._configuration_args() |
373 | ||
694c47b2 | 374 | # start_time = info_dict.get('start_time') or 0 |
375 | # if start_time: | |
376 | # args += ['-ss', compat_str(start_time)] | |
377 | # end_time = info_dict.get('end_time') | |
378 | # if end_time: | |
379 | # args += ['-t', compat_str(end_time - start_time)] | |
12b84ac8 | 380 | |
18e674b4 | 381 | if info_dict.get('http_headers') is not None and re.match(r'^https?://', urls[0]): |
12b84ac8 | 382 | # Trailing \r\n after each HTTP header is important to prevent warning from ffmpeg/avconv: |
383 | # [http @ 00000000003d2fa0] No trailing CRLF found in HTTP header. | |
384 | headers = handle_youtubedl_headers(info_dict['http_headers']) | |
385 | args += [ | |
386 | '-headers', | |
387 | ''.join('%s: %s\r\n' % (key, val) for key, val in headers.items())] | |
388 | ||
e62d9c5c S |
389 | env = None |
390 | proxy = self.params.get('proxy') | |
391 | if proxy: | |
392 | if not re.match(r'^[\da-zA-Z]+://', proxy): | |
393 | proxy = 'http://%s' % proxy | |
20bad91d YCH |
394 | |
395 | if proxy.startswith('socks'): | |
396 | self.report_warning( | |
6c9b71bc YCH |
397 | '%s does not support SOCKS proxies. Downloading is likely to fail. ' |
398 | 'Consider adding --hls-prefer-native to your command.' % self.get_basename()) | |
20bad91d | 399 | |
e62d9c5c S |
400 | # Since December 2015 ffmpeg supports -http_proxy option (see |
401 | # http://git.videolan.org/?p=ffmpeg.git;a=commit;h=b4eb1f29ebddd60c41a2eb39f5af701e38e0d3fd) | |
402 | # We could switch to the following code if we are able to detect version properly | |
403 | # args += ['-http_proxy', proxy] | |
404 | env = os.environ.copy() | |
405 | compat_setenv('HTTP_PROXY', proxy, env=env) | |
50ce1c33 | 406 | compat_setenv('http_proxy', proxy, env=env) |
e62d9c5c | 407 | |
4230c489 | 408 | protocol = info_dict.get('protocol') |
409 | ||
410 | if protocol == 'rtmp': | |
411 | player_url = info_dict.get('player_url') | |
412 | page_url = info_dict.get('page_url') | |
413 | app = info_dict.get('app') | |
414 | play_path = info_dict.get('play_path') | |
415 | tc_url = info_dict.get('tc_url') | |
416 | flash_version = info_dict.get('flash_version') | |
417 | live = info_dict.get('rtmp_live', False) | |
d7d86fdd | 418 | conn = info_dict.get('rtmp_conn') |
4230c489 | 419 | if player_url is not None: |
420 | args += ['-rtmp_swfverify', player_url] | |
421 | if page_url is not None: | |
422 | args += ['-rtmp_pageurl', page_url] | |
423 | if app is not None: | |
424 | args += ['-rtmp_app', app] | |
425 | if play_path is not None: | |
426 | args += ['-rtmp_playpath', play_path] | |
427 | if tc_url is not None: | |
428 | args += ['-rtmp_tcurl', tc_url] | |
429 | if flash_version is not None: | |
430 | args += ['-rtmp_flashver', flash_version] | |
431 | if live: | |
432 | args += ['-rtmp_live', 'live'] | |
d7d86fdd RA |
433 | if isinstance(conn, list): |
434 | for entry in conn: | |
435 | args += ['-rtmp_conn', entry] | |
436 | elif isinstance(conn, compat_str): | |
437 | args += ['-rtmp_conn', conn] | |
4230c489 | 438 | |
18e674b4 | 439 | for url in urls: |
440 | args += ['-i', url] | |
441 | args += ['-c', 'copy'] | |
442 | if info_dict.get('requested_formats'): | |
443 | for (i, fmt) in enumerate(info_dict['requested_formats']): | |
444 | if fmt.get('acodec') != 'none': | |
445 | args.extend(['-map', '%d:a:0' % i]) | |
446 | if fmt.get('vcodec') != 'none': | |
447 | args.extend(['-map', '%d:v:0' % i]) | |
6d0fe752 JH |
448 | |
449 | if self.params.get('test', False): | |
a50862b7 | 450 | args += ['-fs', compat_str(self._TEST_FILE_SIZE)] |
6d0fe752 | 451 | |
f5436c5d | 452 | if protocol in ('m3u8', 'm3u8_native'): |
9bd20204 | 453 | use_mpegts = (tmpfilename == '-') or self.params.get('hls_use_mpegts') |
454 | if use_mpegts is None: | |
455 | use_mpegts = info_dict.get('is_live') | |
456 | if use_mpegts: | |
12b84ac8 | 457 | args += ['-f', 'mpegts'] |
458 | else: | |
8bdc1494 | 459 | args += ['-f', 'mp4'] |
be670b8e | 460 | 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 | 461 | args += ['-bsf:a', 'aac_adtstoasc'] |
4230c489 | 462 | elif protocol == 'rtmp': |
463 | args += ['-f', 'flv'] | |
12b84ac8 | 464 | else: |
a755f825 | 465 | args += ['-f', EXT_TO_OUT_FORMATS.get(info_dict['ext'], info_dict['ext'])] |
12b84ac8 | 466 | |
467 | args = [encodeArgument(opt) for opt in args] | |
d868f43c | 468 | args.append(encodeFilename(ffpp._ffmpeg_filename_argument(tmpfilename), True)) |
12b84ac8 | 469 | |
470 | self._debug_cmd(args) | |
471 | ||
e62d9c5c | 472 | proc = subprocess.Popen(args, stdin=subprocess.PIPE, env=env) |
12b84ac8 | 473 | try: |
474 | retval = proc.wait() | |
f5b1bca9 | 475 | except BaseException as e: |
12b84ac8 | 476 | # subprocces.run would send the SIGKILL signal to ffmpeg and the |
477 | # mp4 file couldn't be played, but if we ask ffmpeg to quit it | |
478 | # produces a file that is playable (this is mostly useful for live | |
479 | # streams). Note that Windows is not affected and produces playable | |
067aa17e | 480 | # files (see https://github.com/ytdl-org/youtube-dl/issues/8300). |
f5b1bca9 | 481 | if isinstance(e, KeyboardInterrupt) and sys.platform != 'win32': |
482 | process_communicate_or_kill(proc, b'q') | |
483 | else: | |
484 | proc.kill() | |
485 | proc.wait() | |
12b84ac8 | 486 | raise |
487 | return retval | |
488 | ||
489 | ||
490 | class AVconvFD(FFmpegFD): | |
491 | pass | |
492 | ||
582be358 | 493 | |
222516d9 PH |
494 | _BY_NAME = dict( |
495 | (klass.get_basename(), klass) | |
496 | for name, klass in globals().items() | |
497 | if name.endswith('FD') and name != 'ExternalFD' | |
498 | ) | |
499 | ||
500 | ||
501 | def list_external_downloaders(): | |
502 | return sorted(_BY_NAME.keys()) | |
503 | ||
504 | ||
505 | def get_external_downloader(external_downloader): | |
506 | """ Given the name of the executable, see whether we support the given | |
507 | downloader . """ | |
6c4d20cd S |
508 | # Drop .exe extension on Windows |
509 | bn = os.path.splitext(os.path.basename(external_downloader))[0] | |
52a8a1e1 | 510 | return _BY_NAME.get(bn) |