1 from __future__
import unicode_literals
10 from Crypto
.Cipher
import AES
11 can_decrypt_frag
= True
13 can_decrypt_frag
= False
15 from .common
import FileDownloader
16 from ..compat
import (
20 from ..postprocessor
.ffmpeg
import FFmpegPostProcessor
, EXT_TO_OUT_FORMATS
25 cli_configuration_args
,
28 handle_youtubedl_headers
,
31 process_communicate_or_kill
,
37 class ExternalFD(FileDownloader
):
38 SUPPORTED_PROTOCOLS
= ('http', 'https', 'ftp', 'ftps')
40 def real_download(self
, filename
, info_dict
):
41 self
.report_destination(filename
)
42 tmpfilename
= self
.temp_name(filename
)
46 retval
= self
._call
_downloader
(tmpfilename
, info_dict
)
47 except KeyboardInterrupt:
48 if not info_dict
.get('is_live'):
50 # Live stream downloading cancellation should be considered as
51 # correct and expected termination thus all postprocessing
54 self
.to_screen('[%s] Interrupted by user' % self
.get_basename())
60 'elapsed': time
.time() - started
,
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
)
67 'downloaded_bytes': fsize
,
70 self
._hook
_progress
(status
)
74 self
.report_error('%s exited with code %d' % (
75 self
.get_basename(), retval
))
79 def get_basename(cls
):
80 return cls
.__name
__[:-2].lower()
84 return self
.get_basename()
87 def available(cls
, path
=None):
88 path
= check_executable(path
or cls
.get_basename(), [cls
.AVAILABLE_OPT
])
95 def supports(cls
, info_dict
):
96 return info_dict
['protocol'] in cls
.SUPPORTED_PROTOCOLS
99 def can_download(cls
, info_dict
, path
=None):
100 return cls
.available(path
) and cls
.supports(info_dict
)
102 def _option(self
, command_option
, param
):
103 return cli_option(self
.params
, command_option
, param
)
105 def _bool_option(self
, command_option
, param
, true_value
='true', false_value
='false', separator
=None):
106 return cli_bool_option(self
.params
, command_option
, param
, true_value
, false_value
, separator
)
108 def _valueless_option(self
, command_option
, param
, expected_value
=True):
109 return cli_valueless_option(self
.params
, command_option
, param
, expected_value
)
111 def _configuration_args(self
, *args
, **kwargs
):
112 return cli_configuration_args(
113 self
.params
.get('external_downloader_args'),
114 [self
.get_basename(), 'default'],
117 def _call_downloader(self
, tmpfilename
, info_dict
):
118 """ Either overwrite this or implement _make_cmd """
119 cmd
= [encodeArgument(a
) for a
in self
._make
_cmd
(tmpfilename
, info_dict
)]
123 if 'fragments' in info_dict
:
124 fragment_retries
= self
.params
.get('fragment_retries', 0)
125 skip_unavailable_fragments
= self
.params
.get('skip_unavailable_fragments', True)
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:
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'))
138 if count
<= fragment_retries
:
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
)
147 dest
, _
= sanitize_open(tmpfilename
, 'wb')
148 for frag_index
, fragment
in enumerate(info_dict
['fragments']):
149 fragment_filename
= '%s-Frag%d' % (tmpfilename
, frag_index
)
151 src
, _
= sanitize_open(fragment_filename
, 'rb')
153 if skip_unavailable_fragments
and frag_index
> 1:
154 self
.to_screen('[%s] Skipping fragment %d ...' % (self
.get_basename(), frag_index
))
156 self
.report_error('Unable to open fragment %d' % frag_index
)
158 decrypt_info
= fragment
.get('decrypt_info')
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
)
169 fragment_data
= src
.read()
170 dest
.write(fragment_data
)
172 fragment_data
= src
.read()
173 dest
.write(fragment_data
)
175 if not self
.params
.get('keep_fragments', False):
176 os
.remove(encodeFilename(fragment_filename
))
178 os
.remove(encodeFilename('%s.frag.urls' % tmpfilename
))
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'))
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
192 class CurlFD(ExternalFD
):
195 def _make_cmd(self
, tmpfilename
, info_dict
):
196 cmd
= [self
.exe
, '--location', '-o', tmpfilename
]
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
)]
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')
205 retry
= self
._option
('--retry', 'retries')
207 if retry
[1] in ('inf', 'infinite'):
208 retry
[1] = '2147483647'
210 cmd
+= self
._option
('--max-filesize', 'max_filesize')
211 cmd
+= self
._option
('--interface', 'source_address')
212 cmd
+= self
._option
('--proxy', 'proxy')
213 cmd
+= self
._valueless
_option
('--insecure', 'nocheckcertificate')
214 cmd
+= self
._configuration
_args
()
215 cmd
+= ['--', info_dict
['url']]
218 def _call_downloader(self
, tmpfilename
, info_dict
):
219 cmd
= [encodeArgument(a
) for a
in self
._make
_cmd
(tmpfilename
, info_dict
)]
223 # curl writes the progress to stderr so don't capture it.
224 p
= subprocess
.Popen(cmd
)
225 process_communicate_or_kill(p
)
229 class AxelFD(ExternalFD
):
232 def _make_cmd(self
, tmpfilename
, info_dict
):
233 cmd
= [self
.exe
, '-o', tmpfilename
]
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
)]
237 cmd
+= self
._configuration
_args
()
238 cmd
+= ['--', info_dict
['url']]
242 class WgetFD(ExternalFD
):
243 AVAILABLE_OPT
= '--version'
245 def _make_cmd(self
, tmpfilename
, info_dict
):
246 cmd
= [self
.exe
, '-O', tmpfilename
, '-nv', '--no-cookies']
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
)]
250 cmd
+= self
._option
('--limit-rate', 'ratelimit')
251 retry
= self
._option
('--tries', 'retries')
253 if retry
[1] in ('inf', 'infinite'):
256 cmd
+= self
._option
('--bind-address', 'source_address')
257 cmd
+= self
._option
('--proxy', 'proxy')
258 cmd
+= self
._valueless
_option
('--no-check-certificate', 'nocheckcertificate')
259 cmd
+= self
._configuration
_args
()
260 cmd
+= ['--', info_dict
['url']]
264 class Aria2cFD(ExternalFD
):
266 SUPPORTED_PROTOCOLS
= ('http', 'https', 'ftp', 'ftps', 'dash_frag_urls', 'm3u8_frag_urls')
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
274 check_results
= (not re
.search(feature
, manifest
) for feature
in UNSUPPORTED_FEATURES
)
275 return all(check_results
)
277 def _make_cmd(self
, tmpfilename
, info_dict
):
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']
284 cmd
+= ['--min-split-size', '1M']
286 if info_dict
.get('http_headers') is not None:
287 for key
, val
in info_dict
['http_headers'].items():
288 cmd
+= ['--header', '%s: %s' % (key
, val
)]
289 cmd
+= self
._option
('--interface', 'source_address')
290 cmd
+= self
._option
('--all-proxy', 'proxy')
291 cmd
+= self
._bool
_option
('--check-certificate', 'nocheckcertificate', 'false', 'true', '=')
292 cmd
+= self
._bool
_option
('--remote-time', 'updatetime', 'true', 'false', '=')
293 cmd
+= self
._configuration
_args
()
295 # aria2c strips out spaces from the beginning/end of filenames and paths.
296 # We work around this issue by adding a "./" to the beginning of the
297 # filename and relative path, and adding a "/" at the end of the path.
298 # See: https://github.com/yt-dlp/yt-dlp/issues/276
299 # https://github.com/ytdl-org/youtube-dl/issues/20312
300 # https://github.com/aria2/aria2/issues/1373
301 dn
= os
.path
.dirname(tmpfilename
)
303 if not os
.path
.isabs(dn
):
304 dn
= '.%s%s' % (os
.path
.sep
, dn
)
305 cmd
+= ['--dir', dn
+ os
.path
.sep
]
306 if 'fragments' not in info_dict
:
307 cmd
+= ['--out', '.%s%s' % (os
.path
.sep
, os
.path
.basename(tmpfilename
))]
308 cmd
+= ['--auto-file-renaming=false']
310 if 'fragments' in info_dict
:
311 cmd
+= ['--file-allocation=none', '--uri-selector=inorder']
312 url_list_file
= '%s.frag.urls' % tmpfilename
314 for frag_index
, fragment
in enumerate(info_dict
['fragments']):
315 fragment_filename
= '%s-Frag%d' % (os
.path
.basename(tmpfilename
), frag_index
)
316 url_list
.append('%s\n\tout=%s' % (fragment
['url'], fragment_filename
))
317 stream
, _
= sanitize_open(url_list_file
, 'wb')
318 stream
.write('\n'.join(url_list
).encode('utf-8'))
320 cmd
+= ['-i', url_list_file
]
322 cmd
+= ['--', info_dict
['url']]
326 class HttpieFD(ExternalFD
):
327 AVAILABLE_OPT
= '--version'
330 def available(cls
, path
=None):
331 return ExternalFD
.available(cls
, path
or 'http')
333 def _make_cmd(self
, tmpfilename
, info_dict
):
334 cmd
= ['http', '--download', '--output', tmpfilename
, info_dict
['url']]
336 if info_dict
.get('http_headers') is not None:
337 for key
, val
in info_dict
['http_headers'].items():
338 cmd
+= ['%s:%s' % (key
, val
)]
342 class FFmpegFD(ExternalFD
):
343 SUPPORTED_PROTOCOLS
= ('http', 'https', 'ftp', 'ftps', 'm3u8', 'm3u8_native', 'rtsp', 'rtmp', 'rtmp_ffmpeg', 'mms')
346 def available(cls
, path
=None):
347 # TODO: Fix path for ffmpeg
348 return FFmpegPostProcessor().available
350 def on_process_started(self
, proc
, stdin
):
351 """ Override this in subclasses """
354 def _call_downloader(self
, tmpfilename
, info_dict
):
355 urls
= [f
['url'] for f
in info_dict
.get('requested_formats', [])] or [info_dict
['url']]
356 ffpp
= FFmpegPostProcessor(downloader
=self
)
357 if not ffpp
.available
:
358 self
.report_error('m3u8 download detected but ffmpeg could not be found. Please install')
362 args
= [ffpp
.executable
, '-y']
364 for log_level
in ('quiet', 'verbose'):
365 if self
.params
.get(log_level
, False):
366 args
+= ['-loglevel', log_level
]
368 if not self
.params
.get('verbose'):
369 args
+= ['-hide_banner']
371 seekable
= info_dict
.get('_seekable')
372 if seekable
is not None:
373 # setting -seekable prevents ffmpeg from guessing if the server
374 # supports seeking(by adding the header `Range: bytes=0-`), which
375 # can cause problems in some cases
376 # https://github.com/ytdl-org/youtube-dl/issues/11800#issuecomment-275037127
377 # http://trac.ffmpeg.org/ticket/6125#comment:10
378 args
+= ['-seekable', '1' if seekable
else '0']
380 args
+= self
._configuration
_args
()
382 # start_time = info_dict.get('start_time') or 0
384 # args += ['-ss', compat_str(start_time)]
385 # end_time = info_dict.get('end_time')
387 # args += ['-t', compat_str(end_time - start_time)]
389 if info_dict
.get('http_headers') is not None and re
.match(r
'^https?://', urls
[0]):
390 # Trailing \r\n after each HTTP header is important to prevent warning from ffmpeg/avconv:
391 # [http @ 00000000003d2fa0] No trailing CRLF found in HTTP header.
392 headers
= handle_youtubedl_headers(info_dict
['http_headers'])
395 ''.join('%s: %s\r\n' % (key
, val
) for key
, val
in headers
.items())]
398 proxy
= self
.params
.get('proxy')
400 if not re
.match(r
'^[\da-zA-Z]+://', proxy
):
401 proxy
= 'http://%s' % proxy
403 if proxy
.startswith('socks'):
405 '%s does not support SOCKS proxies. Downloading is likely to fail. '
406 'Consider adding --hls-prefer-native to your command.' % self
.get_basename())
408 # Since December 2015 ffmpeg supports -http_proxy option (see
409 # http://git.videolan.org/?p=ffmpeg.git;a=commit;h=b4eb1f29ebddd60c41a2eb39f5af701e38e0d3fd)
410 # We could switch to the following code if we are able to detect version properly
411 # args += ['-http_proxy', proxy]
412 env
= os
.environ
.copy()
413 compat_setenv('HTTP_PROXY', proxy
, env
=env
)
414 compat_setenv('http_proxy', proxy
, env
=env
)
416 protocol
= info_dict
.get('protocol')
418 if protocol
== 'rtmp':
419 player_url
= info_dict
.get('player_url')
420 page_url
= info_dict
.get('page_url')
421 app
= info_dict
.get('app')
422 play_path
= info_dict
.get('play_path')
423 tc_url
= info_dict
.get('tc_url')
424 flash_version
= info_dict
.get('flash_version')
425 live
= info_dict
.get('rtmp_live', False)
426 conn
= info_dict
.get('rtmp_conn')
427 if player_url
is not None:
428 args
+= ['-rtmp_swfverify', player_url
]
429 if page_url
is not None:
430 args
+= ['-rtmp_pageurl', page_url
]
432 args
+= ['-rtmp_app', app
]
433 if play_path
is not None:
434 args
+= ['-rtmp_playpath', play_path
]
435 if tc_url
is not None:
436 args
+= ['-rtmp_tcurl', tc_url
]
437 if flash_version
is not None:
438 args
+= ['-rtmp_flashver', flash_version
]
440 args
+= ['-rtmp_live', 'live']
441 if isinstance(conn
, list):
443 args
+= ['-rtmp_conn', entry
]
444 elif isinstance(conn
, compat_str
):
445 args
+= ['-rtmp_conn', conn
]
449 args
+= ['-c', 'copy']
450 if info_dict
.get('requested_formats'):
451 for (i
, fmt
) in enumerate(info_dict
['requested_formats']):
452 if fmt
.get('acodec') != 'none':
453 args
.extend(['-map', '%d:a:0' % i
])
454 if fmt
.get('vcodec') != 'none':
455 args
.extend(['-map', '%d:v:0' % i
])
457 if self
.params
.get('test', False):
458 args
+= ['-fs', compat_str(self
._TEST
_FILE
_SIZE
)]
460 if protocol
in ('m3u8', 'm3u8_native'):
461 use_mpegts
= (tmpfilename
== '-') or self
.params
.get('hls_use_mpegts')
462 if use_mpegts
is None:
463 use_mpegts
= info_dict
.get('is_live')
465 args
+= ['-f', 'mpegts']
467 args
+= ['-f', 'mp4']
468 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')):
469 args
+= ['-bsf:a', 'aac_adtstoasc']
470 elif protocol
== 'rtmp':
471 args
+= ['-f', 'flv']
473 args
+= ['-f', EXT_TO_OUT_FORMATS
.get(info_dict
['ext'], info_dict
['ext'])]
475 args
= [encodeArgument(opt
) for opt
in args
]
476 args
.append(encodeFilename(ffpp
._ffmpeg
_filename
_argument
(tmpfilename
), True))
478 self
._debug
_cmd
(args
)
480 proc
= subprocess
.Popen(args
, stdin
=subprocess
.PIPE
, env
=env
)
481 if url
in ('-', 'pipe:'):
482 self
.on_process_started(proc
, proc
.stdin
)
485 except BaseException
as e
:
486 # subprocces.run would send the SIGKILL signal to ffmpeg and the
487 # mp4 file couldn't be played, but if we ask ffmpeg to quit it
488 # produces a file that is playable (this is mostly useful for live
489 # streams). Note that Windows is not affected and produces playable
490 # files (see https://github.com/ytdl-org/youtube-dl/issues/8300).
491 if isinstance(e
, KeyboardInterrupt) and sys
.platform
!= 'win32' and url
not in ('-', 'pipe:'):
492 process_communicate_or_kill(proc
, b
'q')
500 class AVconvFD(FFmpegFD
):
505 (klass
.get_basename(), klass
)
506 for name
, klass
in globals().items()
507 if name
.endswith('FD') and name
!= 'ExternalFD'
511 def list_external_downloaders():
512 return sorted(_BY_NAME
.keys())
515 def get_external_downloader(external_downloader
):
516 """ Given the name of the executable, see whether we support the given
518 # Drop .exe extension on Windows
519 bn
= os
.path
.splitext(os
.path
.basename(external_downloader
))[0]
520 return _BY_NAME
.get(bn
)