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