]> jfr.im git - yt-dlp.git/blob - youtube_dl/extractor/subtitles.py
[subtitles] fixed multiple subtitles language separated by comma after merge
[yt-dlp.git] / youtube_dl / extractor / subtitles.py
1 import socket
2
3 from .common import InfoExtractor
4
5 from ..utils import (
6 compat_http_client,
7 compat_urllib_error,
8 compat_urllib_request,
9 compat_str,
10 )
11
12
13 class SubtitlesIE(InfoExtractor):
14
15 def _list_available_subtitles(self, video_id):
16 """ outputs the available subtitles for the video """
17 sub_lang_list = self._get_available_subtitles(video_id)
18 sub_lang = ",".join(list(sub_lang_list.keys()))
19 self.to_screen(u'%s: Available subtitles for video: %s' %
20 (video_id, sub_lang))
21
22 def _extract_subtitles(self, video_id):
23 """ returns {sub_lang: sub} or {} if subtitles not found """
24 available_subs_list = self._get_available_subtitles(video_id)
25 if not available_subs_list: # error, it didn't get the available subtitles
26 return {}
27 if self._downloader.params.get('allsubtitles', False):
28 sub_lang_list = available_subs_list
29 else:
30 if self._downloader.params.get('writesubtitles', False):
31 if self._downloader.params.get('subtitleslangs', False):
32 requested_langs = self._downloader.params.get('subtitleslangs')
33 elif 'en' in available_subs_list:
34 requested_langs = ['en']
35 else:
36 requested_langs = [list(available_subs_list.keys())[0]]
37
38 sub_lang_list = {}
39 for sub_lang in requested_langs:
40 if not sub_lang in available_subs_list:
41 self._downloader.report_warning(u'no closed captions found in the specified language "%s"' % sub_lang)
42 continue
43 sub_lang_list[sub_lang] = available_subs_list[sub_lang]
44
45 subtitles = {}
46 for sub_lang, url in sub_lang_list.items():
47 subtitle = self._request_subtitle_url(sub_lang, url)
48 if subtitle:
49 subtitles[sub_lang] = subtitle
50 return subtitles
51
52 def _request_subtitle_url(self, sub_lang, url):
53 """ makes the http request for the subtitle """
54 try:
55 sub = compat_urllib_request.urlopen(url).read().decode('utf-8')
56 except (compat_urllib_error.URLError, compat_http_client.HTTPException, socket.error) as err:
57 self._downloader.report_warning(u'unable to download video subtitles for %s: %s' % (sub_lang, compat_str(err)))
58 return
59 if not sub:
60 self._downloader.report_warning(u'Did not fetch video subtitles')
61 return
62 return sub
63
64 def _get_available_subtitles(self, video_id):
65 """ returns {sub_lang: url} or {} if not available """
66 """ Must be redefined by the subclasses """
67 pass
68
69 def _request_automatic_caption(self, video_id, webpage):
70 """ returns {sub_lang: sub} or {} if not available """
71 """ Must be redefined by the subclasses """
72 pass
73
74
75 class NoAutoSubtitlesIE(SubtitlesIE):
76 """ A subtitle class for the servers that don't support auto-captions"""
77
78 def _request_automatic_caption(self, video_id, webpage):
79 self._downloader.report_warning(u'Automatic Captions not supported by this server')
80 return {}