Last active
August 29, 2015 14:12
-
-
Save jeremija/f48f9a3e2dfa8d0cf0f8 to your computer and use it in GitHub Desktop.
muxtitles
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env python3 | |
| # | |
| # Uses subdl to download video subtitles and muxes it to a new mkv video. The only requirement is for the video is to | |
| # be mkv-compatible. | |
| # | |
| # You can find the subdl here: https://github.com/akexakex/subdl/blob/master/subdl | |
| # | |
| # If you use xfce4/thunar, custom actions can be added for *.mp4 and *.mkv video files: | |
| # | |
| # xfce4-terminal --hold --execute /usr/local/bin/muxtitles %F | |
| # xfce4-terminal --hold --execute /usr/local/bin/muxtitles --interactive %F | |
| # | |
| from subprocess import check_call | |
| from uuid import uuid4 | |
| import os | |
| import argparse | |
| import ntpath | |
| parser = argparse.ArgumentParser(description='Downloads subtitles for mkv movies and muxes it') | |
| parser.add_argument('-i', '--interactive', dest='interactive', action='store_true', default=False, help='Ask for subtitle choice') | |
| parser.add_argument('-d', '--dry-run', dest='dry_run', action='store_true', default=False, help='Skips the mux part') | |
| parser.add_argument('files', nargs=argparse.REMAINDER) | |
| args = parser.parse_args() | |
| interactive = args.interactive | |
| dry_run = args.dry_run | |
| #files = [x for x in args.files if x is not None] | |
| files = args.files | |
| subtitles = {} | |
| if len(files) == 0: | |
| print('Error: no files specified') | |
| exit(1) | |
| def mux(file, subtitle): | |
| new_file = "{}.subtitle.mkv".format(file) | |
| check_call(['mkvmerge', file, subtitle, '-o', new_file]) | |
| return new_file | |
| def download_subtitle(file, interactive=False): | |
| subtitle = '/tmp/{}.srt'.format(uuid4()) | |
| subdl_command = ['subdl', '--lang=eng', '--output={}'.format(subtitle)] | |
| if interactive: | |
| subdl_command.append('--interactive') | |
| subdl_command.append(file) | |
| check_call(subdl_command) | |
| return subtitle | |
| for file in args.files: | |
| print('downloading subtitle for:', ntpath.basename(file)) | |
| subtitle = download_subtitle(file, interactive=interactive) | |
| subtitles[file] = subtitle | |
| for file in args.files: | |
| print('muxing subtitle with file:', ntpath.basename(file)) | |
| subtitle = subtitles[file] | |
| if not dry_run: | |
| new_file = mux(file, subtitle) | |
| print('wrote', new_file) | |
| else: | |
| print('skipping mux because of --dry-run flag') | |
| os.remove(subtitle) | |
| print('-- done --') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
huh?