]> jfr.im git - yt-dlp.git/blame - youtube_dlc/options.py
[viki] new way of obtaining subtitles.
[yt-dlp.git] / youtube_dlc / options.py
CommitLineData
2daabe49
PH
1from __future__ import unicode_literals
2
34a741a8
PH
3import os.path
4import optparse
3aa9a735 5import re
34a741a8
PH
6import sys
7
222516d9 8from .downloader.external import list_external_downloaders
8c25f81b 9from .compat import (
4644ac55 10 compat_expanduser,
003c69a8 11 compat_get_terminal_size,
4644ac55 12 compat_getenv,
c7b0add8 13 compat_kwargs,
cab792ab 14 compat_shlex_split,
8c25f81b
PH
15)
16from .utils import (
ff556f5c 17 preferredencoding,
34a741a8
PH
18 write_string,
19)
20from .version import __version__
21
22
905d18a7
YCH
23def _hide_login_info(opts):
24 PRIVATE_OPTS = set(['-p', '--password', '-u', '--username', '--video-password', '--ap-password', '--ap-username'])
25 eqre = re.compile('^(?P<key>' + ('|'.join(re.escape(po) for po in PRIVATE_OPTS)) + ')=.+$')
26
27 def _scrub_eq(o):
28 m = eqre.match(o)
29 if m:
30 return m.group('key') + '=PRIVATE'
31 else:
32 return o
33
34 opts = list(map(_scrub_eq, opts))
35 for idx, opt in enumerate(opts):
36 if opt in PRIVATE_OPTS and idx + 1 < len(opts):
37 opts[idx + 1] = 'PRIVATE'
38 return opts
39
40
34a741a8
PH
41def parseOpts(overrideArguments=None):
42 def _readOptions(filename_bytes, default=[]):
43 try:
44 optionf = open(filename_bytes)
45 except IOError:
46 return default # silently skip if file is not present
47 try:
067aa17e 48 # FIXME: https://github.com/ytdl-org/youtube-dl/commit/dfe5fa49aed02cf36ba9f743b11b0903554b5e56
1251565e
S
49 contents = optionf.read()
50 if sys.version_info < (3,):
51 contents = contents.decode(preferredencoding())
52 res = compat_shlex_split(contents, comments=True)
34a741a8
PH
53 finally:
54 optionf.close()
55 return res
56
57 def _readUserConf():
4644ac55 58 xdg_config_home = compat_getenv('XDG_CONFIG_HOME')
34a741a8 59 if xdg_config_home:
cefecac1 60 userConfFile = os.path.join(xdg_config_home, 'youtube-dlc', 'config')
34a741a8 61 if not os.path.isfile(userConfFile):
cefecac1 62 userConfFile = os.path.join(xdg_config_home, 'youtube-dlc.conf')
34a741a8 63 else:
cefecac1 64 userConfFile = os.path.join(compat_expanduser('~'), '.config', 'youtube-dlc', 'config')
34a741a8 65 if not os.path.isfile(userConfFile):
cefecac1 66 userConfFile = os.path.join(compat_expanduser('~'), '.config', 'youtube-dlc.conf')
34a741a8
PH
67 userConf = _readOptions(userConfFile, None)
68
69 if userConf is None:
4644ac55 70 appdata_dir = compat_getenv('appdata')
34a741a8
PH
71 if appdata_dir:
72 userConf = _readOptions(
cefecac1 73 os.path.join(appdata_dir, 'youtube-dlc', 'config'),
34a741a8
PH
74 default=None)
75 if userConf is None:
76 userConf = _readOptions(
cefecac1 77 os.path.join(appdata_dir, 'youtube-dlc', 'config.txt'),
34a741a8
PH
78 default=None)
79
80 if userConf is None:
81 userConf = _readOptions(
cefecac1 82 os.path.join(compat_expanduser('~'), 'youtube-dlc.conf'),
34a741a8
PH
83 default=None)
84 if userConf is None:
85 userConf = _readOptions(
cefecac1 86 os.path.join(compat_expanduser('~'), 'youtube-dlc.conf.txt'),
34a741a8
PH
87 default=None)
88
89 if userConf is None:
90 userConf = []
91
92 return userConf
93
94 def _format_option_string(option):
95 ''' ('-o', '--option') -> -o, --format METAVAR'''
96
97 opts = []
98
99 if option._short_opts:
100 opts.append(option._short_opts[0])
101 if option._long_opts:
102 opts.append(option._long_opts[0])
103 if len(opts) > 1:
104 opts.insert(1, ', ')
105
8450c15c
PH
106 if option.takes_value():
107 opts.append(' %s' % option.metavar)
34a741a8 108
611c1dd9 109 return ''.join(opts)
34a741a8
PH
110
111 def _comma_separated_values_options_callback(option, opt_str, value, parser):
112 setattr(parser.values, option.dest, value.split(','))
113
34a741a8 114 # No need to wrap help messages if we're on a wide console
003c69a8 115 columns = compat_get_terminal_size().columns
8450c15c
PH
116 max_width = columns if columns else 80
117 max_help_position = 80
34a741a8
PH
118
119 fmt = optparse.IndentedHelpFormatter(width=max_width, max_help_position=max_help_position)
120 fmt.format_option_strings = _format_option_string
121
122 kw = {
8450c15c
PH
123 'version': __version__,
124 'formatter': fmt,
a42419da 125 'usage': '%prog [OPTIONS] URL [URL...]',
8450c15c 126 'conflict_handler': 'resolve',
34a741a8
PH
127 }
128
c7b0add8 129 parser = optparse.OptionParser(**compat_kwargs(kw))
34a741a8 130
8450c15c
PH
131 general = optparse.OptionGroup(parser, 'General Options')
132 general.add_option(
133 '-h', '--help',
134 action='help',
17941321 135 help='Print this help text and exit')
8450c15c 136 general.add_option(
b76f0e58 137 '--version',
8450c15c 138 action='version',
17941321 139 help='Print program version and exit')
8450c15c
PH
140 general.add_option(
141 '-U', '--update',
142 action='store_true', dest='update_self',
17941321 143 help='Update this program to latest version. Make sure that you have sufficient permissions (run with sudo if needed)')
8450c15c
PH
144 general.add_option(
145 '-i', '--ignore-errors',
146 action='store_true', dest='ignoreerrors', default=False,
17941321 147 help='Continue on download errors, for example to skip unavailable videos in a playlist')
8450c15c
PH
148 general.add_option(
149 '--abort-on-error',
150 action='store_false', dest='ignoreerrors',
151 help='Abort downloading of further videos (in the playlist or the command line) if an error occurs')
152 general.add_option(
153 '--dump-user-agent',
154 action='store_true', dest='dump_user_agent', default=False,
17941321 155 help='Display the current browser identification')
34a741a8 156 general.add_option(
8450c15c
PH
157 '--list-extractors',
158 action='store_true', dest='list_extractors', default=False,
9836cfb8 159 help='List all supported extractors')
8450c15c
PH
160 general.add_option(
161 '--extractor-descriptions',
162 action='store_true', dest='list_extractor_descriptions', default=False,
163 help='Output descriptions of all supported extractors')
d22dec74
S
164 general.add_option(
165 '--force-generic-extractor',
166 action='store_true', dest='force_generic_extractor', default=False,
167 help='Force extraction to use the generic extractor')
34a741a8
PH
168 general.add_option(
169 '--default-search',
170 dest='default_search', metavar='PREFIX',
cefecac1 171 help='Use this prefix for unqualified URLs. For example "gvsearch2:" downloads two videos from google videos for youtube-dlc "large apple". Use the value "auto" to let youtube-dlc guess ("auto_warning" to emit a warning when guessing). "error" just throws an error. The default value "fixup_error" repairs broken URLs, but emits an error if this is not possible instead of searching.')
34a741a8
PH
172 general.add_option(
173 '--ignore-config',
174 action='store_true',
d4e06d4a 175 help='Do not read configuration files. '
cefecac1
U
176 'When given in the global configuration file /etc/youtube-dlc.conf: '
177 'Do not read the user configuration in ~/.config/youtube-dlc/config '
178 '(%APPDATA%/youtube-dlc/config.txt on Windows)')
e66dca5e 179 general.add_option(
b6ee45e9
S
180 '--config-location',
181 dest='config_location', metavar='PATH',
182 help='Location of the configuration file; either the path to the config or its containing directory.')
057a5206
PH
183 general.add_option(
184 '--flat-playlist',
185 action='store_const', dest='extract_flat', const='in_playlist',
186 default=False,
187 help='Do not extract the videos of a playlist, only list them.')
d77ab8e2
S
188 general.add_option(
189 '--mark-watched',
190 action='store_true', dest='mark_watched', default=False,
191 help='Mark videos watched (YouTube only)')
90f794c6
S
192 general.add_option(
193 '--no-mark-watched',
194 action='store_false', dest='mark_watched', default=False,
195 help='Do not mark videos watched (YouTube only)')
7e5db8c9
PH
196 general.add_option(
197 '--no-color', '--no-colors',
198 action='store_true', dest='no_color',
199 default=False,
17941321 200 help='Do not emit color codes in output')
34a741a8 201
be4a824d
PH
202 network = optparse.OptionGroup(parser, 'Network Options')
203 network.add_option(
204 '--proxy', dest='proxy',
205 default=None, metavar='URL',
504f20dd 206 help='Use the specified HTTP/HTTPS/SOCKS proxy. To enable '
6ddb4888
YCH
207 'SOCKS proxy, specify a proper scheme. For example '
208 'socks5://127.0.0.1:1080/. Pass in an empty string (--proxy "") '
209 'for direct connection')
be4a824d
PH
210 network.add_option(
211 '--socket-timeout',
212 dest='socket_timeout', type=float, default=None, metavar='SECONDS',
213 help='Time to wait before giving up, in seconds')
214 network.add_option(
215 '--source-address',
216 metavar='IP', dest='source_address', default=None,
24ee6b97 217 help='Client-side IP address to bind to',
be4a824d 218 )
500b8b41
PH
219 network.add_option(
220 '-4', '--force-ipv4',
221 action='store_const', const='0.0.0.0', dest='source_address',
24ee6b97 222 help='Make all connections via IPv4',
500b8b41
PH
223 )
224 network.add_option(
225 '-6', '--force-ipv6',
226 action='store_const', const='::', dest='source_address',
24ee6b97 227 help='Make all connections via IPv6',
500b8b41 228 )
0aa10994
S
229
230 geo = optparse.OptionGroup(parser, 'Geo Restriction')
231 geo.add_option(
38cce791
YCH
232 '--geo-verification-proxy',
233 dest='geo_verification_proxy', default=None, metavar='URL',
234 help='Use this proxy to verify the IP address for some geo-restricted sites. '
1e4fe5a7 235 'The default proxy specified by --proxy (or none, if the option is not present) is used for the actual downloading.')
0aa10994 236 geo.add_option(
91410c9b
PH
237 '--cn-verification-proxy',
238 dest='cn_verification_proxy', default=None, metavar='URL',
0aa10994
S
239 help=optparse.SUPPRESS_HELP)
240 geo.add_option(
241 '--geo-bypass',
242 action='store_true', dest='geo_bypass', default=True,
504f20dd 243 help='Bypass geographic restriction via faking X-Forwarded-For HTTP header')
0aa10994
S
244 geo.add_option(
245 '--no-geo-bypass',
246 action='store_false', dest='geo_bypass', default=True,
504f20dd 247 help='Do not bypass geographic restriction via faking X-Forwarded-For HTTP header')
0aa10994
S
248 geo.add_option(
249 '--geo-bypass-country', metavar='CODE',
250 dest='geo_bypass_country', default=None,
504f20dd 251 help='Force bypass geographic restriction with explicitly provided two-letter ISO 3166-2 country code')
5f95927a
S
252 geo.add_option(
253 '--geo-bypass-ip-block', metavar='IP_BLOCK',
254 dest='geo_bypass_ip_block', default=None,
504f20dd 255 help='Force bypass geographic restriction with explicitly provided IP block in CIDR notation')
be4a824d 256
8450c15c 257 selection = optparse.OptionGroup(parser, 'Video Selection')
34a741a8
PH
258 selection.add_option(
259 '--playlist-start',
260 dest='playliststart', metavar='NUMBER', default=1, type=int,
17941321 261 help='Playlist video to start at (default is %default)')
34a741a8
PH
262 selection.add_option(
263 '--playlist-end',
264 dest='playlistend', metavar='NUMBER', default=None, type=int,
17941321 265 help='Playlist video to end at (default is last)')
c14e88f0
PH
266 selection.add_option(
267 '--playlist-items',
268 dest='playlist_items', metavar='ITEM_SPEC', default=None,
4eb59a6b 269 help='Playlist video items to download. Specify indices of the videos in the playlist separated by commas like: "--playlist-items 1,2,5,8" if you want to download videos indexed 1, 2, 5, 8 in the playlist. You can specify range: "--playlist-items 1-3,7,10-13", it will download the videos at index 1, 2, 3, 7, 10, 11, 12 and 13.')
34a741a8 270 selection.add_option(
8450c15c
PH
271 '--match-title',
272 dest='matchtitle', metavar='REGEX',
17941321 273 help='Download only matching titles (regex or caseless sub-string)')
8450c15c
PH
274 selection.add_option(
275 '--reject-title',
276 dest='rejecttitle', metavar='REGEX',
17941321 277 help='Skip download for matching titles (regex or caseless sub-string)')
8450c15c
PH
278 selection.add_option(
279 '--max-downloads',
280 dest='max_downloads', metavar='NUMBER', type=int, default=None,
281 help='Abort after downloading NUMBER files')
282 selection.add_option(
283 '--min-filesize',
284 metavar='SIZE', dest='min_filesize', default=None,
285 help='Do not download any videos smaller than SIZE (e.g. 50k or 44.6m)')
286 selection.add_option(
287 '--max-filesize',
288 metavar='SIZE', dest='max_filesize', default=None,
289 help='Do not download any videos larger than SIZE (e.g. 50k or 44.6m)')
290 selection.add_option(
291 '--date',
292 metavar='DATE', dest='date', default=None,
17941321 293 help='Download only videos uploaded in this date')
8450c15c
PH
294 selection.add_option(
295 '--datebefore',
296 metavar='DATE', dest='datebefore', default=None,
17941321 297 help='Download only videos uploaded on or before this date (i.e. inclusive)')
34a741a8 298 selection.add_option(
8450c15c
PH
299 '--dateafter',
300 metavar='DATE', dest='dateafter', default=None,
17941321 301 help='Download only videos uploaded on or after this date (i.e. inclusive)')
34a741a8 302 selection.add_option(
8450c15c
PH
303 '--min-views',
304 metavar='COUNT', dest='min_views', default=None, type=int,
17941321 305 help='Do not download any videos with less than COUNT views')
34a741a8 306 selection.add_option(
8450c15c
PH
307 '--max-views',
308 metavar='COUNT', dest='max_views', default=None, type=int,
309 help='Do not download any videos with more than COUNT views')
347de493
PH
310 selection.add_option(
311 '--match-filter',
312 metavar='FILTER', dest='match_filter', default=None,
313 help=(
24ee6b97 314 'Generic video filter. '
a355b57f 315 'Specify any key (see the "OUTPUT TEMPLATE" for a list of available keys) to '
2c1f442c
S
316 'match if the key is present, '
317 '!key to check if the key is not present, '
347de493 318 'key > NUMBER (like "comment_count > 12", also works with '
ac33accd
S
319 '>=, <, <=, !=, =) to compare against a number, '
320 'key = \'LITERAL\' (like "uploader = \'Mike Smith\'", also works with !=) '
321 'to match against a string literal '
322 'and & to require multiple matches. '
2c1f442c
S
323 'Values which are not known are excluded unless you '
324 'put a question mark (?) after the operator. '
347de493
PH
325 'For example, to only match videos that have been liked more than '
326 '100 times and disliked less than 50 times (or the dislike '
327 'functionality is not available at the given service), but who '
5495937f 328 'also have a description, use --match-filter '
347de493
PH
329 '"like_count > 100 & dislike_count <? 50 & description" .'
330 ))
34a741a8 331 selection.add_option(
8450c15c
PH
332 '--no-playlist',
333 action='store_true', dest='noplaylist', default=False,
17941321 334 help='Download only the video, if the URL refers to a video and a playlist.')
df4bd0d5
PH
335 selection.add_option(
336 '--yes-playlist',
337 action='store_false', dest='noplaylist', default=False,
17941321 338 help='Download the playlist, if the URL refers to a video and a playlist.')
8450c15c
PH
339 selection.add_option(
340 '--age-limit',
341 metavar='YEARS', dest='age_limit', default=None, type=int,
17941321 342 help='Download only videos suitable for the given age')
8450c15c
PH
343 selection.add_option(
344 '--download-archive', metavar='FILE',
345 dest='download_archive',
346 help='Download only videos not listed in the archive file. Record the IDs of all downloaded videos in it.')
347 selection.add_option(
348 '--include-ads',
349 dest='include_ads', action='store_true',
34a741a8 350 help='Download advertisements as well (experimental)')
34a741a8 351
8450c15c
PH
352 authentication = optparse.OptionGroup(parser, 'Authentication Options')
353 authentication.add_option(
354 '-u', '--username',
355 dest='username', metavar='USERNAME',
17941321 356 help='Login with this account ID')
8450c15c
PH
357 authentication.add_option(
358 '-p', '--password',
359 dest='password', metavar='PASSWORD',
cefecac1 360 help='Account password. If this option is left out, youtube-dlc will ask interactively.')
8450c15c
PH
361 authentication.add_option(
362 '-2', '--twofactor',
363 dest='twofactor', metavar='TWOFACTOR',
3540fe26 364 help='Two-factor authentication code')
8450c15c
PH
365 authentication.add_option(
366 '-n', '--netrc',
367 action='store_true', dest='usenetrc', default=False,
17941321 368 help='Use .netrc authentication data')
8450c15c
PH
369 authentication.add_option(
370 '--video-password',
371 dest='videopassword', metavar='PASSWORD',
804c343a 372 help='Video password (vimeo, smotri, youku)')
1b6712ab
RA
373
374 adobe_pass = optparse.OptionGroup(parser, 'Adobe Pass Options')
375 adobe_pass.add_option(
797c636b
RA
376 '--ap-mso',
377 dest='ap_mso', metavar='MSO',
537f7533 378 help='Adobe Pass multiple-system operator (TV provider) identifier, use --ap-list-mso for a list of available MSOs')
1b6712ab
RA
379 adobe_pass.add_option(
380 '--ap-username',
797c636b 381 dest='ap_username', metavar='USERNAME',
537f7533 382 help='Multiple-system operator account login')
1b6712ab
RA
383 adobe_pass.add_option(
384 '--ap-password',
797c636b 385 dest='ap_password', metavar='PASSWORD',
cefecac1 386 help='Multiple-system operator account password. If this option is left out, youtube-dlc will ask interactively.')
1b6712ab 387 adobe_pass.add_option(
87148bb7
RA
388 '--ap-list-mso',
389 action='store_true', dest='ap_list_mso', default=False,
537f7533 390 help='List all supported multiple-system operators')
8450c15c
PH
391
392 video_format = optparse.OptionGroup(parser, 'Video Format Options')
393 video_format.add_option(
394 '-f', '--format',
395 action='store', dest='format', metavar='FORMAT', default=None,
a3805092 396 help='Video format code, see the "FORMAT SELECTION" for all the info')
8450c15c
PH
397 video_format.add_option(
398 '--all-formats',
399 action='store_const', dest='format', const='all',
17941321 400 help='Download all available video formats')
8450c15c
PH
401 video_format.add_option(
402 '--prefer-free-formats',
403 action='store_true', dest='prefer_free_formats', default=False,
17941321 404 help='Prefer free video formats unless a specific one is requested')
8450c15c
PH
405 video_format.add_option(
406 '-F', '--list-formats',
407 action='store_true', dest='listformats',
4b3fbafd 408 help='List all available formats of requested videos')
203fb43f
PH
409 video_format.add_option(
410 '--youtube-include-dash-manifest',
411 action='store_true', dest='youtube_include_dash_manifest', default=True,
412 help=optparse.SUPPRESS_HELP)
413 video_format.add_option(
414 '--youtube-skip-dash-manifest',
415 action='store_false', dest='youtube_include_dash_manifest',
b2575b38 416 help='Do not download the DASH manifests and related data on YouTube videos')
78895bd3
U
417 video_format.add_option(
418 '--youtube-include-hls-manifest',
419 action='store_true', dest='youtube_include_hls_manifest', default=True,
420 help=optparse.SUPPRESS_HELP)
421 video_format.add_option(
422 '--youtube-skip-hls-manifest',
423 action='store_false', dest='youtube_include_hls_manifest',
424 help='Do not download the HLS manifests and related data on YouTube videos')
d120e901 425 video_format.add_option(
bd1a281e
PH
426 '--merge-output-format',
427 action='store', dest='merge_output_format', metavar='FORMAT', default=None,
d120e901 428 help=(
00334d0d
S
429 'If a merge is required (e.g. bestvideo+bestaudio), '
430 'output to given container format. One of mkv, mp4, ogg, webm, flv. '
d120e901 431 'Ignored if no merge is required'))
8450c15c
PH
432
433 subtitles = optparse.OptionGroup(parser, 'Subtitle Options')
434 subtitles.add_option(
435 '--write-sub', '--write-srt',
436 action='store_true', dest='writesubtitles', default=False,
17941321 437 help='Write subtitle file')
8450c15c
PH
438 subtitles.add_option(
439 '--write-auto-sub', '--write-automatic-sub',
440 action='store_true', dest='writeautomaticsub', default=False,
741dd8ea 441 help='Write automatically generated subtitle file (YouTube only)')
8450c15c
PH
442 subtitles.add_option(
443 '--all-subs',
444 action='store_true', dest='allsubtitles', default=False,
17941321 445 help='Download all the available subtitles of the video')
8450c15c
PH
446 subtitles.add_option(
447 '--list-subs',
448 action='store_true', dest='listsubtitles', default=False,
17941321 449 help='List all available subtitles for the video')
8450c15c
PH
450 subtitles.add_option(
451 '--sub-format',
a504ced0 452 action='store', dest='subtitlesformat', metavar='FORMAT', default='best',
4a3cdf81 453 help='Subtitle format, accepts formats preference, for example: "srt" or "ass/srt/best"')
8450c15c
PH
454 subtitles.add_option(
455 '--sub-lang', '--sub-langs', '--srt-lang',
456 action='callback', dest='subtitleslangs', metavar='LANGS', type='str',
457 default=[], callback=_comma_separated_values_options_callback,
1ca59dac 458 help='Languages of the subtitles to download (optional) separated by commas, use --list-subs for available language tags')
8450c15c
PH
459
460 downloader = optparse.OptionGroup(parser, 'Download Options')
461 downloader.add_option(
8ec2b2c4
S
462 '-r', '--limit-rate', '--rate-limit',
463 dest='ratelimit', metavar='RATE',
17941321 464 help='Maximum download rate in bytes per second (e.g. 50K or 4.2M)')
8450c15c
PH
465 downloader.add_option(
466 '-R', '--retries',
467 dest='retries', metavar='RETRIES', default=10,
17941321 468 help='Number of retries (default is %default), or "infinite".')
52bb437e
S
469 downloader.add_option(
470 '--fragment-retries',
471 dest='fragment_retries', metavar='RETRIES', default=10,
12ee65ea 472 help='Number of retries for a fragment (default is %default), or "infinite" (DASH, hlsnative and ISM)')
9603b660
S
473 downloader.add_option(
474 '--skip-unavailable-fragments',
475 action='store_true', dest='skip_unavailable_fragments', default=True,
12ee65ea 476 help='Skip unavailable fragments (DASH, hlsnative and ISM)')
732fb3f8 477 downloader.add_option(
9603b660
S
478 '--abort-on-unavailable-fragment',
479 action='store_false', dest='skip_unavailable_fragments',
480 help='Abort downloading when some fragment is not available')
0eee52f3
S
481 downloader.add_option(
482 '--keep-fragments',
483 action='store_true', dest='keep_fragments', default=False,
484 help='Keep downloaded fragments on disk after downloading is finished; fragments are erased by default')
8450c15c
PH
485 downloader.add_option(
486 '--buffer-size',
487 dest='buffersize', metavar='SIZE', default='1024',
17941321 488 help='Size of download buffer (e.g. 1024 or 16K) (default is %default)')
8450c15c
PH
489 downloader.add_option(
490 '--no-resize-buffer',
491 action='store_true', dest='noresizebuffer', default=False,
17941321 492 help='Do not automatically adjust the buffer size. By default, the buffer size is automatically resized from an initial value of SIZE.')
ba515388
S
493 downloader.add_option(
494 '--http-chunk-size',
495 dest='http_chunk_size', metavar='SIZE', default=None,
496 help='Size of a chunk for chunk-based HTTP downloading (e.g. 10485760 or 10M) (default is disabled). '
497 'May be useful for bypassing bandwidth throttling imposed by a webserver (experimental)')
8450c15c
PH
498 downloader.add_option(
499 '--test',
500 action='store_true', dest='test', default=False,
501 help=optparse.SUPPRESS_HELP)
ff815fe6
MS
502 downloader.add_option(
503 '--playlist-reverse',
504 action='store_true',
505 help='Download playlist videos in reverse order')
75822ca7
TC
506 downloader.add_option(
507 '--playlist-random',
508 action='store_true',
509 help='Download playlist videos in random order')
881e6a1f
PH
510 downloader.add_option(
511 '--xattr-set-filesize',
512 dest='xattr_set_filesize', action='store_true',
504f20dd 513 help='Set file xattribute ytdl.filesize with expected file size')
85729c51
PH
514 downloader.add_option(
515 '--hls-prefer-native',
bf09af3a 516 dest='hls_prefer_native', action='store_true', default=None,
870d5258 517 help='Use the native HLS downloader instead of ffmpeg')
bf09af3a
S
518 downloader.add_option(
519 '--hls-prefer-ffmpeg',
520 dest='hls_prefer_native', action='store_false', default=None,
521 help='Use ffmpeg instead of the native HLS downloader')
7d106a65
JMF
522 downloader.add_option(
523 '--hls-use-mpegts',
524 dest='hls_use_mpegts', action='store_true',
525 help='Use the mpegts container for HLS videos, allowing to play the '
53be8894 526 'video while downloading (some players may not be able to play it)')
222516d9
PH
527 downloader.add_option(
528 '--external-downloader',
529 dest='external_downloader', metavar='COMMAND',
c75f0b36 530 help='Use the specified external downloader. '
222516d9 531 'Currently supports %s' % ','.join(list_external_downloaders()))
c75f0b36
PH
532 downloader.add_option(
533 '--external-downloader-args',
534 dest='external_downloader_args', metavar='ARGS',
17941321 535 help='Give these arguments to the external downloader')
8450c15c
PH
536
537 workarounds = optparse.OptionGroup(parser, 'Workarounds')
34a741a8 538 workarounds.add_option(
8450c15c
PH
539 '--encoding',
540 dest='encoding', metavar='ENCODING',
34a741a8
PH
541 help='Force the specified encoding (experimental)')
542 workarounds.add_option(
8450c15c
PH
543 '--no-check-certificate',
544 action='store_true', dest='no_check_certificate', default=False,
17941321 545 help='Suppress HTTPS certificate validation')
34a741a8 546 workarounds.add_option(
8450c15c
PH
547 '--prefer-insecure',
548 '--prefer-unsecure', action='store_true', dest='prefer_insecure',
0c3e5f49 549 help='Use an unencrypted connection to retrieve information about the video. (Currently supported only for YouTube)')
34a741a8 550 workarounds.add_option(
8450c15c
PH
551 '--user-agent',
552 metavar='UA', dest='user_agent',
17941321 553 help='Specify a custom user agent')
34a741a8 554 workarounds.add_option(
8450c15c
PH
555 '--referer',
556 metavar='URL', dest='referer', default=None,
17941321 557 help='Specify a custom referer, use if the video access is restricted to one domain',
34a741a8
PH
558 )
559 workarounds.add_option(
8450c15c
PH
560 '--add-header',
561 metavar='FIELD:VALUE', dest='headers', action='append',
17941321 562 help='Specify a custom HTTP header and its value, separated by a colon \':\'. You can use this option multiple times',
34a741a8
PH
563 )
564 workarounds.add_option(
8450c15c
PH
565 '--bidi-workaround',
566 dest='bidi_workaround', action='store_true',
567 help='Work around terminals that lack bidirectional text support. Requires bidiv or fribidi executable in PATH')
5f0d813d 568 workarounds.add_option(
065bc354 569 '--sleep-interval', '--min-sleep-interval', metavar='SECONDS',
649f7966 570 dest='sleep_interval', type=float,
7aa589a5
S
571 help=(
572 'Number of seconds to sleep before each download when used alone '
573 'or a lower bound of a range for randomized sleep before each download '
574 '(minimum possible number of seconds to sleep) when used along with '
575 '--max-sleep-interval.'))
065bc354 576 workarounds.add_option(
577 '--max-sleep-interval', metavar='SECONDS',
578 dest='max_sleep_interval', type=float,
7aa589a5
S
579 help=(
580 'Upper bound of a range for randomized sleep before each download '
581 '(maximum possible number of seconds to sleep). Must only be used '
582 'along with --min-sleep-interval.'))
0c9df79e
U
583 workarounds.add_option(
584 '--sleep-subtitles',
9f448fcb 585 dest='sleep_interval_subtitles', action='store_true', default=0,
0c9df79e 586 help='Enforce sleep interval on subtitles as well')
34a741a8 587
8450c15c
PH
588 verbosity = optparse.OptionGroup(parser, 'Verbosity / Simulation Options')
589 verbosity.add_option(
590 '-q', '--quiet',
591 action='store_true', dest='quiet', default=False,
17941321 592 help='Activate quiet mode')
34a741a8
PH
593 verbosity.add_option(
594 '--no-warnings',
595 dest='no_warnings', action='store_true', default=False,
596 help='Ignore warnings')
8450c15c
PH
597 verbosity.add_option(
598 '-s', '--simulate',
599 action='store_true', dest='simulate', default=False,
17941321 600 help='Do not download the video and do not write anything to disk')
8450c15c
PH
601 verbosity.add_option(
602 '--skip-download',
603 action='store_true', dest='skip_download', default=False,
17941321 604 help='Do not download the video')
8450c15c
PH
605 verbosity.add_option(
606 '-g', '--get-url',
607 action='store_true', dest='geturl', default=False,
17941321 608 help='Simulate, quiet but print URL')
8450c15c
PH
609 verbosity.add_option(
610 '-e', '--get-title',
611 action='store_true', dest='gettitle', default=False,
17941321 612 help='Simulate, quiet but print title')
8450c15c
PH
613 verbosity.add_option(
614 '--get-id',
615 action='store_true', dest='getid', default=False,
17941321 616 help='Simulate, quiet but print id')
8450c15c
PH
617 verbosity.add_option(
618 '--get-thumbnail',
619 action='store_true', dest='getthumbnail', default=False,
17941321 620 help='Simulate, quiet but print thumbnail URL')
8450c15c
PH
621 verbosity.add_option(
622 '--get-description',
623 action='store_true', dest='getdescription', default=False,
17941321 624 help='Simulate, quiet but print video description')
8450c15c
PH
625 verbosity.add_option(
626 '--get-duration',
627 action='store_true', dest='getduration', default=False,
17941321 628 help='Simulate, quiet but print video length')
8450c15c
PH
629 verbosity.add_option(
630 '--get-filename',
631 action='store_true', dest='getfilename', default=False,
17941321 632 help='Simulate, quiet but print output filename')
8450c15c
PH
633 verbosity.add_option(
634 '--get-format',
635 action='store_true', dest='getformat', default=False,
17941321 636 help='Simulate, quiet but print output format')
8450c15c
PH
637 verbosity.add_option(
638 '-j', '--dump-json',
639 action='store_true', dest='dumpjson', default=False,
a355b57f 640 help='Simulate, quiet but print JSON information. See the "OUTPUT TEMPLATE" for a description of available keys.')
63e0be34
PH
641 verbosity.add_option(
642 '-J', '--dump-single-json',
643 action='store_true', dest='dump_single_json', default=False,
17941321 644 help='Simulate, quiet but print JSON information for each command-line argument. If the URL refers to a playlist, dump the whole playlist information in a single line.')
c0bdf32a
PH
645 verbosity.add_option(
646 '--print-json',
647 action='store_true', dest='print_json', default=False,
648 help='Be quiet and print the video information as JSON (video is still being downloaded).',
649 )
8450c15c
PH
650 verbosity.add_option(
651 '--newline',
652 action='store_true', dest='progress_with_newline', default=False,
17941321 653 help='Output progress bar as new lines')
8450c15c
PH
654 verbosity.add_option(
655 '--no-progress',
656 action='store_true', dest='noprogress', default=False,
17941321 657 help='Do not print progress bar')
8450c15c
PH
658 verbosity.add_option(
659 '--console-title',
660 action='store_true', dest='consoletitle', default=False,
17941321 661 help='Display progress in console titlebar')
8450c15c
PH
662 verbosity.add_option(
663 '-v', '--verbose',
664 action='store_true', dest='verbose', default=False,
17941321 665 help='Print various debugging information')
8450c15c 666 verbosity.add_option(
8bba753c 667 '--dump-pages', '--dump-intermediate-pages',
8450c15c 668 action='store_true', dest='dump_intermediate_pages', default=False,
79979c68 669 help='Print downloaded pages encoded using base64 to debug problems (very verbose)')
8450c15c
PH
670 verbosity.add_option(
671 '--write-pages',
672 action='store_true', dest='write_pages', default=False,
673 help='Write downloaded intermediary pages to files in the current directory to debug problems')
674 verbosity.add_option(
675 '--youtube-print-sig-code',
676 action='store_true', dest='youtube_print_sig_code', default=False,
677 help=optparse.SUPPRESS_HELP)
678 verbosity.add_option(
2f543a21 679 '--print-traffic', '--dump-headers',
8450c15c
PH
680 dest='debug_printtraffic', action='store_true', default=False,
681 help='Display sent and read HTTP traffic')
58b1f00d
PH
682 verbosity.add_option(
683 '-C', '--call-home',
684 dest='call_home', action='store_true', default=False,
cefecac1 685 help='Contact the youtube-dlc server for debugging')
8bfa7545
PH
686 verbosity.add_option(
687 '--no-call-home',
688 dest='call_home', action='store_false', default=False,
cefecac1 689 help='Do NOT contact the youtube-dlc server for debugging')
8450c15c
PH
690
691 filesystem = optparse.OptionGroup(parser, 'Filesystem Options')
692 filesystem.add_option(
693 '-a', '--batch-file',
694 dest='batchfile', metavar='FILE',
5d60b997
AR
695 help="File containing URLs to download ('-' for stdin), one URL per line. "
696 "Lines starting with '#', ';' or ']' are considered as comments and ignored.")
8450c15c
PH
697 filesystem.add_option(
698 '--id', default=False,
17941321 699 action='store_true', dest='useid', help='Use only video ID in file name')
8450c15c
PH
700 filesystem.add_option(
701 '-o', '--output',
702 dest='outtmpl', metavar='TEMPLATE',
8b2dc4c3 703 help=('Output filename template, see the "OUTPUT TEMPLATE" for all the info'))
8450c15c
PH
704 filesystem.add_option(
705 '--autonumber-size',
be5df5ee
S
706 dest='autonumber_size', metavar='NUMBER', type=int,
707 help=optparse.SUPPRESS_HELP)
acbb2374
CP
708 filesystem.add_option(
709 '--autonumber-start',
1a241a2d
S
710 dest='autonumber_start', metavar='NUMBER', default=1, type=int,
711 help='Specify the start value for %(autonumber)s (default is %default)')
8450c15c
PH
712 filesystem.add_option(
713 '--restrict-filenames',
714 action='store_true', dest='restrictfilenames', default=False,
715 help='Restrict filenames to only ASCII characters, and avoid "&" and spaces in filenames')
2865cf04
PH
716 filesystem.add_option(
717 '-A', '--auto-number',
718 action='store_true', dest='autonumber', default=False,
be5df5ee 719 help=optparse.SUPPRESS_HELP)
8450c15c
PH
720 filesystem.add_option(
721 '-t', '--title',
722 action='store_true', dest='usetitle', default=False,
be5df5ee 723 help=optparse.SUPPRESS_HELP)
8450c15c
PH
724 filesystem.add_option(
725 '-l', '--literal', default=False,
726 action='store_true', dest='usetitle',
be5df5ee 727 help=optparse.SUPPRESS_HELP)
8450c15c
PH
728 filesystem.add_option(
729 '-w', '--no-overwrites',
730 action='store_true', dest='nooverwrites', default=False,
17941321 731 help='Do not overwrite files')
8450c15c
PH
732 filesystem.add_option(
733 '-c', '--continue',
734 action='store_true', dest='continue_dl', default=True,
cefecac1 735 help='Force resume of partially downloaded files. By default, youtube-dlc will resume downloads if possible.')
8450c15c
PH
736 filesystem.add_option(
737 '--no-continue',
738 action='store_false', dest='continue_dl',
17941321 739 help='Do not resume partially downloaded files (restart from beginning)')
8450c15c
PH
740 filesystem.add_option(
741 '--no-part',
742 action='store_true', dest='nopart', default=False,
17941321 743 help='Do not use .part files - write directly into output file')
8450c15c
PH
744 filesystem.add_option(
745 '--no-mtime',
746 action='store_false', dest='updatetime', default=True,
17941321 747 help='Do not use the Last-modified header to set the file modification time')
8450c15c
PH
748 filesystem.add_option(
749 '--write-description',
750 action='store_true', dest='writedescription', default=False,
17941321 751 help='Write video description to a .description file')
8450c15c
PH
752 filesystem.add_option(
753 '--write-info-json',
754 action='store_true', dest='writeinfojson', default=False,
17941321 755 help='Write video metadata to a .info.json file')
8450c15c
PH
756 filesystem.add_option(
757 '--write-annotations',
758 action='store_true', dest='writeannotations', default=False,
0669c89c 759 help='Write video annotations to a .annotations.xml file')
8450c15c 760 filesystem.add_option(
244fe977 761 '--load-info-json', '--load-info',
8450c15c 762 dest='load_info_filename', metavar='FILE',
1a48181a 763 help='JSON file containing the video information (created with the "--write-info-json" option)')
8450c15c
PH
764 filesystem.add_option(
765 '--cookies',
766 dest='cookiefile', metavar='FILE',
17941321 767 help='File to read cookies from and dump cookie jar in')
34a741a8
PH
768 filesystem.add_option(
769 '--cache-dir', dest='cachedir', default=None, metavar='DIR',
cefecac1 770 help='Location in the filesystem where youtube-dlc can store some downloaded information permanently. By default $XDG_CACHE_HOME/youtube-dlc or ~/.cache/youtube-dlc . At the moment, only YouTube player files (for videos with obfuscated signatures) are cached, but that may change.')
34a741a8
PH
771 filesystem.add_option(
772 '--no-cache-dir', action='store_const', const=False, dest='cachedir',
773 help='Disable filesystem caching')
774 filesystem.add_option(
8450c15c
PH
775 '--rm-cache-dir',
776 action='store_true', dest='rm_cachedir',
34a741a8 777 help='Delete all filesystem cache files')
bdc3fd2f
U
778 filesystem.add_option(
779 '--trim-file-name', dest='trim_file_name', default=0, type=int,
780 help='Limit the filename length (extension excluded)')
34a741a8 781
cfb56d1a
PH
782 thumbnail = optparse.OptionGroup(parser, 'Thumbnail images')
783 thumbnail.add_option(
784 '--write-thumbnail',
785 action='store_true', dest='writethumbnail', default=False,
17941321 786 help='Write thumbnail image to disk')
ec82d85a
PH
787 thumbnail.add_option(
788 '--write-all-thumbnails',
789 action='store_true', dest='write_all_thumbnails', default=False,
17941321 790 help='Write all thumbnail image formats to disk')
cfb56d1a
PH
791 thumbnail.add_option(
792 '--list-thumbnails',
793 action='store_true', dest='list_thumbnails', default=False,
794 help='Simulate and list all available thumbnail formats')
795
8450c15c
PH
796 postproc = optparse.OptionGroup(parser, 'Post-processing Options')
797 postproc.add_option(
798 '-x', '--extract-audio',
799 action='store_true', dest='extractaudio', default=False,
17941321 800 help='Convert video files to audio-only files (requires ffmpeg or avconv and ffprobe or avprobe)')
8450c15c
PH
801 postproc.add_option(
802 '--audio-format', metavar='FORMAT', dest='audioformat', default='best',
0efbc6b5 803 help='Specify audio format: "best", "aac", "flac", "mp3", "m4a", "opus", "vorbis", or "wav"; "%default" by default; No effect without -x')
8450c15c
PH
804 postproc.add_option(
805 '--audio-quality', metavar='QUALITY',
806 dest='audioquality', default='5',
17941321 807 help='Specify ffmpeg/avconv audio quality, insert a value between 0 (better) and 9 (worse) for VBR or a specific bitrate like 128K (default %default)')
efe87a10
FS
808 postproc.add_option(
809 '--remux-video',
810 metavar='FORMAT', dest='remuxvideo', default=None,
811 help='Remux the video to another container format if necessary (currently supported: mp4|mkv, target container format must support video / audio encoding, remuxing may fail)')
8450c15c
PH
812 postproc.add_option(
813 '--recode-video',
814 metavar='FORMAT', dest='recodevideo', default=None,
f72b0a60 815 help='Encode the video to another format if necessary (currently supported: mp4|flv|ogg|webm|mkv|avi)')
d84f1d14 816 postproc.add_option(
1866432d 817 '--postprocessor-args',
f72b0a60
S
818 dest='postprocessor_args', metavar='ARGS',
819 help='Give these arguments to the postprocessor')
8450c15c
PH
820 postproc.add_option(
821 '-k', '--keep-video',
822 action='store_true', dest='keepvideo', default=False,
17941321 823 help='Keep the video file on disk after the post-processing; the video is erased by default')
8450c15c
PH
824 postproc.add_option(
825 '--no-post-overwrites',
826 action='store_true', dest='nopostoverwrites', default=False,
17941321 827 help='Do not overwrite post-processed files; the post-processed files are overwritten by default')
8450c15c
PH
828 postproc.add_option(
829 '--embed-subs',
830 action='store_true', dest='embedsubtitles', default=False,
40025ee2 831 help='Embed subtitles in the video (only for mp4, webm and mkv videos)')
8450c15c
PH
832 postproc.add_option(
833 '--embed-thumbnail',
834 action='store_true', dest='embedthumbnail', default=False,
17941321 835 help='Embed thumbnail in the audio as cover art')
8450c15c
PH
836 postproc.add_option(
837 '--add-metadata',
838 action='store_true', dest='addmetadata', default=False,
17941321 839 help='Write metadata to the video file')
e7db87f7 840 postproc.add_option(
841 '--metadata-from-title',
842 metavar='FORMAT', dest='metafromtitle',
17941321 843 help='Parse additional metadata like song title / artist from the video title. '
fa26734e
S
844 'The format syntax is the same as --output. Regular expression with '
845 'named capture groups may also be used. '
846 'The parsed parameters replace existing values. '
166d12b0 847 'Example: --metadata-from-title "%(artist)s - %(title)s" matches a title like '
fa26734e
S
848 '"Coldplay - Paradise". '
849 'Example (regex): --metadata-from-title "(?P<artist>.+?) - (?P<title>.+)"')
8450c15c
PH
850 postproc.add_option(
851 '--xattrs',
852 action='store_true', dest='xattrs', default=False,
17941321 853 help='Write metadata to the video file\'s xattrs (using dublin core and xdg standards)')
6271f1ca
PH
854 postproc.add_option(
855 '--fixup',
856 metavar='POLICY', dest='fixup', default='detect_or_warn',
9fffd046 857 help='Automatically correct known faults of the file. '
6271f1ca 858 'One of never (do nothing), warn (only emit a warning), '
5774ef35 859 'detect_or_warn (the default; fix file if we can, warn otherwise)')
8450c15c
PH
860 postproc.add_option(
861 '--prefer-avconv',
862 action='store_false', dest='prefer_ffmpeg',
d4a24f40 863 help='Prefer avconv over ffmpeg for running the postprocessors')
8450c15c
PH
864 postproc.add_option(
865 '--prefer-ffmpeg',
866 action='store_true', dest='prefer_ffmpeg',
d4a24f40 867 help='Prefer ffmpeg over avconv for running the postprocessors (default)')
73fac4e9
PH
868 postproc.add_option(
869 '--ffmpeg-location', '--avconv-location', metavar='PATH',
870 dest='ffmpeg_location',
871 help='Location of the ffmpeg/avconv binary; either the path to the binary or its containing directory.')
34a741a8 872 postproc.add_option(
8450c15c
PH
873 '--exec',
874 metavar='CMD', dest='exec_cmd',
46d0baf9 875 help='Execute a command on the file after downloading and post-processing, similar to find\'s -exec syntax. Example: --exec \'adb push {} /sdcard/Music/ && rm {}\'')
e9fade72 876 postproc.add_option(
f5bc4b5f 877 '--convert-subs', '--convert-subtitles',
e9fade72 878 metavar='FORMAT', dest='convertsubtitles', default=None,
8c289530 879 help='Convert the subtitles to other format (currently supported: srt|ass|vtt|lrc)')
34a741a8 880
78895bd3
U
881 extractor = optparse.OptionGroup(parser, 'Extractor Options')
882 extractor.add_option(
883 '--allow-dynamic-mpd',
884 action='store_true', dest='dynamic_mpd', default=True,
885 help=optparse.SUPPRESS_HELP)
886 extractor.add_option(
887 '--ignore-dynamic-mpd',
888 action='store_false', dest='dynamic_mpd',
889 help='Do not process dynamic DASH manifests')
890
34a741a8 891 parser.add_option_group(general)
be4a824d 892 parser.add_option_group(network)
0aa10994 893 parser.add_option_group(geo)
34a741a8
PH
894 parser.add_option_group(selection)
895 parser.add_option_group(downloader)
896 parser.add_option_group(filesystem)
cfb56d1a 897 parser.add_option_group(thumbnail)
34a741a8
PH
898 parser.add_option_group(verbosity)
899 parser.add_option_group(workarounds)
900 parser.add_option_group(video_format)
901 parser.add_option_group(subtitles)
902 parser.add_option_group(authentication)
d2522b86 903 parser.add_option_group(adobe_pass)
34a741a8 904 parser.add_option_group(postproc)
78895bd3 905 parser.add_option_group(extractor)
34a741a8
PH
906
907 if overrideArguments is not None:
908 opts, args = parser.parse_args(overrideArguments)
909 if opts.verbose:
8450c15c 910 write_string('[debug] Override config: ' + repr(overrideArguments) + '\n')
34a741a8 911 else:
b04b94da
S
912 def compat_conf(conf):
913 if sys.version_info < (3,):
914 return [a.decode(preferredencoding(), 'replace') for a in conf]
915 return conf
916
917 command_line_conf = compat_conf(sys.argv[1:])
e66dca5e 918 opts, args = parser.parse_args(command_line_conf)
f5e2efbb 919
b6ee45e9
S
920 system_conf = user_conf = custom_conf = []
921
922 if '--config-location' in command_line_conf:
923 location = compat_expanduser(opts.config_location)
924 if os.path.isdir(location):
cefecac1 925 location = os.path.join(location, 'youtube-dlc.conf')
b6ee45e9
S
926 if not os.path.exists(location):
927 parser.error('config-location %s does not exist.' % location)
928 custom_conf = _readOptions(location)
929 elif '--ignore-config' in command_line_conf:
930 pass
34a741a8 931 else:
cefecac1 932 system_conf = _readOptions('/etc/youtube-dlc.conf')
b6ee45e9 933 if '--ignore-config' not in system_conf:
30d22dae 934 user_conf = _readUserConf()
e66dca5e 935
0ce8c66f 936 argv = system_conf + user_conf + custom_conf + command_line_conf
34a741a8
PH
937 opts, args = parser.parse_args(argv)
938 if opts.verbose:
b6ee45e9
S
939 for conf_label, conf in (
940 ('System config', system_conf),
941 ('User config', user_conf),
942 ('Custom config', custom_conf),
943 ('Command-line args', command_line_conf)):
944 write_string('[debug] %s: %s\n' % (conf_label, repr(_hide_login_info(conf))))
34a741a8
PH
945
946 return parser, opts, args