1 from __future__ import unicode_literals
8 from .common import FileDownloader
9 from ..compat import compat_setenv
10 from ..postprocessor.ffmpeg import FFmpegPostProcessor, EXT_TO_OUT_FORMATS
15 cli_configuration_args,
18 handle_youtubedl_headers,
23 class ExternalFD(FileDownloader):
24 def real_download(self, filename, info_dict):
25 self.report_destination(filename)
26 tmpfilename = self.temp_name(filename)
28 retval = self._call_downloader(tmpfilename, info_dict)
30 fsize = os.path.getsize(encodeFilename(tmpfilename))
31 self.to_screen('\r[%s] Downloaded %s bytes' % (self.get_basename(), fsize))
32 self.try_rename(tmpfilename, filename)
34 'downloaded_bytes': fsize,
42 self.report_error('%s exited with code %d' % (
43 self.get_basename(), retval))
47 def get_basename(cls):
48 return cls.__name__[:-2].lower()
52 return self.params.get('external_downloader')
56 return check_executable(cls.get_basename(), [cls.AVAILABLE_OPT])
59 def supports(cls, info_dict):
60 return info_dict['protocol'] in ('http', 'https', 'ftp', 'ftps')
63 def can_download(cls, info_dict):
64 return cls.available() and cls.supports(info_dict)
66 def _option(self, command_option, param):
67 return cli_option(self.params, command_option, param)
69 def _bool_option(self, command_option, param, true_value='true', false_value='false', separator=None):
70 return cli_bool_option(self.params, command_option, param, true_value, false_value, separator)
72 def _valueless_option(self, command_option, param, expected_value=True):
73 return cli_valueless_option(self.params, command_option, param, expected_value)
75 def _configuration_args(self, default=[]):
76 return cli_configuration_args(self.params, 'external_downloader_args', default)
78 def _call_downloader(self, tmpfilename, info_dict):
79 """ Either overwrite this or implement _make_cmd """
80 cmd = [encodeArgument(a) for a in self._make_cmd(tmpfilename, info_dict)]
85 cmd, stderr=subprocess.PIPE)
86 _, stderr = p.communicate()
88 self.to_stderr(stderr.decode('utf-8', 'replace'))
92 class CurlFD(ExternalFD):
95 def _make_cmd(self, tmpfilename, info_dict):
96 cmd = [self.exe, '--location', '-o', tmpfilename]
97 for key, val in info_dict['http_headers'].items():
98 cmd += ['--header', '%s: %s' % (key, val)]
99 cmd += self._bool_option('--continue-at', 'continuedl', '-', '0')
100 cmd += self._valueless_option('--silent', 'noprogress')
101 cmd += self._valueless_option('--verbose', 'verbose')
102 cmd += self._option('--limit-rate', 'ratelimit')
103 cmd += self._option('--retry', 'retries')
104 cmd += self._option('--max-filesize', 'max_filesize')
105 cmd += self._option('--interface', 'source_address')
106 cmd += self._option('--proxy', 'proxy')
107 cmd += self._valueless_option('--insecure', 'nocheckcertificate')
108 cmd += self._configuration_args()
109 cmd += ['--', info_dict['url']]
112 def _call_downloader(self, tmpfilename, info_dict):
113 cmd = [encodeArgument(a) for a in self._make_cmd(tmpfilename, info_dict)]
117 # curl writes the progress to stderr so don't capture it.
118 p = subprocess.Popen(cmd)
123 class AxelFD(ExternalFD):
126 def _make_cmd(self, tmpfilename, info_dict):
127 cmd = [self.exe, '-o', tmpfilename]
128 for key, val in info_dict['http_headers'].items():
129 cmd += ['-H', '%s: %s' % (key, val)]
130 cmd += self._configuration_args()
131 cmd += ['--', info_dict['url']]
135 class WgetFD(ExternalFD):
136 AVAILABLE_OPT = '--version'
138 def _make_cmd(self, tmpfilename, info_dict):
139 cmd = [self.exe, '-O', tmpfilename, '-nv', '--no-cookies']
140 for key, val in info_dict['http_headers'].items():
141 cmd += ['--header', '%s: %s' % (key, val)]
142 cmd += self._option('--bind-address', 'source_address')
143 cmd += self._option('--proxy', 'proxy')
144 cmd += self._valueless_option('--no-check-certificate', 'nocheckcertificate')
145 cmd += self._configuration_args()
146 cmd += ['--', info_dict['url']]
150 class Aria2cFD(ExternalFD):
153 def _make_cmd(self, tmpfilename, info_dict):
154 cmd = [self.exe, '-c']
155 cmd += self._configuration_args([
156 '--min-split-size', '1M', '--max-connection-per-server', '4'])
157 dn = os.path.dirname(tmpfilename)
160 cmd += ['--out', os.path.basename(tmpfilename)]
161 for key, val in info_dict['http_headers'].items():
162 cmd += ['--header', '%s: %s' % (key, val)]
163 cmd += self._option('--interface', 'source_address')
164 cmd += self._option('--all-proxy', 'proxy')
165 cmd += self._bool_option('--check-certificate', 'nocheckcertificate', 'false', 'true', '=')
166 cmd += ['--', info_dict['url']]
170 class HttpieFD(ExternalFD):
173 return check_executable('http', ['--version'])
175 def _make_cmd(self, tmpfilename, info_dict):
176 cmd = ['http', '--download', '--output', tmpfilename, info_dict['url']]
177 for key, val in info_dict['http_headers'].items():
178 cmd += ['%s:%s' % (key, val)]
182 class FFmpegFD(ExternalFD):
184 def supports(cls, info_dict):
185 return info_dict['protocol'] in ('http', 'https', 'ftp', 'ftps', 'm3u8', 'rtsp', 'rtmp', 'mms')
189 return FFmpegPostProcessor().available
191 def _call_downloader(self, tmpfilename, info_dict):
192 url = info_dict['url']
193 ffpp = FFmpegPostProcessor(downloader=self)
194 if not ffpp.available:
195 self.report_error('m3u8 download detected but ffmpeg or avconv could not be found. Please install one.')
199 args = [ffpp.executable, '-y']
201 args += self._configuration_args()
203 # start_time = info_dict.get('start_time') or 0
205 # args += ['-ss', compat_str(start_time)]
206 # end_time = info_dict.get('end_time')
208 # args += ['-t', compat_str(end_time - start_time)]
210 if info_dict['http_headers'] and re.match(r'^https?://', url):
211 # Trailing \r\n after each HTTP header is important to prevent warning from ffmpeg/avconv:
212 # [http @ 00000000003d2fa0] No trailing CRLF found in HTTP header.
213 headers = handle_youtubedl_headers(info_dict['http_headers'])
216 ''.join('%s: %s\r\n' % (key, val) for key, val in headers.items())]
219 proxy = self.params.get('proxy')
221 if not re.match(r'^[\da-zA-Z]+://', proxy):
222 proxy = 'http://%s' % proxy
224 if proxy.startswith('socks'):
226 '%s does not support SOCKS proxies. Downloading may fail.' % self.get_basename())
228 # Since December 2015 ffmpeg supports -http_proxy option (see
229 # http://git.videolan.org/?p=ffmpeg.git;a=commit;h=b4eb1f29ebddd60c41a2eb39f5af701e38e0d3fd)
230 # We could switch to the following code if we are able to detect version properly
231 # args += ['-http_proxy', proxy]
232 env = os.environ.copy()
233 compat_setenv('HTTP_PROXY', proxy, env=env)
234 compat_setenv('http_proxy', proxy, env=env)
236 protocol = info_dict.get('protocol')
238 if protocol == 'rtmp':
239 player_url = info_dict.get('player_url')
240 page_url = info_dict.get('page_url')
241 app = info_dict.get('app')
242 play_path = info_dict.get('play_path')
243 tc_url = info_dict.get('tc_url')
244 flash_version = info_dict.get('flash_version')
245 live = info_dict.get('rtmp_live', False)
246 if player_url is not None:
247 args += ['-rtmp_swfverify', player_url]
248 if page_url is not None:
249 args += ['-rtmp_pageurl', page_url]
251 args += ['-rtmp_app', app]
252 if play_path is not None:
253 args += ['-rtmp_playpath', play_path]
254 if tc_url is not None:
255 args += ['-rtmp_tcurl', tc_url]
256 if flash_version is not None:
257 args += ['-rtmp_flashver', flash_version]
259 args += ['-rtmp_live', 'live']
261 args += ['-i', url, '-c', 'copy']
262 if protocol in ('m3u8', 'm3u8_native'):
263 if self.params.get('hls_use_mpegts', False) or tmpfilename == '-':
264 args += ['-f', 'mpegts']
266 args += ['-f', 'mp4', '-bsf:a', 'aac_adtstoasc']
267 elif protocol == 'rtmp':
268 args += ['-f', 'flv']
270 args += ['-f', EXT_TO_OUT_FORMATS.get(info_dict['ext'], info_dict['ext'])]
272 args = [encodeArgument(opt) for opt in args]
273 args.append(encodeFilename(ffpp._ffmpeg_filename_argument(tmpfilename), True))
275 self._debug_cmd(args)
277 proc = subprocess.Popen(args, stdin=subprocess.PIPE, env=env)
280 except KeyboardInterrupt:
281 # subprocces.run would send the SIGKILL signal to ffmpeg and the
282 # mp4 file couldn't be played, but if we ask ffmpeg to quit it
283 # produces a file that is playable (this is mostly useful for live
284 # streams). Note that Windows is not affected and produces playable
285 # files (see https://github.com/rg3/youtube-dl/issues/8300).
286 if sys.platform != 'win32':
287 proc.communicate(b'q')
292 class AVconvFD(FFmpegFD):
296 (klass.get_basename(), klass)
297 for name, klass in globals().items()
298 if name.endswith('FD') and name != 'ExternalFD'
302 def list_external_downloaders():
303 return sorted(_BY_NAME.keys())
306 def get_external_downloader(external_downloader):
307 """ Given the name of the executable, see whether we support the given
309 # Drop .exe extension on Windows
310 bn = os.path.splitext(os.path.basename(external_downloader))[0]