]>
jfr.im git - yt-dlp.git/blob - youtube_dl/extractor/subtitles.py
1 from .common
import InfoExtractor
9 class SubtitlesInfoExtractor(InfoExtractor
):
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' %
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
)
27 if not available_subs_list
: # error, it didn't get the available subtitles
29 if self
._downloader
.params
.get('allsubtitles', False):
30 sub_lang_list
= available_subs_list
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']
37 requested_langs
= [list(available_subs_list
.keys())[0]]
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
)
44 sub_lang_list
[sub_lang
] = available_subs_list
[sub_lang
]
47 for sub_lang
, url
in sub_lang_list
.items():
48 subtitle
= self
._request
_subtitle
_url
(sub_lang
, url
)
50 subtitles
[sub_lang
] = subtitle
53 def _request_subtitle_url(self
, sub_lang
, url
):
54 """ makes the http request for the subtitle """
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
)))
61 self
._downloader
.report_warning(u
'Did not fetch video subtitles')
65 def _get_available_subtitles(self
, video_id
):
67 returns {sub_lang: url} or {} if not available
68 Must be redefined by the subclasses
72 def _get_available_automatic_caption(self
, video_id
, webpage
):
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 {}
78 self
._downloader
.report_warning(u
'Automatic Captions not supported by this server')