Add the option "--write-auto-sub" to download automatic subtitles from Youtube

Now automatic subtitles are only downloaded if the option is given.
(closes #903)
This commit is contained in:
Jaime Marquínez Ferrándiz 2013-06-25 23:45:16 +02:00
parent 81b42336ad
commit b004821fa9
4 changed files with 14 additions and 10 deletions

View File

@ -100,7 +100,7 @@ class TestYoutubeSubtitles(unittest.TestCase):
self.assertEqual(info_dict, None) self.assertEqual(info_dict, None)
def test_youtube_automatic_captions(self): def test_youtube_automatic_captions(self):
DL = FakeYDL() DL = FakeYDL()
DL.params['writesubtitles'] = True DL.params['writeautomaticsub'] = True
DL.params['subtitleslang'] = 'it' DL.params['subtitleslang'] = 'it'
IE = YoutubeIE(DL) IE = YoutubeIE(DL)
info_dict = IE.extract('8YoUxe5ncPo') info_dict = IE.extract('8YoUxe5ncPo')

View File

@ -72,6 +72,7 @@ class YoutubeDL(object):
writeinfojson: Write the video description to a .info.json file writeinfojson: Write the video description to a .info.json file
writethumbnail: Write the thumbnail image to a file writethumbnail: Write the thumbnail image to a file
writesubtitles: Write the video subtitles to a file writesubtitles: Write the video subtitles to a file
writeautomaticsub: Write the automatic subtitles to a file
allsubtitles: Downloads all the subtitles of the video allsubtitles: Downloads all the subtitles of the video
listsubtitles: Lists all available subtitles for the video listsubtitles: Lists all available subtitles for the video
subtitlesformat: Subtitle format [sbv/srt] (default=srt) subtitlesformat: Subtitle format [sbv/srt] (default=srt)
@ -474,7 +475,7 @@ class YoutubeDL(object):
self.report_error(u'Cannot write description file ' + descfn) self.report_error(u'Cannot write description file ' + descfn)
return return
if self.params.get('writesubtitles', False) and 'subtitles' in info_dict and info_dict['subtitles']: if (self.params.get('writesubtitles', False) or self.params.get('writeautomaticsub')) and 'subtitles' in info_dict and info_dict['subtitles']:
# subtitles download errors are already managed as troubles in relevant IE # subtitles download errors are already managed as troubles in relevant IE
# that way it will silently go on when used with unsupporting IE # that way it will silently go on when used with unsupporting IE
subtitle = info_dict['subtitles'][0] subtitle = info_dict['subtitles'][0]

View File

@ -191,6 +191,9 @@ def parseOpts(overrideArguments=None):
video_format.add_option('--write-sub', '--write-srt', video_format.add_option('--write-sub', '--write-srt',
action='store_true', dest='writesubtitles', action='store_true', dest='writesubtitles',
help='write subtitle file (currently youtube only)', default=False) help='write subtitle file (currently youtube only)', default=False)
video_format.add_option('--write-auto-sub', '--write-automatic-sub',
action='store_true', dest='writeautomaticsub',
help='write automatic subtitle file (currently youtube only)', default=False)
video_format.add_option('--only-sub', video_format.add_option('--only-sub',
action='store_true', dest='skip_download', action='store_true', dest='skip_download',
help='[deprecated] alias of --skip-download', default=False) help='[deprecated] alias of --skip-download', default=False)
@ -537,6 +540,7 @@ def _real_main(argv=None):
'writeinfojson': opts.writeinfojson, 'writeinfojson': opts.writeinfojson,
'writethumbnail': opts.writethumbnail, 'writethumbnail': opts.writethumbnail,
'writesubtitles': opts.writesubtitles, 'writesubtitles': opts.writesubtitles,
'writeautomaticsub': opts.writeautomaticsub,
'allsubtitles': opts.allsubtitles, 'allsubtitles': opts.allsubtitles,
'listsubtitles': opts.listsubtitles, 'listsubtitles': opts.listsubtitles,
'subtitlesformat': opts.subtitlesformat, 'subtitlesformat': opts.subtitlesformat,

View File

@ -454,14 +454,13 @@ class YoutubeIE(InfoExtractor):
if video_subtitles: if video_subtitles:
(sub_error, sub_lang, sub) = video_subtitles[0] (sub_error, sub_lang, sub) = video_subtitles[0]
if sub_error: if sub_error:
# We try with the automatic captions self._downloader.report_warning(sub_error)
video_subtitles = self._request_automatic_caption(video_id, video_webpage)
(sub_error_auto, sub_lang, sub) = video_subtitles[0] if self._downloader.params.get('writeautomaticsub', False):
if sub is not None: video_subtitles = self._request_automatic_caption(video_id, video_webpage)
pass (sub_error, sub_lang, sub) = video_subtitles[0]
else: if sub_error:
# We report the original error self._downloader.report_warning(sub_error)
self._downloader.report_warning(sub_error)
if self._downloader.params.get('allsubtitles', False): if self._downloader.params.get('allsubtitles', False):
video_subtitles = self._extract_all_subtitles(video_id) video_subtitles = self._extract_all_subtitles(video_id)