Skip to content

Instantly share code, notes, and snippets.

@zhangyangjing
Last active October 14, 2016 07:22
Show Gist options
  • Save zhangyangjing/1e935a40e9380330801a9e0ddda05c31 to your computer and use it in GitHub Desktop.
Save zhangyangjing/1e935a40e9380330801a9e0ddda05c31 to your computer and use it in GitHub Desktop.
subtitles_concat
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import os
import sys
if 2 != len(sys.argv):
print 'bad argument'
exit(1)
movie_path = sys.argv[1]
if not os.path.isabs(movie_path):
movie_path = os.path.abspath(movie_path)
movie_directory = os.path.dirname(movie_path)
movie_filename = os.path.basename(movie_path)
movie_name = os.path.splitext(movie_filename)[0]
subtitle_filenames = []
for filename in os.listdir(movie_directory):
if os.path.isfile(filename) \
and filename.startswith(movie_name) \
and (filename.endswith('.srt') or filename.endswith('.ass')):
subtitle_filenames.append(filename)
cmd = 'ffmpeg -i "' + movie_filename + '"'
cnt = 0
cmd_map = ''
cmd_subtitle_meta = ''
for subtitle_filename in subtitle_filenames:
subtitle_name = (subtitle_filename.replace(movie_name + '.', '')
.replace('.srt', '')
.replace('.ass', ''))
cmd += ' -i "' + subtitle_filename + '"'
cmd_map += ' -map ' + str(cnt + 1) + ':0'
cmd_subtitle_meta += ' -metadata:s:s:' + str(cnt) \
+ ' language=' + subtitle_name
cnt += 1
cmd += cmd_subtitle_meta
cmd += cmd_map
cmd += ' -map 0'
cmd += ' -c copy'
cmd += ' out.mkv'
print cmd
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment