]>
jfr.im git - yt-dlp.git/blob - youtube_dl/downloader/external.py
697f81e3f66118b163a1d08d1ad533ba2a199829
1 from __future__
import unicode_literals
8 from .common
import FileDownloader
9 from ..postprocessor
.ffmpeg
import FFmpegPostProcessor
, EXT_TO_OUT_FORMATS
14 cli_configuration_args
,
17 handle_youtubedl_headers
,
22 class ExternalFD(FileDownloader
):
23 def real_download(self
, filename
, info_dict
):
24 self
.report_destination(filename
)
25 tmpfilename
= self
.temp_name(filename
)
27 retval
= self
._call
_downloader
(tmpfilename
, info_dict
)
29 fsize
= os
.path
.getsize(encodeFilename(tmpfilename
))
30 self
.to_screen('\r[%s] Downloaded %s bytes' % (self
.get_basename(), fsize
))
31 self
.try_rename(tmpfilename
, filename
)
33 'downloaded_bytes': fsize
,
41 self
.report_error('%s exited with code %d' % (
42 self
.get_basename(), retval
))
46 def get_basename(cls
):
47 return cls
.__name
__[:-2].lower()
51 return self
.params
.get('external_downloader')
55 return check_executable(cls
.get_basename(), [cls
.AVAILABLE_OPT
])
58 def supports(cls
, info_dict
):
59 return info_dict
['protocol'] in ('http', 'https', 'ftp', 'ftps')
62 def can_download(cls
, info_dict
):
63 return cls
.available() and cls
.supports(info_dict
)
65 def _option(self
, command_option
, param
):
66 return cli_option(self
.params
, command_option
, param
)
68 def _bool_option(self
, command_option
, param
, true_value
='true', false_value
='false', separator
=None):
69 return cli_bool_option(self
.params
, command_option
, param
, true_value
, false_value
, separator
)
71 def _valueless_option(self
, command_option
, param
, expected_value
=True):
72 return cli_valueless_option(self
.params
, command_option
, param
, expected_value
)
74 def _configuration_args(self
, default
=[]):
75 return cli_configuration_args(self
.params
, 'external_downloader_args', default
)
77 def _call_downloader(self
, tmpfilename
, info_dict
):
78 """ Either overwrite this or implement _make_cmd """
79 cmd
= [encodeArgument(a
) for a
in self
._make
_cmd
(tmpfilename
, info_dict
)]
84 cmd
, stderr
=subprocess
.PIPE
)
85 _
, stderr
= p
.communicate()
87 self
.to_stderr(stderr
)
91 class CurlFD(ExternalFD
):
94 def _make_cmd(self
, tmpfilename
, info_dict
):
95 cmd
= [self
.exe
, '--location', '-o', tmpfilename
]
96 for key
, val
in info_dict
['http_headers'].items():
97 cmd
+= ['--header', '%s: %s' % (key
, val
)]
98 cmd
+= self
._option
('--interface', 'source_address')
99 cmd
+= self
._option
('--proxy', 'proxy')
100 cmd
+= self
._valueless
_option
('--insecure', 'nocheckcertificate')
101 cmd
+= self
._configuration
_args
()
102 cmd
+= ['--', info_dict
['url']]
106 class AxelFD(ExternalFD
):
109 def _make_cmd(self
, tmpfilename
, info_dict
):
110 cmd
= [self
.exe
, '-o', tmpfilename
]
111 for key
, val
in info_dict
['http_headers'].items():
112 cmd
+= ['-H', '%s: %s' % (key
, val
)]
113 cmd
+= self
._configuration
_args
()
114 cmd
+= ['--', info_dict
['url']]
118 class WgetFD(ExternalFD
):
119 AVAILABLE_OPT
= '--version'
121 def _make_cmd(self
, tmpfilename
, info_dict
):
122 cmd
= [self
.exe
, '-O', tmpfilename
, '-nv', '--no-cookies']
123 for key
, val
in info_dict
['http_headers'].items():
124 cmd
+= ['--header', '%s: %s' % (key
, val
)]
125 cmd
+= self
._option
('--bind-address', 'source_address')
126 cmd
+= self
._option
('--proxy', 'proxy')
127 cmd
+= self
._valueless
_option
('--no-check-certificate', 'nocheckcertificate')
128 cmd
+= self
._configuration
_args
()
129 cmd
+= ['--', info_dict
['url']]
133 class Aria2cFD(ExternalFD
):
136 def _make_cmd(self
, tmpfilename
, info_dict
):
137 cmd
= [self
.exe
, '-c']
138 cmd
+= self
._configuration
_args
([
139 '--min-split-size', '1M', '--max-connection-per-server', '4'])
140 dn
= os
.path
.dirname(tmpfilename
)
143 cmd
+= ['--out', os
.path
.basename(tmpfilename
)]
144 for key
, val
in info_dict
['http_headers'].items():
145 cmd
+= ['--header', '%s: %s' % (key
, val
)]
146 cmd
+= self
._option
('--interface', 'source_address')
147 cmd
+= self
._option
('--all-proxy', 'proxy')
148 cmd
+= self
._bool
_option
('--check-certificate', 'nocheckcertificate', 'false', 'true', '=')
149 cmd
+= ['--', info_dict
['url']]
153 class HttpieFD(ExternalFD
):
156 return check_executable('http', ['--version'])
158 def _make_cmd(self
, tmpfilename
, info_dict
):
159 cmd
= ['http', '--download', '--output', tmpfilename
, info_dict
['url']]
160 for key
, val
in info_dict
['http_headers'].items():
161 cmd
+= ['%s:%s' % (key
, val
)]
165 class FFmpegFD(ExternalFD
):
167 def supports(cls
, info_dict
):
168 return info_dict
['protocol'] in ('http', 'https', 'ftp', 'ftps', 'm3u8', 'rtsp', 'rtmp', 'mms')
172 return FFmpegPostProcessor().available
174 def _call_downloader(self
, tmpfilename
, info_dict
):
175 url
= info_dict
['url']
176 ffpp
= FFmpegPostProcessor(downloader
=self
)
177 if not ffpp
.available
:
178 self
.report_error('m3u8 download detected but ffmpeg or avconv could not be found. Please install one.')
182 args
= [ffpp
.executable
, '-y']
184 args
+= self
._configuration
_args
()
186 # start_time = info_dict.get('start_time') or 0
188 # args += ['-ss', compat_str(start_time)]
189 # end_time = info_dict.get('end_time')
191 # args += ['-t', compat_str(end_time - start_time)]
193 if info_dict
['http_headers'] and re
.match(r
'^https?://', url
):
194 # Trailing \r\n after each HTTP header is important to prevent warning from ffmpeg/avconv:
195 # [http @ 00000000003d2fa0] No trailing CRLF found in HTTP header.
196 headers
= handle_youtubedl_headers(info_dict
['http_headers'])
199 ''.join('%s: %s\r\n' % (key
, val
) for key
, val
in headers
.items())]
201 args
+= ['-i', url
, '-c', 'copy']
202 if info_dict
.get('protocol') == 'm3u8':
203 if self
.params
.get('hls_use_mpegts', False):
204 args
+= ['-f', 'mpegts']
206 args
+= ['-f', 'mp4', '-bsf:a', 'aac_adtstoasc']
208 args
+= ['-f', EXT_TO_OUT_FORMATS
.get(info_dict
['ext'], info_dict
['ext'])]
210 args
= [encodeArgument(opt
) for opt
in args
]
211 args
.append(encodeFilename(ffpp
._ffmpeg
_filename
_argument
(tmpfilename
), True))
213 self
._debug
_cmd
(args
)
215 proc
= subprocess
.Popen(args
, stdin
=subprocess
.PIPE
)
218 except KeyboardInterrupt:
219 # subprocces.run would send the SIGKILL signal to ffmpeg and the
220 # mp4 file couldn't be played, but if we ask ffmpeg to quit it
221 # produces a file that is playable (this is mostly useful for live
222 # streams). Note that Windows is not affected and produces playable
223 # files (see https://github.com/rg3/youtube-dl/issues/8300).
224 if sys
.platform
!= 'win32':
225 proc
.communicate(b
'q')
230 class AVconvFD(FFmpegFD
):
234 (klass
.get_basename(), klass
)
235 for name
, klass
in globals().items()
236 if name
.endswith('FD') and name
!= 'ExternalFD'
240 def list_external_downloaders():
241 return sorted(_BY_NAME
.keys())
244 def get_external_downloader(external_downloader
):
245 """ Given the name of the executable, see whether we support the given
247 # Drop .exe extension on Windows
248 bn
= os
.path
.splitext(os
.path
.basename(external_downloader
))[0]