]>
jfr.im git - yt-dlp.git/blob - youtube_dl/downloader/external.py
1 from __future__
import unicode_literals
8 from .common
import FileDownloader
9 from ..postprocessor
.ffmpeg
import FFmpegPostProcessor
, EXT_TO_OUT_FORMATS
10 from ..compat
import compat_str
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
)
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
._option
('--interface', 'source_address')
100 cmd
+= self
._option
('--proxy', 'proxy')
101 cmd
+= self
._valueless
_option
('--insecure', 'nocheckcertificate')
102 cmd
+= self
._configuration
_args
()
103 cmd
+= ['--', info_dict
['url']]
107 class AxelFD(ExternalFD
):
110 def _make_cmd(self
, tmpfilename
, info_dict
):
111 cmd
= [self
.exe
, '-o', tmpfilename
]
112 for key
, val
in info_dict
['http_headers'].items():
113 cmd
+= ['-H', '%s: %s' % (key
, val
)]
114 cmd
+= self
._configuration
_args
()
115 cmd
+= ['--', info_dict
['url']]
119 class WgetFD(ExternalFD
):
120 AVAILABLE_OPT
= '--version'
122 def _make_cmd(self
, tmpfilename
, info_dict
):
123 cmd
= [self
.exe
, '-O', tmpfilename
, '-nv', '--no-cookies']
124 for key
, val
in info_dict
['http_headers'].items():
125 cmd
+= ['--header', '%s: %s' % (key
, val
)]
126 cmd
+= self
._option
('--bind-address', 'source_address')
127 cmd
+= self
._option
('--proxy', 'proxy')
128 cmd
+= self
._valueless
_option
('--no-check-certificate', 'nocheckcertificate')
129 cmd
+= self
._configuration
_args
()
130 cmd
+= ['--', info_dict
['url']]
134 class Aria2cFD(ExternalFD
):
137 def _make_cmd(self
, tmpfilename
, info_dict
):
138 cmd
= [self
.exe
, '-c']
139 cmd
+= self
._configuration
_args
([
140 '--min-split-size', '1M', '--max-connection-per-server', '4'])
141 dn
= os
.path
.dirname(tmpfilename
)
144 cmd
+= ['--out', os
.path
.basename(tmpfilename
)]
145 for key
, val
in info_dict
['http_headers'].items():
146 cmd
+= ['--header', '%s: %s' % (key
, val
)]
147 cmd
+= self
._option
('--interface', 'source_address')
148 cmd
+= self
._option
('--all-proxy', 'proxy')
149 cmd
+= self
._bool
_option
('--check-certificate', 'nocheckcertificate', 'false', 'true', '=')
150 cmd
+= ['--', info_dict
['url']]
154 class HttpieFD(ExternalFD
):
157 return check_executable('http', ['--version'])
159 def _make_cmd(self
, tmpfilename
, info_dict
):
160 cmd
= ['http', '--download', '--output', tmpfilename
, info_dict
['url']]
161 for key
, val
in info_dict
['http_headers'].items():
162 cmd
+= ['%s:%s' % (key
, val
)]
166 class FFmpegFD(ExternalFD
):
168 def supports(cls
, info_dict
):
169 return info_dict
['protocol'] in ('http', 'https', 'ftp', 'ftps', 'm3u8', 'rtsp', 'rtmp', 'mms')
173 return FFmpegPostProcessor().available
175 def _call_downloader(self
, tmpfilename
, info_dict
):
176 url
= info_dict
['url']
177 ffpp
= FFmpegPostProcessor(downloader
=self
)
180 args
= [ffpp
.executable
, '-y']
182 start_time
= info_dict
.get('start_time') or 0
184 args
+= ['-ss', compat_str(start_time
)]
185 end_time
= info_dict
.get('end_time')
187 args
+= ['-t', compat_str(end_time
- start_time
)]
189 if info_dict
['http_headers'] and re
.match(r
'^https?://', url
):
190 # Trailing \r\n after each HTTP header is important to prevent warning from ffmpeg/avconv:
191 # [http @ 00000000003d2fa0] No trailing CRLF found in HTTP header.
192 headers
= handle_youtubedl_headers(info_dict
['http_headers'])
195 ''.join('%s: %s\r\n' % (key
, val
) for key
, val
in headers
.items())]
197 args
+= ['-i', url
, '-c', 'copy']
198 if info_dict
.get('protocol') == 'm3u8':
199 if self
.params
.get('hls_use_mpegts', False):
200 args
+= ['-f', 'mpegts']
202 args
+= ['-f', 'mp4', '-bsf:a', 'aac_adtstoasc']
204 args
+= ['-f', EXT_TO_OUT_FORMATS
.get(info_dict
['ext'], info_dict
['ext'])]
206 args
= [encodeArgument(opt
) for opt
in args
]
207 args
.append(encodeFilename(ffpp
._ffmpeg
_filename
_argument
(tmpfilename
), True))
209 self
._debug
_cmd
(args
)
211 proc
= subprocess
.Popen(args
, stdin
=subprocess
.PIPE
)
214 except KeyboardInterrupt:
215 # subprocces.run would send the SIGKILL signal to ffmpeg and the
216 # mp4 file couldn't be played, but if we ask ffmpeg to quit it
217 # produces a file that is playable (this is mostly useful for live
218 # streams). Note that Windows is not affected and produces playable
219 # files (see https://github.com/rg3/youtube-dl/issues/8300).
220 if sys
.platform
!= 'win32':
221 proc
.communicate(b
'q')
226 class AVconvFD(FFmpegFD
):
230 (klass
.get_basename(), klass
)
231 for name
, klass
in globals().items()
232 if name
.endswith('FD') and name
!= 'ExternalFD'
236 def list_external_downloaders():
237 return sorted(_BY_NAME
.keys())
240 def get_external_downloader(external_downloader
):
241 """ Given the name of the executable, see whether we support the given
243 # Drop .exe extension on Windows
244 bn
= os
.path
.splitext(os
.path
.basename(external_downloader
))[0]