Moving protocol to download subtitles back to the subtitle_info and keep the download logic in YoutubeDl

This commit is contained in:
Sacha Arnoud 2021-09-21 16:31:43 +00:00
parent d0774569c1
commit 5fb593d50a
5 changed files with 14 additions and 15 deletions

View File

@ -1771,7 +1771,7 @@ class YoutubeDL(object):
self.to_stdout(formatSeconds(info_dict['duration']))
print_mandatory('format')
if self.params.get('forcejson', False):
self.to_stdout(json.dumps(info_dict, default=lambda _:'<not serialized>'))
self.to_stdout(json.dumps(info_dict))
def process_info(self, info_dict):
"""Process a single resolved IE result."""
@ -1879,15 +1879,16 @@ class YoutubeDL(object):
except (OSError, IOError):
self.report_error('Cannot write subtitles file ' + sub_filename)
return
elif callable(sub_info.get('downloader')):
sub_info.get('downloader')(self, encodeFilename(sub_filename))
else:
fd = get_suitable_downloader(sub_info, self.params)(self, self.params)
try:
sub_data = ie._request_webpage(
sub_info['url'], info_dict['id'], note=False).read()
with io.open(encodeFilename(sub_filename), 'wb') as subfile:
subfile.write(sub_data)
except (ExtractorError, IOError, OSError, ValueError) as err:
if self.params.get('verbose'):
self.to_screen('[debug] Invoking subtitle downloader on %r' % sub_info.get('url'))
# The FD is supposed to encodeFilename()
if not fd.download(sub_filename, sub_info):
# depending on the FD, it may catch errors and return False, or not
raise DownloadError('Subtitle download failed')
except (compat_urllib_error.URLError, compat_http_client.HTTPException, socket.error, OSError, IOError, YoutubeDLError) as err:
self.report_warning('Unable to download subtitle for "%s": %s' %
(sub_lang, error_to_compat_str(err)))
continue
@ -2076,7 +2077,7 @@ class YoutubeDL(object):
raise
else:
if self.params.get('dump_single_json', False):
self.to_stdout(json.dumps(res, default=lambda _:'<not serialized>'))
self.to_stdout(json.dumps(res))
return self._download_retcode

View File

@ -33,7 +33,8 @@ def get_suitable_downloader(info_dict, params={}):
"""Get the downloader class that can handle the info dict."""
protocol = determine_protocol(info_dict)
info_dict['protocol'] = protocol
print('SACHA> ', protocol)
# if (info_dict.get('start_time') or info_dict.get('end_time')) and not info_dict.get('requested_formats') and FFmpegFD.can_download(info_dict):
# return FFmpegFD

View File

@ -1708,6 +1708,7 @@ class InfoExtractor(object):
subtitles[media['LANGUAGE']] = [{
'url': format_url(media['URI']),
'ext': media.get('SUBFORMAT', 'webtt'),
'protocol': 'm3u8_native',
}]
return
if media_type not in ('VIDEO', 'AUDIO'):

View File

@ -206,10 +206,6 @@ class FranceTVIE(InfoExtractor):
info['title'] += ' - %s' % info['subtitle']
info['title'] = info['title'].strip()
for lang, sts in info['subtitles'].items():
for st in sts:
st['downloader'] = lambda ydl, filename: PROTOCOL_MAP['m3u8_native'](ydl, ydl.params).download(filename, st)
return {
'id': video_id,
'title': self._live_title(info['title']) if is_live else info['title'],

View File

@ -1830,7 +1830,7 @@ def write_json_file(obj, fn):
try:
with tf:
json.dump(obj, tf, default=lambda _:'<not serialized>')
json.dump(obj)
if sys.platform == 'win32':
# Need to remove existing file on Windows, else os.rename raises
# WindowsError or FileExistsError.