Merge pull request #12909 from remitamine/raw-sub

[YoutubeDL] write raw subtitle files
This commit is contained in:
Yen Chi Hsuan 2017-09-13 17:36:40 +08:00 committed by GitHub
commit 757984af90

View File

@ -1763,29 +1763,30 @@ def ensure_dir_exists(path):
ie = self.get_info_extractor(info_dict['extractor_key']) ie = self.get_info_extractor(info_dict['extractor_key'])
for sub_lang, sub_info in subtitles.items(): for sub_lang, sub_info in subtitles.items():
sub_format = sub_info['ext'] sub_format = sub_info['ext']
if sub_info.get('data') is not None: sub_filename = subtitles_filename(filename, sub_lang, sub_format)
sub_data = sub_info['data'] if self.params.get('nooverwrites', False) and os.path.exists(encodeFilename(sub_filename)):
self.to_screen('[info] Video subtitle %s.%s is already present' % (sub_lang, sub_format))
else: else:
try: self.to_screen('[info] Writing video subtitles to: ' + sub_filename)
sub_data = ie._download_webpage( if sub_info.get('data') is not None:
sub_info['url'], info_dict['id'], note=False) try:
except ExtractorError as err: # Use newline='' to prevent conversion of newline characters
self.report_warning('Unable to download subtitle for "%s": %s' % # See https://github.com/rg3/youtube-dl/issues/10268
(sub_lang, error_to_compat_str(err.cause))) with io.open(encodeFilename(sub_filename), 'w', encoding='utf-8', newline='') as subfile:
continue subfile.write(sub_info['data'])
try: except (OSError, IOError):
sub_filename = subtitles_filename(filename, sub_lang, sub_format) self.report_error('Cannot write subtitles file ' + sub_filename)
if self.params.get('nooverwrites', False) and os.path.exists(encodeFilename(sub_filename)): return
self.to_screen('[info] Video subtitle %s.%s is already_present' % (sub_lang, sub_format))
else: else:
self.to_screen('[info] Writing video subtitles to: ' + sub_filename) try:
# Use newline='' to prevent conversion of newline characters sub_data = ie._request_webpage(
# See https://github.com/rg3/youtube-dl/issues/10268 sub_info['url'], info_dict['id'], note=False).read()
with io.open(encodeFilename(sub_filename), 'w', encoding='utf-8', newline='') as subfile: with io.open(encodeFilename(sub_filename), 'wb') as subfile:
subfile.write(sub_data) subfile.write(sub_data)
except (OSError, IOError): except (ExtractorError, IOError, OSError, ValueError) as err:
self.report_error('Cannot write subtitles file ' + sub_filename) self.report_warning('Unable to download subtitle for "%s": %s' %
return (sub_lang, error_to_compat_str(err)))
continue
if self.params.get('writeinfojson', False): if self.params.get('writeinfojson', False):
infofn = replace_extension(filename, 'info.json', info_dict.get('ext')) infofn = replace_extension(filename, 'info.json', info_dict.get('ext'))