#!/usr/bin/python3 import sys import os import subprocess import string import re # the inputfile wheee inputfile = sys.argv[1] # media types - im not actually using this filetypes = {'V_MPEG4/ISO/AVC' : 'h264', 'A_AAC' : 'aac', 'S_TEXT/UTF8' : 'srt'} # run mkvmerge to get the list of tracks in this mkv output = subprocess.Popen(['mkvmerge', '-i', inputfile], stdout=subprocess.PIPE).communicate()[0] output = output.decode() # iterate over each line of the output for line in output.split('\n'): # search for a track list matches = re.search(r'Track ID (\d)\: \w* \(([A-Z_/0-9]*)\)', line) if matches: id = matches.group(1) type = matches.group(2) # check if we have a dts track if type == 'A_DTS': # extract the dts track out of our mkv file extractOutput = subprocess.Popen(['mkvextract', 'tracks', inputfile, id + ':sound.dts'], stdout=subprocess.PIPE).communicate()[0] # reencode audio to aac reencodeOutput = subprocess.Popen(['ffmpeg', '-i', 'sound.dts', '-vn', '-acodec', 'libfaac', '-ac', '2', '-ar', '48000', '-ab', '160k', 'sound.aac'], stdout=subprocess.PIPE).communicate()[0] print(reencodeOutput) # mux it in with the original file to make a brand SHINY NEW FILE OMGZZ newFilename = inputfile[:-4] + '-shiny.mkv' remuxOutput = subprocess.Popen(['mkvmerge', '-o', newFilename, inputfile, 'sound.aac'], stdout=subprocess.PIPE).communicate()[0] # we dont need the aac or dts files anymore - DELETED deleteOutput = subprocess.Popen(['rm', 'sound.aac', 'sound.dts'], stdout=subprocess.PIPE).communicate()[0] # stop iterating over tracks as we are done - VICTORY~~! break