Created
July 2, 2012 16:48
-
-
Save garlandkr/3034205 to your computer and use it in GitHub Desktop.
This file contains 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
import re | |
import sys | |
import argparse | |
import subprocess | |
import xml.etree.cElementTree as ET | |
def cmdline(command): | |
""" Run commandline and return output """ | |
process = subprocess.Popen(command, stdout=subprocess.PIPE).communicate()[0] | |
return process | |
def get_args(): | |
parser = argparse.ArgumentParser() | |
parser.add_argument("--retag", dest="retag", required=True, | |
help="Hostname as described in the snapshot, ex: author01-prod, snap-cae91eb7") | |
return parser.parse_args() | |
def run(): | |
try: | |
args = get_args() | |
proc = cmdline(["./mediainfo", args.retag, "--Output=XML"]) | |
xmltree = "" | |
for line in iter(proc.readline, ''): | |
xmltree += line | |
tree = ET.XML(xmltree) | |
videoelement = tree.find(".//track[@type='Video']") | |
audioelement = tree.find(".//track[@type='Audio']") | |
framerate = videoelement.findtext("./Frame_rate").replace(' fps','') | |
videoformat = videoelement.findtext("./Format") | |
audioformat = audioelement.findtext("./Format") | |
videotrack = videoelement.findtext("./ID") | |
audiotrack = audioelement.findtext("./ID") | |
proc = cmdline(["./mkvextract" ,"tracks" ,args.retag, | |
"{0}:{1}".format(videotrack, "video.h264" if (videoformat == "AVC") else "video.h265"), | |
"{0}:{1}".format(audiotrack, "audio.ac3" if (audioformat == "AC-3") else "audio.aac")]) | |
proc = cmdline(["./mp4box", "-add", | |
"{0}:fps={1}".format("video.h264" if (videoformat == "AVC") else "video.h265", framerate), "-add", | |
"{0}".format("audio.ac3" if (audioformat == "AC-3") else "audio.aac"), | |
args.retag.replace('.mkv','.mp4')]) | |
except Exception, e: | |
print(e) | |
if __name__ == '__main__': | |
run() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment