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.

79 lines
3.2 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, video_webpage=None):
  14. """ returns {sub_lang: sub} or {} if subtitles not found """
  15. if self._downloader.params.get('writesubtitles', False) or self._downloader.params.get('allsubtitles', False):
  16. available_subs_list = self._get_available_subtitles(video_id)
  17. elif self._downloader.params.get('writeautomaticsub', False):
  18. available_subs_list = self._get_available_automatic_caption(video_id, video_webpage)
  19. else:
  20. return None
  21. if not available_subs_list: # error, it didn't get the available subtitles
  22. return {}
  23. if self._downloader.params.get('allsubtitles', False):
  24. sub_lang_list = available_subs_list
  25. else:
  26. if self._downloader.params.get('subtitleslangs', False):
  27. requested_langs = self._downloader.params.get('subtitleslangs')
  28. elif 'en' in available_subs_list:
  29. requested_langs = ['en']
  30. else:
  31. requested_langs = [list(available_subs_list.keys())[0]]
  32. sub_lang_list = {}
  33. for sub_lang in requested_langs:
  34. if not sub_lang in available_subs_list:
  35. self._downloader.report_warning(u'no closed captions found in the specified language "%s"' % sub_lang)
  36. continue
  37. sub_lang_list[sub_lang] = available_subs_list[sub_lang]
  38. subtitles = {}
  39. for sub_lang, url in sub_lang_list.items():
  40. subtitle = self._request_subtitle_url(sub_lang, url)
  41. if subtitle:
  42. subtitles[sub_lang] = subtitle
  43. return subtitles
  44. def _request_subtitle_url(self, sub_lang, url):
  45. """ makes the http request for the subtitle """
  46. try:
  47. sub = self._download_webpage(url, None, note=False)
  48. except ExtractorError as err:
  49. self._downloader.report_warning(u'unable to download video subtitles for %s: %s' % (sub_lang, compat_str(err)))
  50. return
  51. if not sub:
  52. self._downloader.report_warning(u'Did not fetch video subtitles')
  53. return
  54. return sub
  55. def _get_available_subtitles(self, video_id):
  56. """
  57. returns {sub_lang: url} or {} if not available
  58. Must be redefined by the subclasses
  59. """
  60. pass
  61. def _get_available_automatic_caption(self, video_id, webpage):
  62. """
  63. returns {sub_lang: url} or {} if not available
  64. Must be redefined by the subclasses that support automatic captions,
  65. otherwise it will return {}
  66. """
  67. self._downloader.report_warning(u'Automatic Captions not supported by this server')
  68. return {}