Skip to content

Instantly share code, notes, and snippets.

@innerfence
Created August 1, 2010 01:51
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save innerfence/502844 to your computer and use it in GitHub Desktop.
Save innerfence/502844 to your computer and use it in GitHub Desktop.
#!/bin/bash
#
# mkv2m4v inputfile.mkv
#
# Given an MKV container with H.264 video and AC3 audio, converts
# quickly to an iPad-compatible MP4 container without re-encoding the
# video (so it must already be in an iPad-compatible resolution); the
# audio is downmixed to stereo with Dynamic Range Compression.
#
ME=$(basename $0)
usage() {
echo "usage: ${ME} inputfile.mkv" 1>&2
exit 1
}
message() {
echo ${ME}: "$@"
}
error() {
message "$@" 1>&2
exit 1
}
checkdep() {
[ ! -z $(which "$@") ] || error "Can't find dependency:" "$@"
}
checkdep mkvmerge
checkdep mkvextract
checkdep a52dec
checkdep faac
checkdep MP4Box
INFILE=$1
shift
if [ "${INFILE}" == "" ]; then
usage
fi
if [ ! -r $INFILE ]; then
error "Can't read $INFILE"
fi
DESTDIR=$(dirname $INFILE)
INFILE_BASE=$(basename $INFILE)
BASE=$DESTDIR/${INFILE_BASE%.*}
message "Analyzing Matroska ..."
mkvmerge -i ${INFILE} > ${BASE}.tid
AC3TID=$(grep A_AC3 ${BASE}.tid | perl -ne '/(\d+)/ and print $1')
AVCTID=$(grep AVC ${BASE}.tid | perl -ne '/(\d+)/ and print $1')
if [ "${AC3TID}" == "" ]; then
error "Couldn't find AC3 stream"
fi
if [ "${AVCTID}" == "" ]; then
error "Couldn't find AVC stream"
fi
message "Demultiplexing Matroska ..."
mkvextract tracks ${INFILE} ${AC3TID}:${BASE}.ac3 ${AVCTID}:${BASE}.264
message "Downmixing AC3 ..."
a52dec -o wav ${BASE}.ac3 > ${BASE}.wav
message "Encoding audio for MP4 ..."
faac -b 96 --mpeg-vers 4 -o ${BASE}.aac ${BASE}.wav
message "Creating MP4 container ..."
MP4Box -add ${BASE}.264:fps=23.976 -add ${BASE}.aac ${BASE}.m4v
message "Cleaning up ..."
rm ${BASE}.{tid,264,ac3,wav,aac}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment