Improve --sub-langs (see desc)

* Treat `--sub-langs` entries as regex
* `all` can be used to refer to all the subtitles
* the language code can be prefixed with `-` to exclude it
* Deprecates `--all-subs`
Closes #253
This commit is contained in:
pukkandan 2021-04-20 02:47:09 +05:30
parent 3097d9e512
commit c32b0aab8a
No known key found for this signature in database
GPG Key ID: 0F00D95A001F4698
4 changed files with 59 additions and 16 deletions

View File

@ -594,15 +594,17 @@ Then simply run `make`. You can also run `make yt-dlp` instead to compile only t
(Alias: --write-automatic-subs) (Alias: --write-automatic-subs)
--no-write-auto-subs Do not write auto-generated subtitles --no-write-auto-subs Do not write auto-generated subtitles
(default) (Alias: --no-write-automatic-subs) (default) (Alias: --no-write-automatic-subs)
--all-subs Download all the available subtitles of the
video
--list-subs List all available subtitles for the video --list-subs List all available subtitles for the video
--sub-format FORMAT Subtitle format, accepts formats --sub-format FORMAT Subtitle format, accepts formats
preference, for example: "srt" or preference, for example: "srt" or
"ass/srt/best" "ass/srt/best"
--sub-langs LANGS Languages of the subtitles to download --sub-langs LANGS Languages of the subtitles to download (can
(optional) separated by commas, use --list- be regex) or "all" separated by commas.
subs for available language tags (Eg: --sub-langs en.*,ja) You can prefix
the language code with a "-" to exempt it
from the requested languages. (Eg: --sub-
langs all,-live_chat) Use --list-subs for a
list of available language tags
## Authentication Options: ## Authentication Options:
-u, --username USERNAME Login with this account ID -u, --username USERNAME Login with this account ID
@ -1257,6 +1259,7 @@ These are all the deprecated options and the current alternative to achieve the
-t, --title -o "%(title)s-%(id)s.%(ext)s" -t, --title -o "%(title)s-%(id)s.%(ext)s"
-l, --literal -o accepts literal names -l, --literal -o accepts literal names
--all-formats -f all --all-formats -f all
--all-subs --sub-langs all --write-subs
--autonumber-size NUMBER Use string formatting. Eg: %(autonumber)03d --autonumber-size NUMBER Use string formatting. Eg: %(autonumber)03d
--metadata-from-title FORMAT --parse-metadata "%(title)s:FORMAT" --metadata-from-title FORMAT --parse-metadata "%(title)s:FORMAT"
--prefer-avconv avconv is no longer officially supported (Alias: --no-prefer-ffmpeg) --prefer-avconv avconv is no longer officially supported (Alias: --no-prefer-ffmpeg)

View File

@ -601,6 +601,26 @@ class TestYoutubeDL(unittest.TestCase):
self.assertTrue(subs) self.assertTrue(subs)
self.assertEqual(set(subs.keys()), set(['es', 'fr'])) self.assertEqual(set(subs.keys()), set(['es', 'fr']))
result = get_info({'writesubtitles': True, 'subtitleslangs': ['all', '-en']})
subs = result['requested_subtitles']
self.assertTrue(subs)
self.assertEqual(set(subs.keys()), set(['es', 'fr']))
result = get_info({'writesubtitles': True, 'subtitleslangs': ['en', 'fr', '-en']})
subs = result['requested_subtitles']
self.assertTrue(subs)
self.assertEqual(set(subs.keys()), set(['fr']))
result = get_info({'writesubtitles': True, 'subtitleslangs': ['-en', 'en']})
subs = result['requested_subtitles']
self.assertTrue(subs)
self.assertEqual(set(subs.keys()), set(['en']))
result = get_info({'writesubtitles': True, 'subtitleslangs': ['e.+']})
subs = result['requested_subtitles']
self.assertTrue(subs)
self.assertEqual(set(subs.keys()), set(['es', 'en']))
result = get_info({'writesubtitles': True, 'writeautomaticsub': True, 'subtitleslangs': ['es', 'pt']}) result = get_info({'writesubtitles': True, 'writeautomaticsub': True, 'subtitleslangs': ['es', 'pt']})
subs = result['requested_subtitles'] subs = result['requested_subtitles']
self.assertTrue(subs) self.assertTrue(subs)

View File

@ -244,11 +244,15 @@ class YoutubeDL(object):
writedesktoplink: Write a Linux internet shortcut file (.desktop) writedesktoplink: Write a Linux internet shortcut file (.desktop)
writesubtitles: Write the video subtitles to a file writesubtitles: Write the video subtitles to a file
writeautomaticsub: Write the automatically generated subtitles to a file writeautomaticsub: Write the automatically generated subtitles to a file
allsubtitles: Downloads all the subtitles of the video allsubtitles: Deprecated - Use subtitlelangs = ['all']
Downloads all the subtitles of the video
(requires writesubtitles or writeautomaticsub) (requires writesubtitles or writeautomaticsub)
listsubtitles: Lists all available subtitles for the video listsubtitles: Lists all available subtitles for the video
subtitlesformat: The format code for subtitles subtitlesformat: The format code for subtitles
subtitleslangs: List of languages of the subtitles to download subtitleslangs: List of languages of the subtitles to download (can be regex).
The list may contain "all" to refer to all the available
subtitles. The language can be prefixed with a "-" to
exclude it from the requested languages. Eg: ['all', '-live_chat']
keepvideo: Keep the video file after post-processing keepvideo: Keep the video file after post-processing
daterange: A DateRange object, download only if the upload_date is in the range. daterange: A DateRange object, download only if the upload_date is in the range.
skip_download: Skip the actual download of the video file skip_download: Skip the actual download of the video file
@ -2038,15 +2042,28 @@ class YoutubeDL(object):
available_subs): available_subs):
return None return None
all_sub_langs = available_subs.keys()
if self.params.get('allsubtitles', False): if self.params.get('allsubtitles', False):
requested_langs = available_subs.keys() requested_langs = all_sub_langs
elif self.params.get('subtitleslangs', False):
requested_langs = set()
for lang in self.params.get('subtitleslangs'):
if lang == 'all':
requested_langs.update(all_sub_langs)
continue
discard = lang[0] == '-'
if discard:
lang = lang[1:]
current_langs = filter(re.compile(lang + '$').match, all_sub_langs)
if discard:
for lang in current_langs:
requested_langs.discard(lang)
else:
requested_langs.update(current_langs)
elif 'en' in available_subs:
requested_langs = ['en']
else: else:
if self.params.get('subtitleslangs', False): requested_langs = [list(all_sub_langs)[0]]
requested_langs = self.params.get('subtitleslangs')
elif 'en' in available_subs:
requested_langs = ['en']
else:
requested_langs = [list(available_subs.keys())[0]]
formats_query = self.params.get('subtitlesformat', 'best') formats_query = self.params.get('subtitlesformat', 'best')
formats_preference = formats_query.split('/') if formats_query else [] formats_preference = formats_query.split('/') if formats_query else []

View File

@ -548,7 +548,7 @@ def parseOpts(overrideArguments=None):
subtitles.add_option( subtitles.add_option(
'--all-subs', '--all-subs',
action='store_true', dest='allsubtitles', default=False, action='store_true', dest='allsubtitles', default=False,
help='Download all the available subtitles of the video') help=optparse.SUPPRESS_HELP)
subtitles.add_option( subtitles.add_option(
'--list-subs', '--list-subs',
action='store_true', dest='listsubtitles', default=False, action='store_true', dest='listsubtitles', default=False,
@ -561,7 +561,10 @@ def parseOpts(overrideArguments=None):
'--sub-langs', '--srt-langs', '--sub-langs', '--srt-langs',
action='callback', dest='subtitleslangs', metavar='LANGS', type='str', action='callback', dest='subtitleslangs', metavar='LANGS', type='str',
default=[], callback=_comma_separated_values_options_callback, default=[], callback=_comma_separated_values_options_callback,
help='Languages of the subtitles to download (optional) separated by commas, use --list-subs for available language tags') help=(
'Languages of the subtitles to download (can be regex) or "all" separated by commas. (Eg: --sub-langs en.*,ja) '
'You can prefix the language code with a "-" to exempt it from the requested languages. (Eg: --sub-langs all,-live_chat) '
'Use --list-subs for a list of available language tags'))
downloader = optparse.OptionGroup(parser, 'Download Options') downloader = optparse.OptionGroup(parser, 'Download Options')
downloader.add_option( downloader.add_option(