mirror of
https://github.com/yt-dlp/yt-dlp.git
synced 2024-12-14 23:32:33 +00:00
Merge pull request #764 from jaimeMF/subtitles_not_found
Fix crash when subtitles are not found
This commit is contained in:
commit
f33154cd39
2 changed files with 41 additions and 23 deletions
|
@ -485,14 +485,17 @@ class FileDownloader(object):
|
||||||
subtitle = info_dict['subtitles'][0]
|
subtitle = info_dict['subtitles'][0]
|
||||||
(sub_error, sub_lang, sub) = subtitle
|
(sub_error, sub_lang, sub) = subtitle
|
||||||
sub_format = self.params.get('subtitlesformat')
|
sub_format = self.params.get('subtitlesformat')
|
||||||
try:
|
if sub_error:
|
||||||
sub_filename = filename.rsplit('.', 1)[0] + u'.' + sub_lang + u'.' + sub_format
|
self.report_warning("Some error while getting the subtitles")
|
||||||
self.report_writesubtitles(sub_filename)
|
else:
|
||||||
with io.open(encodeFilename(sub_filename), 'w', encoding='utf-8') as subfile:
|
try:
|
||||||
subfile.write(sub)
|
sub_filename = filename.rsplit('.', 1)[0] + u'.' + sub_lang + u'.' + sub_format
|
||||||
except (OSError, IOError):
|
self.report_writesubtitles(sub_filename)
|
||||||
self.report_error(u'Cannot write subtitles file ' + descfn)
|
with io.open(encodeFilename(sub_filename), 'w', encoding='utf-8') as subfile:
|
||||||
return
|
subfile.write(sub)
|
||||||
|
except (OSError, IOError):
|
||||||
|
self.report_error(u'Cannot write subtitles file ' + descfn)
|
||||||
|
return
|
||||||
if self.params.get('onlysubtitles', False):
|
if self.params.get('onlysubtitles', False):
|
||||||
return
|
return
|
||||||
|
|
||||||
|
@ -501,14 +504,17 @@ class FileDownloader(object):
|
||||||
sub_format = self.params.get('subtitlesformat')
|
sub_format = self.params.get('subtitlesformat')
|
||||||
for subtitle in subtitles:
|
for subtitle in subtitles:
|
||||||
(sub_error, sub_lang, sub) = subtitle
|
(sub_error, sub_lang, sub) = subtitle
|
||||||
try:
|
if sub_error:
|
||||||
sub_filename = filename.rsplit('.', 1)[0] + u'.' + sub_lang + u'.' + sub_format
|
self.report_warning("Some error while getting the subtitles")
|
||||||
self.report_writesubtitles(sub_filename)
|
else:
|
||||||
with io.open(encodeFilename(sub_filename), 'w', encoding='utf-8') as subfile:
|
try:
|
||||||
subfile.write(sub)
|
sub_filename = filename.rsplit('.', 1)[0] + u'.' + sub_lang + u'.' + sub_format
|
||||||
except (OSError, IOError):
|
self.report_writesubtitles(sub_filename)
|
||||||
self.trouble(u'ERROR: Cannot write subtitles file ' + descfn)
|
with io.open(encodeFilename(sub_filename), 'w', encoding='utf-8') as subfile:
|
||||||
return
|
subfile.write(sub)
|
||||||
|
except (OSError, IOError):
|
||||||
|
self.trouble(u'ERROR: Cannot write subtitles file ' + descfn)
|
||||||
|
return
|
||||||
if self.params.get('onlysubtitles', False):
|
if self.params.get('onlysubtitles', False):
|
||||||
return
|
return
|
||||||
|
|
||||||
|
|
|
@ -253,11 +253,11 @@ class YoutubeIE(InfoExtractor):
|
||||||
try:
|
try:
|
||||||
sub_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:
|
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)
|
return (u'unable to download video subtitles: %s' % compat_str(err), None)
|
||||||
sub_lang_list = re.findall(r'name="([^"]*)"[^>]+lang_code="([\w\-]+)"', sub_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)
|
sub_lang_list = dict((l[1], l[0]) for l in sub_lang_list)
|
||||||
if not sub_lang_list:
|
if not sub_lang_list:
|
||||||
return (u'WARNING: video doesn\'t have subtitles', None)
|
return (u'video doesn\'t have subtitles', None)
|
||||||
return sub_lang_list
|
return sub_lang_list
|
||||||
|
|
||||||
def _list_available_subtitles(self, video_id):
|
def _list_available_subtitles(self, video_id):
|
||||||
|
@ -265,6 +265,10 @@ class YoutubeIE(InfoExtractor):
|
||||||
self.report_video_subtitles_available(video_id, sub_lang_list)
|
self.report_video_subtitles_available(video_id, sub_lang_list)
|
||||||
|
|
||||||
def _request_subtitle(self, sub_lang, sub_name, video_id, format):
|
def _request_subtitle(self, sub_lang, sub_name, video_id, format):
|
||||||
|
"""
|
||||||
|
Return tuple:
|
||||||
|
(error_message, sub_lang, sub)
|
||||||
|
"""
|
||||||
self.report_video_subtitles_request(video_id, sub_lang, format)
|
self.report_video_subtitles_request(video_id, sub_lang, format)
|
||||||
params = compat_urllib_parse.urlencode({
|
params = compat_urllib_parse.urlencode({
|
||||||
'lang': sub_lang,
|
'lang': sub_lang,
|
||||||
|
@ -276,14 +280,20 @@ class YoutubeIE(InfoExtractor):
|
||||||
try:
|
try:
|
||||||
sub = 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:
|
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)
|
return (u'unable to download video subtitles: %s' % compat_str(err), None, None)
|
||||||
if not sub:
|
if not sub:
|
||||||
return (u'WARNING: Did not fetch video subtitles', None)
|
return (u'Did not fetch video subtitles', None, None)
|
||||||
return (None, sub_lang, sub)
|
return (None, sub_lang, sub)
|
||||||
|
|
||||||
def _extract_subtitle(self, video_id):
|
def _extract_subtitle(self, video_id):
|
||||||
|
"""
|
||||||
|
Return a list with a tuple:
|
||||||
|
[(error_message, sub_lang, sub)]
|
||||||
|
"""
|
||||||
sub_lang_list = self._get_available_subtitles(video_id)
|
sub_lang_list = self._get_available_subtitles(video_id)
|
||||||
sub_format = self._downloader.params.get('subtitlesformat')
|
sub_format = self._downloader.params.get('subtitlesformat')
|
||||||
|
if isinstance(sub_lang_list,tuple): #There was some error, it didn't get the available subtitles
|
||||||
|
return [(sub_lang_list[0], None, None)]
|
||||||
if self._downloader.params.get('subtitleslang', False):
|
if self._downloader.params.get('subtitleslang', False):
|
||||||
sub_lang = self._downloader.params.get('subtitleslang')
|
sub_lang = self._downloader.params.get('subtitleslang')
|
||||||
elif 'en' in sub_lang_list:
|
elif 'en' in sub_lang_list:
|
||||||
|
@ -291,7 +301,7 @@ class YoutubeIE(InfoExtractor):
|
||||||
else:
|
else:
|
||||||
sub_lang = list(sub_lang_list.keys())[0]
|
sub_lang = list(sub_lang_list.keys())[0]
|
||||||
if not sub_lang in sub_lang_list:
|
if not sub_lang in sub_lang_list:
|
||||||
return (u'WARNING: no closed captions found in the specified language "%s"' % sub_lang, None)
|
return [(u'no closed captions found in the specified language "%s"' % sub_lang, None, None)]
|
||||||
|
|
||||||
subtitle = self._request_subtitle(sub_lang, sub_lang_list[sub_lang].encode('utf-8'), video_id, sub_format)
|
subtitle = self._request_subtitle(sub_lang, sub_lang_list[sub_lang].encode('utf-8'), video_id, sub_format)
|
||||||
return [subtitle]
|
return [subtitle]
|
||||||
|
@ -299,6 +309,8 @@ class YoutubeIE(InfoExtractor):
|
||||||
def _extract_all_subtitles(self, video_id):
|
def _extract_all_subtitles(self, video_id):
|
||||||
sub_lang_list = self._get_available_subtitles(video_id)
|
sub_lang_list = self._get_available_subtitles(video_id)
|
||||||
sub_format = self._downloader.params.get('subtitlesformat')
|
sub_format = self._downloader.params.get('subtitlesformat')
|
||||||
|
if isinstance(sub_lang_list,tuple): #There was some error, it didn't get the available subtitles
|
||||||
|
return [(sub_lang_list[0], None, None)]
|
||||||
subtitles = []
|
subtitles = []
|
||||||
for sub_lang in sub_lang_list:
|
for sub_lang in sub_lang_list:
|
||||||
subtitle = self._request_subtitle(sub_lang, sub_lang_list[sub_lang].encode('utf-8'), video_id, sub_format)
|
subtitle = self._request_subtitle(sub_lang, sub_lang_list[sub_lang].encode('utf-8'), video_id, sub_format)
|
||||||
|
@ -532,14 +544,14 @@ class YoutubeIE(InfoExtractor):
|
||||||
if video_subtitles:
|
if video_subtitles:
|
||||||
(sub_error, sub_lang, sub) = video_subtitles[0]
|
(sub_error, sub_lang, sub) = video_subtitles[0]
|
||||||
if sub_error:
|
if sub_error:
|
||||||
self._downloader.trouble(sub_error)
|
self._downloader.report_error(sub_error)
|
||||||
|
|
||||||
if self._downloader.params.get('allsubtitles', False):
|
if self._downloader.params.get('allsubtitles', False):
|
||||||
video_subtitles = self._extract_all_subtitles(video_id)
|
video_subtitles = self._extract_all_subtitles(video_id)
|
||||||
for video_subtitle in video_subtitles:
|
for video_subtitle in video_subtitles:
|
||||||
(sub_error, sub_lang, sub) = video_subtitle
|
(sub_error, sub_lang, sub) = video_subtitle
|
||||||
if sub_error:
|
if sub_error:
|
||||||
self._downloader.trouble(sub_error)
|
self._downloader.report_error(sub_error)
|
||||||
|
|
||||||
if self._downloader.params.get('listsubtitles', False):
|
if self._downloader.params.get('listsubtitles', False):
|
||||||
sub_lang_list = self._list_available_subtitles(video_id)
|
sub_lang_list = self._list_available_subtitles(video_id)
|
||||||
|
|
Loading…
Reference in a new issue