Added new option '--sub-format' to choose the format of the subtitles to downloade (defaut=srt)

This commit is contained in:
Ismael Mejia 2013-02-22 03:53:54 +01:00 committed by Filippo Valsorda
parent 553d097442
commit 9e62bc4439
5 changed files with 26 additions and 9 deletions

View file

@ -29,6 +29,7 @@
"simulate": false, "simulate": false,
"skip_download": false, "skip_download": false,
"subtitleslang": null, "subtitleslang": null,
"subtitlesformat": "srt",
"test": true, "test": true,
"updatetime": true, "updatetime": true,
"usenetrc": false, "usenetrc": false,

View file

@ -42,7 +42,7 @@ def setUp(self):
DL = FakeDownloader() DL = FakeDownloader()
DL.params['allsubtitles'] = False DL.params['allsubtitles'] = False
DL.params['writesubtitles'] = False DL.params['writesubtitles'] = False
DL.params['subtitlesformat'] = 'srt'
def test_youtube_no_subtitles(self): def test_youtube_no_subtitles(self):
DL = FakeDownloader() DL = FakeDownloader()
DL.params['writesubtitles'] = False DL.params['writesubtitles'] = False
@ -80,6 +80,14 @@ def test_youtube_allsubtitles(self):
info_dict = IE.extract('QRS8MkLhQmM') info_dict = IE.extract('QRS8MkLhQmM')
subtitles = info_dict[0]['subtitles'] subtitles = info_dict[0]['subtitles']
self.assertEqual(len(subtitles), 12) 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__': if __name__ == '__main__':
unittest.main() unittest.main()

View file

@ -78,9 +78,10 @@ class FileDownloader(object):
updatetime: Use the Last-modified header to set output file timestamps. updatetime: Use the Last-modified header to set output file timestamps.
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 file (default=srt) 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
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.
keepvideo: Keep the video file after post-processing keepvideo: Keep the video file after post-processing
@ -445,8 +446,9 @@ def process_info(self, info_dict):
# 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]
(sub_error, sub_lang, sub) = subtitle (sub_error, sub_lang, sub) = subtitle
sub_format = self.params.get('subtitlesformat')
try: try:
sub_filename = filename.rsplit('.', 1)[0] + u'.' + sub_lang + u'.srt' sub_filename = filename.rsplit('.', 1)[0] + u'.' + sub_lang + u'.' + sub_format
self.report_writesubtitles(sub_filename) self.report_writesubtitles(sub_filename)
with io.open(encodeFilename(sub_filename), 'w', encoding='utf-8') as subfile: with io.open(encodeFilename(sub_filename), 'w', encoding='utf-8') as subfile:
subfile.write(sub) subfile.write(sub)
@ -458,10 +460,11 @@ def process_info(self, info_dict):
if self.params.get('allsubtitles', False) and 'subtitles' in info_dict and info_dict['subtitles']: if self.params.get('allsubtitles', False) and 'subtitles' in info_dict and info_dict['subtitles']:
subtitles = info_dict['subtitles'] subtitles = info_dict['subtitles']
sub_format = self.params.get('subtitlesformat')
for subtitle in subtitles: for subtitle in subtitles:
(sub_error, sub_lang, sub) = subtitle (sub_error, sub_lang, sub) = subtitle
try: try:
sub_filename = filename.rsplit('.', 1)[0] + u'.' + sub_lang + u'.srt' sub_filename = filename.rsplit('.', 1)[0] + u'.' + sub_lang + u'.' + sub_format
self.report_writesubtitles(sub_filename) self.report_writesubtitles(sub_filename)
with io.open(encodeFilename(sub_filename), 'w', encoding='utf-8') as subfile: with io.open(encodeFilename(sub_filename), 'w', encoding='utf-8') as subfile:
subfile.write(sub) subfile.write(sub)

View file

@ -244,7 +244,7 @@ def _get_available_subtitles(self, video_id):
return (u'WARNING: video has no closed captions', None) return (u'WARNING: video has no closed captions', None)
return sub_lang_list 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) self.report_video_subtitles_request(video_id, sub_lang)
params = compat_urllib_parse.urlencode({ params = compat_urllib_parse.urlencode({
'lang': sub_lang, 'lang': sub_lang,
@ -264,7 +264,7 @@ def _request_subtitle(self, sub_lang, sub_name, video_id, format = 'srt'):
def _extract_subtitle(self, video_id): def _extract_subtitle(self, video_id):
self.report_video_subtitles_download(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')
if self._downloader.params.get('subtitleslang', False): if self._downloader.params.get('subtitleslang', False):
sub_lang = self._downloader.params.get('subtitleslang') sub_lang = self._downloader.params.get('subtitleslang')
elif 'en' in sub_lang_list: elif 'en' in sub_lang_list:
@ -274,15 +274,16 @@ def _extract_subtitle(self, video_id):
if not sub_lang in sub_lang_list: if not sub_lang in sub_lang_list:
return (u'WARNING: no closed captions found in the specified language "%s"' % sub_lang, None) 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] return [subtitle]
def _extract_all_subtitles(self, video_id): def _extract_all_subtitles(self, video_id):
self.report_video_subtitles_download(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')
subtitles = [] subtitles = []
for sub_lang in sub_lang_list: 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) subtitles.append(subtitle)
return subtitles return subtitles
@ -505,7 +506,7 @@ def _real_extract(self, url):
else: else:
video_description = '' video_description = ''
# closed captions # subtitles
video_subtitles = None video_subtitles = None
if self._downloader.params.get('writesubtitles', False): if self._downloader.params.get('writesubtitles', False):

View file

@ -182,6 +182,9 @@ def _find_term_columns():
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('--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', video_format.add_option('--sub-lang',
action='store', dest='subtitleslang', metavar='LANG', action='store', dest='subtitleslang', metavar='LANG',
help='language of the subtitles to download (optional) use IETF language tags like \'en\'') help='language of the subtitles to download (optional) use IETF language tags like \'en\'')
@ -458,6 +461,7 @@ def _real_main():
'writesubtitles': opts.writesubtitles, 'writesubtitles': opts.writesubtitles,
'onlysubtitles': opts.onlysubtitles, 'onlysubtitles': opts.onlysubtitles,
'allsubtitles': opts.allsubtitles, 'allsubtitles': opts.allsubtitles,
'subtitlesformat': opts.subtitlesformat,
'subtitleslang': opts.subtitleslang, 'subtitleslang': opts.subtitleslang,
'matchtitle': decodeOption(opts.matchtitle), 'matchtitle': decodeOption(opts.matchtitle),
'rejecttitle': decodeOption(opts.rejecttitle), 'rejecttitle': decodeOption(opts.rejecttitle),