Skip to content

Instantly share code, notes, and snippets.

@aheadley
Created April 2, 2014 18:29
Show Gist options
  • Save aheadley/9940153 to your computer and use it in GitHub Desktop.
Save aheadley/9940153 to your computer and use it in GitHub Desktop.
Simple script to fix the reversed dialogue in HorribleSubs's april fools subs. Correctly handles UTF-8 text and remuxes into a new MKV.
#!/usr/bin/env python3
import re
import sys
import subprocess
import hashlib
import os
DEBUG = False
MKVEXTRACT = 'mkvextract'
MKVINFO = 'mkvinfo'
MKVMERGE = 'mkvmerge'
md5sum = lambda text: hashlib.md5(text.encode('utf-8')).hexdigest()
def get_sub_track_id(filename):
return 2
def extract_subs(filename):
track_id = get_sub_track_id(filename)
new_filename = '%s.ass' % md5sum(filename)
subprocess.call([MKVEXTRACT, 'tracks', filename, '%d:%s' % (track_id, new_filename)])
return new_filename
def merge_subs(source_filename, sub_filename):
new_filename = source_filename + '.fixed'
subprocess.call([MKVMERGE, '-o', new_filename, '-S', source_filename, sub_filename])
#os.unlink(sub_filename)
return new_filename
def reverse_subs(source, dest):
with open(source, 'r', encoding='utf-8') as sf:
with open(dest, 'w', encoding='utf-8') as df:
for line in sf:
if line.startswith('Dialogue:'):
parts = line.strip().split(',', 9)
line = ','.join(parts[:-1] + [parts[-1][::-1] + '\n'])
df.write(line)
df.flush()
return
if __name__ == '__main__':
source_file = sys.argv[1]
sub_file = extract_subs(source_file)
fixed_sub_file = sub_file + '.fixed'
reverse_subs(sub_file, fixed_sub_file)
fixed_file = merge_subs(source_file, fixed_sub_file)
sys.stdout.write('%s => %s' % (source_file, fixed_file))
if not DEBUG:
for fn in [sub_file, fixed_sub_file]:
os.unlink(fn)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment