Skip to content

Instantly share code, notes, and snippets.

@nikdoof
Created October 7, 2012 18:51
Show Gist options
  • Save nikdoof/3849225 to your computer and use it in GitHub Desktop.
Save nikdoof/3849225 to your computer and use it in GitHub Desktop.
Script to remux MKVs to MP4s
#!/bin/bash
if [ "$1" == "" ]; then
echo "Usage: $0 <file>"
exit 0
fi
tempdir="/home/torrent/temp"
MKTMP="mktemp -u -p ${tempdir}"
# Get filename information
fullpath=$(realpath "$1")
outputdir=$(dirname "$fullpath")
filename=$(basename "$fullpath")
name=${filename%.*}
basename=`$MKTMP`
ln -s "${fullpath}" "${basename}"
echo -n "Inspecting ${fullpath}... "
let "video=`mediainfo --Inform=Video\;%ID% ${basename}`-1"
videofmt=`mediainfo --Inform=Video\;%Format% ${basename}`
let "audio=`mediainfo --Inform=Audio\;%ID% ${basename}`-1"
audiofmt=`mediainfo --Inform=Audio\;%Format% ${basename}`
fps=`mediainfo --Inform=Video\;%FrameRate% ${basename}`
echo "Done"
echo "Format: ${videofmt} (${video}) @ ${fps} fps / ${audiofmt} (${audio})"
echo -n "Extracting Video Track... "
videofile=`$MKTMP`
mkvextract tracks "${basename}" "${video}:${videofile}.h264" > /dev/null
echo "Done"
echo -n "Extracting & Reencoding ${audiofmt} Audio Track... "
audiofile=`$MKTMP`
if [ "${audiofmt}" == "AC-3" ]; then
mkvextract tracks "${basename}" "${audio}:${audiofile}" > /dev/null
a52dec "${audiofile}" -o wavdolby | faac - -o "${audiofile}.m4a" > /dev/null
fi
if [ "${audiofmt}" == "DTS" ]; then
mkvextract tracks "${basename}" "${audiofile}.dts" > /dev/null
dtsdec -o wavdolby "${audiofile}" | faac - -o "${audiofile}.m4a" > /dev/null
fi
echo "Done"
echo -n "Muxing MP4 to ${outputdir}/${name}.mp4... "
MP4Box -tmp ${tempdir} -add "${audiofile}.m4a" -add "${videofile}.h264" -fps $fps "${outputdir}/${name}.mp4" > /dev/null
echo "Done"
rm ${audiofile}* ${videofile}* ${basename}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment