A feature-rich command-line audio/video downloader
Added new option '--sub-format' to choose the format of the subtitles to downloade (defaut=srt)
Ismael Mejia9e62bc443996
diff --git a/test/parameters.json b/test/parameters.jsonindex 0d4bd644cf..750b1c96e1 100644--- a/test/parameters.json+++ b/test/parameters.json@@ -29,6 +29,7 @@"simulate": false,"skip_download": false,"subtitleslang": null,+ "subtitlesformat": "srt","test": true,"updatetime": true,"usenetrc": false,diff --git a/test/test_youtube_subtitles.py b/test/test_youtube_subtitles.pyindex 3b5a53fca1..94adc45552 100644--- a/test/test_youtube_subtitles.py+++ b/test/test_youtube_subtitles.py@@ -42,7 +42,7 @@ class TestYoutubeSubtitles(unittest.TestCase):DL = FakeDownloader()DL.params['allsubtitles'] = FalseDL.params['writesubtitles'] = False-+ DL.params['subtitlesformat'] = 'srt'def test_youtube_no_subtitles(self):DL = FakeDownloader()DL.params['writesubtitles'] = False@@ -80,6 +80,14 @@ class TestYoutubeSubtitles(unittest.TestCase):info_dict = IE.extract('QRS8MkLhQmM')subtitles = info_dict[0]['subtitles']self.assertEqual(len(subtitles), 12)+ def test_youtube_subtitles_format(self):+ DL = FakeDownloader()+ DL.params['writesubtitles'] = True+ DL.params['subtitlesformat'] = 'sbv'+ IE = YoutubeIE(DL)+ info_dict = IE.extract('QRS8MkLhQmM')+ sub = info_dict[0]['subtitles'][0]+ self.assertEqual(md5(sub[2]), '13aeaa0c245a8bed9a451cb643e3ad8b')if __name__ == '__main__':unittest.main()diff --git a/youtube_dl/FileDownloader.py b/youtube_dl/FileDownloader.pyindex 4549dd4648..a041e12199 100644--- a/youtube_dl/FileDownloader.py+++ b/youtube_dl/FileDownloader.py@@ -78,9 +78,10 @@ class FileDownloader(object):updatetime: Use the Last-modified header to set output file timestamps.writedescription: Write the video description to a .description filewriteinfojson: Write the video description to a .info.json file- writesubtitles: Write the video subtitles to a file (default=srt)+ writesubtitles: Write the video subtitles to a fileonlysubtitles: Downloads only the subtitles of the videoallsubtitles: Downloads all the subtitles of the video+ subtitlesformat: Subtitle format [sbv/srt] (default=srt)subtitleslang: Language of the subtitles to downloadtest: Download only first bytes to test the downloader.keepvideo: Keep the video file after post-processing@@ -445,8 +446,9 @@ class FileDownloader(object):# that way it will silently go on when used with unsupporting IEsubtitle = info_dict['subtitles'][0](sub_error, sub_lang, sub) = subtitle+ sub_format = self.params.get('subtitlesformat')try:- sub_filename = filename.rsplit('.', 1)[0] + u'.' + sub_lang + u'.srt'+ sub_filename = filename.rsplit('.', 1)[0] + u'.' + sub_lang + u'.' + sub_formatself.report_writesubtitles(sub_filename)with io.open(encodeFilename(sub_filename), 'w', encoding='utf-8') as subfile:subfile.write(sub)@@ -458,10 +460,11 @@ class FileDownloader(object):if self.params.get('allsubtitles', False) and 'subtitles' in info_dict and info_dict['subtitles']:subtitles = info_dict['subtitles']+ sub_format = self.params.get('subtitlesformat')for subtitle in subtitles:(sub_error, sub_lang, sub) = subtitletry:- sub_filename = filename.rsplit('.', 1)[0] + u'.' + sub_lang + u'.srt'+ sub_filename = filename.rsplit('.', 1)[0] + u'.' + sub_lang + u'.' + sub_formatself.report_writesubtitles(sub_filename)with io.open(encodeFilename(sub_filename), 'w', encoding='utf-8') as subfile:subfile.write(sub)diff --git a/youtube_dl/InfoExtractors.py b/youtube_dl/InfoExtractors.pyindex e078bb083f..62522bb6cf 100755--- a/youtube_dl/InfoExtractors.py+++ b/youtube_dl/InfoExtractors.py@@ -244,7 +244,7 @@ class YoutubeIE(InfoExtractor):return (u'WARNING: video has no closed captions', None)return sub_lang_list- def _request_subtitle(self, sub_lang, sub_name, video_id, format = 'srt'):+ def _request_subtitle(self, sub_lang, sub_name, video_id, format):self.report_video_subtitles_request(video_id, sub_lang)params = compat_urllib_parse.urlencode({'lang': sub_lang,@@ -264,7 +264,7 @@ class YoutubeIE(InfoExtractor):def _extract_subtitle(self, video_id):self.report_video_subtitles_download(video_id)sub_lang_list = self._get_available_subtitles(video_id)-+ sub_format = self._downloader.params.get('subtitlesformat')if self._downloader.params.get('subtitleslang', False):sub_lang = self._downloader.params.get('subtitleslang')elif 'en' in sub_lang_list:@@ -274,15 +274,16 @@ class YoutubeIE(InfoExtractor):if not sub_lang in sub_lang_list:return (u'WARNING: no closed captions found in the specified language "%s"' % sub_lang, None)- subtitle = self._request_subtitle(sub_lang, sub_lang_list[sub_lang].encode('utf-8'), video_id)+ subtitle = self._request_subtitle(sub_lang, sub_lang_list[sub_lang].encode('utf-8'), video_id, sub_format)return [subtitle]def _extract_all_subtitles(self, video_id):self.report_video_subtitles_download(video_id)sub_lang_list = self._get_available_subtitles(video_id)+ sub_format = self._downloader.params.get('subtitlesformat')subtitles = []for sub_lang in sub_lang_list:- subtitle = self._request_subtitle(sub_lang, sub_lang_list[sub_lang].encode('utf-8'), video_id)+ subtitle = self._request_subtitle(sub_lang, sub_lang_list[sub_lang].encode('utf-8'), video_id, sub_format)subtitles.append(subtitle)return subtitles@@ -505,7 +506,7 @@ class YoutubeIE(InfoExtractor):else:video_description = ''- # closed captions+ # subtitlesvideo_subtitles = Noneif self._downloader.params.get('writesubtitles', False):diff --git a/youtube_dl/__init__.py b/youtube_dl/__init__.pyindex 495b5ac41f..914d030a37 100644--- a/youtube_dl/__init__.py+++ b/youtube_dl/__init__.py@@ -182,6 +182,9 @@ def parseOpts():video_format.add_option('--all-subs',action='store_true', dest='allsubtitles',help='downloads all the available subtitles of the video (currently youtube only)', default=False)+ video_format.add_option('--sub-format',+ action='store', dest='subtitlesformat', metavar='LANG',+ help='subtitle format [srt/sbv] (default=srt) (currently youtube only)', default='srt')video_format.add_option('--sub-lang',action='store', dest='subtitleslang', metavar='LANG',help='language of the subtitles to download (optional) use IETF language tags like \'en\'')@@ -458,6 +461,7 @@ def _real_main():'writesubtitles': opts.writesubtitles,'onlysubtitles': opts.onlysubtitles,'allsubtitles': opts.allsubtitles,+ 'subtitlesformat': opts.subtitlesformat,'subtitleslang': opts.subtitleslang,'matchtitle': decodeOption(opts.matchtitle),'rejecttitle': decodeOption(opts.rejecttitle),