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