You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

86 lines
3.5 KiB

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