Skip to content

Instantly share code, notes, and snippets.

@rdela
Forked from lisamelton/convert-mp4-to-mkv.sh
Created July 3, 2014 02:15
Show Gist options
  • Save rdela/22e29e60b9ebcadad157 to your computer and use it in GitHub Desktop.
Save rdela/22e29e60b9ebcadad157 to your computer and use it in GitHub Desktop.
#!/bin/bash
#
# convert-mp4-to-mkv.sh
#
# Copyright (c) 2013-2014 Don Melton
#
about() {
cat <<EOF
$program 1.0 of May 30, 2014
Copyright (c) 2013-2014 Don Melton
EOF
exit 0
}
usage() {
cat <<EOF
Convert MP4 video file into Matroska format with single audio track.
Usage: $program [OPTION]... [FILE]
--help display this help and exit
--version output version information and exit
Requires \`mkvmerge\` executable in \$PATH.
Output is written to current working directory.
EOF
exit 0
}
syntax_error() {
echo "$program: $1" >&2
echo "Try \`$program --help\` for more information." >&2
exit 1
}
die() {
echo "$program: $1" >&2
exit ${2:-1}
}
readonly program="$(basename "$0")"
case $1 in
--help)
usage
;;
--version)
about
;;
esac
readonly input="$1"
if [ ! "$input" ]; then
syntax_error 'too few arguments'
fi
if [ ! -f "$input" ]; then
die "input file not found: $input"
fi
readonly output="$(basename "$input" | sed 's/\.[^.]*$//').mkv"
if [ -e "$output" ]; then
die "output file already exists: $output"
fi
if ! $(which mkvmerge >/dev/null); then
die 'executable not in $PATH: mkvmerge'
fi
readonly identification="$(mkvmerge --identify "$input")"
readonly container="$(echo "$identification" | sed -n 's/^.*container: //p')"
if [ "$container" != 'QuickTime/MP4' ]; then
die "input file not QuickTime/MP4 format: $input"
fi
readonly track1="$(echo "$identification" | sed -n 's/^Track ID 1: //p')"
readonly track2="$(echo "$identification" | sed -n 's/^Track ID 2: //p')"
if [ "$track1" == 'audio (AAC)' ] && [ "$track2" == 'audio (AC3/EAC3)' ]; then
audio_tracks='2'
else
audio_tracks='1'
fi
echo "Converting: $input" >&2
time mkvmerge --output "$output" --audio-tracks $audio_tracks "$input" || exit 1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment