Added new option '--list-subs' to show the available subtitle languages

This commit is contained in:
Ismael Mejia 2013-02-22 04:50:05 +01:00 committed by Filippo Valsorda
parent 9e62bc4439
commit 2a4093eaf3
5 changed files with 33 additions and 8 deletions

View file

@ -39,5 +39,6 @@
"writeinfojson": true, "writeinfojson": true,
"writesubtitles": false, "writesubtitles": false,
"onlysubtitles": false, "onlysubtitles": false,
"allsubtitles": false "allsubtitles": false,
"listssubtitles": false
} }

View file

@ -43,6 +43,7 @@ class TestYoutubeSubtitles(unittest.TestCase):
DL.params['allsubtitles'] = False DL.params['allsubtitles'] = False
DL.params['writesubtitles'] = False DL.params['writesubtitles'] = False
DL.params['subtitlesformat'] = 'srt' DL.params['subtitlesformat'] = 'srt'
DL.params['listsubtitles'] = False
def test_youtube_no_subtitles(self): def test_youtube_no_subtitles(self):
DL = FakeDownloader() DL = FakeDownloader()
DL.params['writesubtitles'] = False DL.params['writesubtitles'] = False
@ -88,6 +89,12 @@ class TestYoutubeSubtitles(unittest.TestCase):
info_dict = IE.extract('QRS8MkLhQmM') info_dict = IE.extract('QRS8MkLhQmM')
sub = info_dict[0]['subtitles'][0] sub = info_dict[0]['subtitles'][0]
self.assertEqual(md5(sub[2]), '13aeaa0c245a8bed9a451cb643e3ad8b') self.assertEqual(md5(sub[2]), '13aeaa0c245a8bed9a451cb643e3ad8b')
def test_youtube_list_subtitles(self):
DL = FakeDownloader()
DL.params['listsubtitles'] = True
IE = YoutubeIE(DL)
info_dict = IE.extract('QRS8MkLhQmM')
self.assertEqual(info_dict, None)
if __name__ == '__main__': if __name__ == '__main__':
unittest.main() unittest.main()

View file

@ -81,6 +81,7 @@ class FileDownloader(object):
writesubtitles: Write the video subtitles to a file writesubtitles: Write the video subtitles to a file
onlysubtitles: Downloads only the subtitles of the video onlysubtitles: Downloads only the subtitles of the video
allsubtitles: Downloads all the subtitles of the video allsubtitles: Downloads all the subtitles of the video
listsubtitles: Lists all available subtitles for the video
subtitlesformat: Subtitle format [sbv/srt] (default=srt) subtitlesformat: Subtitle format [sbv/srt] (default=srt)
subtitleslang: Language of the subtitles to download subtitleslang: Language of the subtitles to download
test: Download only first bytes to test the downloader. test: Download only first bytes to test the downloader.

View file

@ -214,11 +214,16 @@ class YoutubeIE(InfoExtractor):
def report_video_subtitles_download(self, video_id): def report_video_subtitles_download(self, video_id):
"""Report attempt to download video info webpage.""" """Report attempt to download video info webpage."""
self._downloader.to_screen(u'[youtube] %s: Downloading video subtitles' % video_id) self._downloader.to_screen(u'[youtube] %s: Checking available subtitles' % video_id)
def report_video_subtitles_request(self, video_id, lang): def report_video_subtitles_request(self, video_id, sub_lang, format):
"""Report attempt to download video info webpage.""" """Report attempt to download video info webpage."""
self._downloader.to_screen(u'[youtube] %s: Downloading video subtitles for lang: %s' % (video_id,lang)) self._downloader.to_screen(u'[youtube] %s: Downloading video subtitles for %s.%s' % (video_id, sub_lang, format))
def report_video_subtitles_available(self, video_id, sub_lang_list):
"""Report available subtitles."""
sub_lang = ",".join(list(sub_lang_list.keys()))
self._downloader.to_screen(u'[youtube] %s: Available subtitles for video: %s' % (video_id, sub_lang))
def report_information_extraction(self, video_id): def report_information_extraction(self, video_id):
"""Report attempt to extract video information.""" """Report attempt to extract video information."""
@ -233,6 +238,7 @@ class YoutubeIE(InfoExtractor):
self._downloader.to_screen(u'[youtube] RTMP download detected') self._downloader.to_screen(u'[youtube] RTMP download detected')
def _get_available_subtitles(self, video_id): def _get_available_subtitles(self, video_id):
self.report_video_subtitles_download(video_id)
request = compat_urllib_request.Request('http://video.google.com/timedtext?hl=en&type=list&v=%s' % video_id) request = compat_urllib_request.Request('http://video.google.com/timedtext?hl=en&type=list&v=%s' % video_id)
try: try:
sub_list = compat_urllib_request.urlopen(request).read().decode('utf-8') sub_list = compat_urllib_request.urlopen(request).read().decode('utf-8')
@ -241,11 +247,15 @@ class YoutubeIE(InfoExtractor):
sub_lang_list = re.findall(r'name="([^"]*)"[^>]+lang_code="([\w\-]+)"', sub_list) sub_lang_list = re.findall(r'name="([^"]*)"[^>]+lang_code="([\w\-]+)"', sub_list)
sub_lang_list = dict((l[1], l[0]) for l in sub_lang_list) sub_lang_list = dict((l[1], l[0]) for l in sub_lang_list)
if not sub_lang_list: if not sub_lang_list:
return (u'WARNING: video has no closed captions', None) return (u'WARNING: video doesn\'t have download', None)
return sub_lang_list return sub_lang_list
def _list_available_subtitles(self, video_id):
sub_lang_list = self._get_available_subtitles(video_id)
self.report_video_subtitles_available(video_id, sub_lang_list)
def _request_subtitle(self, sub_lang, sub_name, video_id, format): def _request_subtitle(self, sub_lang, sub_name, video_id, format):
self.report_video_subtitles_request(video_id, sub_lang) self.report_video_subtitles_request(video_id, sub_lang, format)
params = compat_urllib_parse.urlencode({ params = compat_urllib_parse.urlencode({
'lang': sub_lang, 'lang': sub_lang,
'name': sub_name, 'name': sub_name,
@ -262,7 +272,6 @@ class YoutubeIE(InfoExtractor):
return (None, sub_lang, sub) return (None, sub_lang, sub)
def _extract_subtitle(self, video_id): def _extract_subtitle(self, video_id):
self.report_video_subtitles_download(video_id)
sub_lang_list = self._get_available_subtitles(video_id) sub_lang_list = self._get_available_subtitles(video_id)
sub_format = self._downloader.params.get('subtitlesformat') sub_format = self._downloader.params.get('subtitlesformat')
if self._downloader.params.get('subtitleslang', False): if self._downloader.params.get('subtitleslang', False):
@ -278,7 +287,6 @@ class YoutubeIE(InfoExtractor):
return [subtitle] return [subtitle]
def _extract_all_subtitles(self, video_id): def _extract_all_subtitles(self, video_id):
self.report_video_subtitles_download(video_id)
sub_lang_list = self._get_available_subtitles(video_id) sub_lang_list = self._get_available_subtitles(video_id)
sub_format = self._downloader.params.get('subtitlesformat') sub_format = self._downloader.params.get('subtitlesformat')
subtitles = [] subtitles = []
@ -523,6 +531,10 @@ class YoutubeIE(InfoExtractor):
if sub_error: if sub_error:
self._downloader.trouble(sub_error) self._downloader.trouble(sub_error)
if self._downloader.params.get('listsubtitles', False):
sub_lang_list = self._list_available_subtitles(video_id)
return
if 'length_seconds' not in video_info: if 'length_seconds' not in video_info:
self._downloader.trouble(u'WARNING: unable to extract video duration') self._downloader.trouble(u'WARNING: unable to extract video duration')
video_duration = '' video_duration = ''

View file

@ -182,6 +182,9 @@ def parseOpts():
video_format.add_option('--all-subs', video_format.add_option('--all-subs',
action='store_true', dest='allsubtitles', action='store_true', dest='allsubtitles',
help='downloads all the available subtitles of the video (currently youtube only)', default=False) help='downloads all the available subtitles of the video (currently youtube only)', default=False)
video_format.add_option('--list-subs',
action='store_true', dest='listsubtitles',
help='lists all available subtitles for the video (currently youtube only)', default=False)
video_format.add_option('--sub-format', video_format.add_option('--sub-format',
action='store', dest='subtitlesformat', metavar='LANG', action='store', dest='subtitlesformat', metavar='LANG',
help='subtitle format [srt/sbv] (default=srt) (currently youtube only)', default='srt') help='subtitle format [srt/sbv] (default=srt) (currently youtube only)', default='srt')
@ -461,6 +464,7 @@ def _real_main():
'writesubtitles': opts.writesubtitles, 'writesubtitles': opts.writesubtitles,
'onlysubtitles': opts.onlysubtitles, 'onlysubtitles': opts.onlysubtitles,
'allsubtitles': opts.allsubtitles, 'allsubtitles': opts.allsubtitles,
'listsubtitles': opts.listsubtitles,
'subtitlesformat': opts.subtitlesformat, 'subtitlesformat': opts.subtitlesformat,
'subtitleslang': opts.subtitleslang, 'subtitleslang': opts.subtitleslang,
'matchtitle': decodeOption(opts.matchtitle), 'matchtitle': decodeOption(opts.matchtitle),