mirror of
https://github.com/ytdl-org/youtube-dl
synced 2024-11-27 19:36:50 +01:00
Refactor subtitle options from srt to the more generic 'sub'.
In order to be more consistent with different subtitle formats. From: * --write-srt to --write-sub * --only-srt to --only-sub * --all-srt to --all-subs * --srt-lang to --sub-lang' Refactored also all the mentions of srt for sub in all the source code.
This commit is contained in:
parent
ae608b8076
commit
553d097442
@ -78,7 +78,7 @@ class FileDownloader(object):
|
||||
updatetime: Use the Last-modified header to set output file timestamps.
|
||||
writedescription: Write the video description to a .description file
|
||||
writeinfojson: Write the video description to a .info.json file
|
||||
writesubtitles: Write the video subtitles to a .srt file
|
||||
writesubtitles: Write the video subtitles to a file (default=srt)
|
||||
onlysubtitles: Downloads only the subtitles of the video
|
||||
allsubtitles: Downloads all the subtitles of the video
|
||||
subtitleslang: Language of the subtitles to download
|
||||
@ -291,9 +291,9 @@ class FileDownloader(object):
|
||||
""" Report that the description file is being written """
|
||||
self.to_screen(u'[info] Writing video description to: ' + descfn)
|
||||
|
||||
def report_writesubtitles(self, srtfn):
|
||||
def report_writesubtitles(self, sub_filename):
|
||||
""" Report that the subtitles file is being written """
|
||||
self.to_screen(u'[info] Writing video subtitles to: ' + srtfn)
|
||||
self.to_screen(u'[info] Writing video subtitles to: ' + sub_filename)
|
||||
|
||||
def report_writeinfojson(self, infofn):
|
||||
""" Report that the metadata file has been written """
|
||||
@ -444,12 +444,12 @@ class FileDownloader(object):
|
||||
# subtitles download errors are already managed as troubles in relevant IE
|
||||
# that way it will silently go on when used with unsupporting IE
|
||||
subtitle = info_dict['subtitles'][0]
|
||||
(srt_error, srt_lang, srt) = subtitle
|
||||
(sub_error, sub_lang, sub) = subtitle
|
||||
try:
|
||||
srtfn = filename.rsplit('.', 1)[0] + u'.' + srt_lang + u'.srt'
|
||||
self.report_writesubtitles(srtfn)
|
||||
with io.open(encodeFilename(srtfn), 'w', encoding='utf-8') as srtfile:
|
||||
srtfile.write(srt)
|
||||
sub_filename = filename.rsplit('.', 1)[0] + u'.' + sub_lang + u'.srt'
|
||||
self.report_writesubtitles(sub_filename)
|
||||
with io.open(encodeFilename(sub_filename), 'w', encoding='utf-8') as subfile:
|
||||
subfile.write(sub)
|
||||
except (OSError, IOError):
|
||||
self.trouble(u'ERROR: Cannot write subtitles file ' + descfn)
|
||||
return
|
||||
@ -459,12 +459,12 @@ class FileDownloader(object):
|
||||
if self.params.get('allsubtitles', False) and 'subtitles' in info_dict and info_dict['subtitles']:
|
||||
subtitles = info_dict['subtitles']
|
||||
for subtitle in subtitles:
|
||||
(srt_error, srt_lang, srt) = subtitle
|
||||
(sub_error, sub_lang, sub) = subtitle
|
||||
try:
|
||||
srtfn = filename.rsplit('.', 1)[0] + u'.' + srt_lang + u'.srt'
|
||||
self.report_writesubtitles(srtfn)
|
||||
with io.open(encodeFilename(srtfn), 'w', encoding='utf-8') as srtfile:
|
||||
srtfile.write(srt)
|
||||
sub_filename = filename.rsplit('.', 1)[0] + u'.' + sub_lang + u'.srt'
|
||||
self.report_writesubtitles(sub_filename)
|
||||
with io.open(encodeFilename(sub_filename), 'w', encoding='utf-8') as subfile:
|
||||
subfile.write(sub)
|
||||
except (OSError, IOError):
|
||||
self.trouble(u'ERROR: Cannot write subtitles file ' + descfn)
|
||||
return
|
||||
|
@ -47,7 +47,7 @@ class InfoExtractor(object):
|
||||
uploader_id: Nickname or id of the video uploader.
|
||||
location: Physical location of the video.
|
||||
player_url: SWF Player URL (used for rtmpdump).
|
||||
subtitles: The .srt file contents.
|
||||
subtitles: The subtitle file contents.
|
||||
urlhandle: [internal] The urlHandle to be used to download the file,
|
||||
like returned by urllib.request.urlopen
|
||||
|
||||
@ -235,56 +235,56 @@ class YoutubeIE(InfoExtractor):
|
||||
def _get_available_subtitles(self, video_id):
|
||||
request = compat_urllib_request.Request('http://video.google.com/timedtext?hl=en&type=list&v=%s' % video_id)
|
||||
try:
|
||||
srt_list = compat_urllib_request.urlopen(request).read().decode('utf-8')
|
||||
sub_list = compat_urllib_request.urlopen(request).read().decode('utf-8')
|
||||
except (compat_urllib_error.URLError, compat_http_client.HTTPException, socket.error) as err:
|
||||
return (u'WARNING: unable to download video subtitles: %s' % compat_str(err), None)
|
||||
srt_lang_list = re.findall(r'name="([^"]*)"[^>]+lang_code="([\w\-]+)"', srt_list)
|
||||
srt_lang_list = dict((l[1], l[0]) for l in srt_lang_list)
|
||||
if not srt_lang_list:
|
||||
sub_lang_list = re.findall(r'name="([^"]*)"[^>]+lang_code="([\w\-]+)"', sub_list)
|
||||
sub_lang_list = dict((l[1], l[0]) for l in sub_lang_list)
|
||||
if not sub_lang_list:
|
||||
return (u'WARNING: video has no closed captions', None)
|
||||
return srt_lang_list
|
||||
return sub_lang_list
|
||||
|
||||
def _request_subtitle(self, str_lang, str_name, video_id, format = 'srt'):
|
||||
self.report_video_subtitles_request(video_id, str_lang)
|
||||
def _request_subtitle(self, sub_lang, sub_name, video_id, format = 'srt'):
|
||||
self.report_video_subtitles_request(video_id, sub_lang)
|
||||
params = compat_urllib_parse.urlencode({
|
||||
'lang': str_lang,
|
||||
'name': str_name,
|
||||
'lang': sub_lang,
|
||||
'name': sub_name,
|
||||
'v': video_id,
|
||||
'fmt': format,
|
||||
})
|
||||
url = 'http://www.youtube.com/api/timedtext?' + params
|
||||
try:
|
||||
srt = compat_urllib_request.urlopen(url).read().decode('utf-8')
|
||||
sub = compat_urllib_request.urlopen(url).read().decode('utf-8')
|
||||
except (compat_urllib_error.URLError, compat_http_client.HTTPException, socket.error) as err:
|
||||
return (u'WARNING: unable to download video subtitles: %s' % compat_str(err), None)
|
||||
if not srt:
|
||||
if not sub:
|
||||
return (u'WARNING: Did not fetch video subtitles', None)
|
||||
return (None, str_lang, srt)
|
||||
return (None, sub_lang, sub)
|
||||
|
||||
def _extract_subtitle(self, video_id):
|
||||
self.report_video_subtitles_download(video_id)
|
||||
srt_lang_list = self._get_available_subtitles(video_id)
|
||||
sub_lang_list = self._get_available_subtitles(video_id)
|
||||
|
||||
if self._downloader.params.get('subtitleslang', False):
|
||||
srt_lang = self._downloader.params.get('subtitleslang')
|
||||
elif 'en' in srt_lang_list:
|
||||
srt_lang = 'en'
|
||||
sub_lang = self._downloader.params.get('subtitleslang')
|
||||
elif 'en' in sub_lang_list:
|
||||
sub_lang = 'en'
|
||||
else:
|
||||
srt_lang = list(srt_lang_list.keys())[0]
|
||||
if not srt_lang in srt_lang_list:
|
||||
return (u'WARNING: no closed captions found in the specified language "%s"' % srt_lang, None)
|
||||
sub_lang = list(sub_lang_list.keys())[0]
|
||||
if not sub_lang in sub_lang_list:
|
||||
return (u'WARNING: no closed captions found in the specified language "%s"' % sub_lang, None)
|
||||
|
||||
sub = self._request_subtitle(srt_lang, srt_lang_list[srt_lang].encode('utf-8'), video_id)
|
||||
return [sub]
|
||||
subtitle = self._request_subtitle(sub_lang, sub_lang_list[sub_lang].encode('utf-8'), video_id)
|
||||
return [subtitle]
|
||||
|
||||
def _extract_all_subtitles(self, video_id):
|
||||
self.report_video_subtitles_download(video_id)
|
||||
srt_lang_list = self._get_available_subtitles(video_id)
|
||||
subs = []
|
||||
for srt_lang in srt_lang_list:
|
||||
sub = self._request_subtitle(srt_lang, srt_lang_list[srt_lang].encode('utf-8'), video_id)
|
||||
subs.append(sub)
|
||||
return subs
|
||||
sub_lang_list = self._get_available_subtitles(video_id)
|
||||
subtitles = []
|
||||
for sub_lang in sub_lang_list:
|
||||
subtitle = self._request_subtitle(sub_lang, sub_lang_list[sub_lang].encode('utf-8'), video_id)
|
||||
subtitles.append(subtitle)
|
||||
return subtitles
|
||||
|
||||
def _print_formats(self, formats):
|
||||
print('Available formats:')
|
||||
@ -511,16 +511,16 @@ class YoutubeIE(InfoExtractor):
|
||||
if self._downloader.params.get('writesubtitles', False):
|
||||
video_subtitles = self._extract_subtitle(video_id)
|
||||
if video_subtitles:
|
||||
(srt_error, srt_lang, srt) = video_subtitles[0]
|
||||
if srt_error:
|
||||
self._downloader.trouble(srt_error)
|
||||
(sub_error, sub_lang, sub) = video_subtitles[0]
|
||||
if sub_error:
|
||||
self._downloader.trouble(sub_error)
|
||||
|
||||
if self._downloader.params.get('allsubtitles', False):
|
||||
video_subtitles = self._extract_all_subtitles(video_id)
|
||||
for video_subtitle in video_subtitles:
|
||||
(srt_error, srt_lang, srt) = video_subtitle
|
||||
if srt_error:
|
||||
self._downloader.trouble(srt_error)
|
||||
(sub_error, sub_lang, sub) = video_subtitle
|
||||
if sub_error:
|
||||
self._downloader.trouble(sub_error)
|
||||
|
||||
if 'length_seconds' not in video_info:
|
||||
self._downloader.trouble(u'WARNING: unable to extract video duration')
|
||||
|
@ -173,18 +173,18 @@ def parseOpts():
|
||||
action='store', dest='format_limit', metavar='FORMAT', help='highest quality format to download')
|
||||
video_format.add_option('-F', '--list-formats',
|
||||
action='store_true', dest='listformats', help='list all available formats (currently youtube only)')
|
||||
video_format.add_option('--write-srt',
|
||||
video_format.add_option('--write-sub',
|
||||
action='store_true', dest='writesubtitles',
|
||||
help='write video closed captions to a .srt file (currently youtube only)', default=False)
|
||||
video_format.add_option('--only-srt',
|
||||
help='write subtitle file (currently youtube only)', default=False)
|
||||
video_format.add_option('--only-sub',
|
||||
action='store_true', dest='onlysubtitles',
|
||||
help='downloads only the subtitles of the video (currently youtube only)', default=False)
|
||||
video_format.add_option('--all-srt',
|
||||
help='downloads only the subtitles (no video)', default=False)
|
||||
video_format.add_option('--all-subs',
|
||||
action='store_true', dest='allsubtitles',
|
||||
help='downloads all the available subtitles of the video (currently youtube only)', default=False)
|
||||
video_format.add_option('--srt-lang',
|
||||
video_format.add_option('--sub-lang',
|
||||
action='store', dest='subtitleslang', metavar='LANG',
|
||||
help='language of the closed captions to download (optional) use IETF language tags like \'en\'')
|
||||
help='language of the subtitles to download (optional) use IETF language tags like \'en\'')
|
||||
|
||||
verbosity.add_option('-q', '--quiet',
|
||||
action='store_true', dest='quiet', help='activates quiet mode', default=False)
|
||||
|
Loading…
Reference in New Issue
Block a user