1
1
mirror of https://github.com/ytdl-org/youtube-dl synced 2024-12-25 06:25:51 +01:00

Added new option '--only-srt' to download only the subtitles of a video

Improved option '--srt-lang'
 - it shows the argument in case of missing subtitles
 - added language suffix for non-english languages (e.g. video.it.srt)
This commit is contained in:
Ismael Mejia 2013-02-21 22:12:36 +01:00
parent 2e5d60b7db
commit cdb130b09a
4 changed files with 22 additions and 1 deletions

View File

@ -53,5 +53,12 @@ class TestYoutubeSubtitles(unittest.TestCase):
info_dict = IE.extract('QRS8MkLhQmM') info_dict = IE.extract('QRS8MkLhQmM')
self.assertEqual(md5(info_dict[0]['subtitles']), '164a51f16f260476a05b50fe4c2f161d') self.assertEqual(md5(info_dict[0]['subtitles']), '164a51f16f260476a05b50fe4c2f161d')
def test_youtube_onlysubtitles(self):
DL = FakeDownloader()
DL.params['onlysubtitles'] = True
IE = YoutubeIE(DL)
info_dict = IE.extract('QRS8MkLhQmM')
self.assertEqual(md5(info_dict[0]['subtitles']), '4cd9278a35ba2305f47354ee13472260')
if __name__ == '__main__': if __name__ == '__main__':
unittest.main() unittest.main()

View File

@ -79,6 +79,7 @@ class FileDownloader(object):
writedescription: Write the video description to a .description file writedescription: Write the video description to a .description file
writeinfojson: Write the video description to a .info.json file writeinfojson: Write the video description to a .info.json file
writesubtitles: Write the video subtitles to a .srt file writesubtitles: Write the video subtitles to a .srt file
onlysubtitles: Downloads only the subtitles of the video
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.
keepvideo: Keep the video file after post-processing keepvideo: Keep the video file after post-processing
@ -443,9 +444,13 @@ class FileDownloader(object):
# that way it will silently go on when used with unsupporting IE # that way it will silently go on when used with unsupporting IE
try: try:
srtfn = filename.rsplit('.', 1)[0] + u'.srt' srtfn = filename.rsplit('.', 1)[0] + u'.srt'
if self.params.get('subtitleslang', False):
srtfn = filename.rsplit('.', 1)[0] + u'.' + self.params['subtitleslang'] + u'.srt'
self.report_writesubtitles(srtfn) self.report_writesubtitles(srtfn)
with io.open(encodeFilename(srtfn), 'w', encoding='utf-8') as srtfile: with io.open(encodeFilename(srtfn), 'w', encoding='utf-8') as srtfile:
srtfile.write(info_dict['subtitles']) srtfile.write(info_dict['subtitles'])
if self.params.get('onlysubtitles', False):
return
except (OSError, IOError): except (OSError, IOError):
self.trouble(u'ERROR: Cannot write subtitles file ' + descfn) self.trouble(u'ERROR: Cannot write subtitles file ' + descfn)
return return

View File

@ -228,6 +228,7 @@ class YoutubeIE(InfoExtractor):
"""Indicate the download will use the RTMP protocol.""" """Indicate the download will use the RTMP protocol."""
self._downloader.to_screen(u'[youtube] RTMP download detected') self._downloader.to_screen(u'[youtube] RTMP download detected')
def _extract_subtitles(self, video_id): def _extract_subtitles(self, video_id):
self.report_video_subtitles_download(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)
@ -246,7 +247,7 @@ class YoutubeIE(InfoExtractor):
else: else:
srt_lang = list(srt_lang_list.keys())[0] srt_lang = list(srt_lang_list.keys())[0]
if not srt_lang in srt_lang_list: if not srt_lang in srt_lang_list:
return (u'WARNING: no closed captions found in the specified language', None) return (u'WARNING: no closed captions found in the specified language "%s"' % srt_lang, None)
params = compat_urllib_parse.urlencode({ params = compat_urllib_parse.urlencode({
'lang': srt_lang, 'lang': srt_lang,
'name': srt_lang_list[srt_lang].encode('utf-8'), 'name': srt_lang_list[srt_lang].encode('utf-8'),
@ -483,6 +484,10 @@ class YoutubeIE(InfoExtractor):
# closed captions # closed captions
video_subtitles = None video_subtitles = None
if self._downloader.params.get('subtitleslang', False):
self._downloader.params['writesubtitles'] = True
if self._downloader.params.get('onlysubtitles', False):
self._downloader.params['writesubtitles'] = True
if self._downloader.params.get('writesubtitles', False): if self._downloader.params.get('writesubtitles', False):
(srt_error, video_subtitles) = self._extract_subtitles(video_id) (srt_error, video_subtitles) = self._extract_subtitles(video_id)
if srt_error: if srt_error:

View File

@ -176,6 +176,9 @@ def parseOpts():
video_format.add_option('--write-srt', video_format.add_option('--write-srt',
action='store_true', dest='writesubtitles', action='store_true', dest='writesubtitles',
help='write video closed captions to a .srt file (currently youtube only)', default=False) help='write video closed captions to a .srt file (currently youtube only)', default=False)
video_format.add_option('--only-srt',
action='store_true', dest='onlysubtitles',
help='downloads only the subtitles of the video (currently youtube only)', default=False)
video_format.add_option('--srt-lang', video_format.add_option('--srt-lang',
action='store', dest='subtitleslang', metavar='LANG', action='store', dest='subtitleslang', metavar='LANG',
help='language of the closed captions to download (optional) use IETF language tags like \'en\'') help='language of the closed captions to download (optional) use IETF language tags like \'en\'')
@ -450,6 +453,7 @@ def _real_main():
'writedescription': opts.writedescription, 'writedescription': opts.writedescription,
'writeinfojson': opts.writeinfojson, 'writeinfojson': opts.writeinfojson,
'writesubtitles': opts.writesubtitles, 'writesubtitles': opts.writesubtitles,
'onlysubtitles': opts.onlysubtitles,
'subtitleslang': opts.subtitleslang, 'subtitleslang': opts.subtitleslang,
'matchtitle': decodeOption(opts.matchtitle), 'matchtitle': decodeOption(opts.matchtitle),
'rejecttitle': decodeOption(opts.rejecttitle), 'rejecttitle': decodeOption(opts.rejecttitle),