Skip to content

Instantly share code, notes, and snippets.

@majora2007
Created June 29, 2020 16:30
Show Gist options
  • Save majora2007/16b138bf38385dcd19eed36c490ba905 to your computer and use it in GitHub Desktop.
Save majora2007/16b138bf38385dcd19eed36c490ba905 to your computer and use it in GitHub Desktop.
Mux ass and mkv files together. Useful for DemonHiro anime releases.
import os
import subprocess
import os
import sys
import argparse
from pathlib import Path # Move to Parse
in_dir = os.path.abspath('.')
# set this to the path for mkvmerge
MKVMERGE = "C:/Program Files/MKVToolNix/mkvmerge.exe"
class MuxPair(object):
stem = ''
mkv = ''
sub = ''
def __init__(self, stem):
self.stem = stem
self.mkv = ''
self.sub = ''
def __str__(self):
return self.stem
def mux_file(output_path, fileset):
""" Calls mkvmerge with ass and mkv in fileset and the output_path """
# build command line
cmd = [MKVMERGE, "-o", output_path, fileset.mkv, fileset.sub]
# get mkv info
mkvmerge = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
stdout, stderr = mkvmerge.communicate()
if mkvmerge.returncode != 0:
print >> sys.stderr, "mkvmerge failed to mux", output_path
return
print(stdout)
def conatins_muxpair(arr, stem):
for p in arr:
if not p == None and p.stem == stem:
return p
return None
if __name__ == '__main__':
filesets = [] # List of MuxPairs
for root, dirs, files in os.walk(in_dir):
for file in files:
if file.endswith('mkv'):
path = os.path.join(root, file)
p = Path(path)
print('File: {0}'.format(path))
muxpair = MuxPair(p.stem)
print(muxpair)
muxpair.mkv = path
filesets.append(muxpair)
for root, dirs, files in os.walk(in_dir):
for file in files:
if file.endswith('ass'):
path = os.path.join(root, file)
p = Path(path)
muxpair = conatins_muxpair(filesets, p.stem)
muxpair.sub = path
# Remove stems without sub filled out
pairs = []
for pair in filesets:
if not pair.sub == '' and not pair.mkv == '':
pairs.append(pair)
for pair in pairs:
new_path = pair.stem + ' (1).mkv'
print('Muxing {} -> {}'.format(pair.stem, new_path))
mux_file(new_path, pair)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment