Skip to content

Instantly share code, notes, and snippets.

@ziadoz ziadoz/convert_video.sh
Last active Nov 16, 2017

Embed
What would you like to do?
MKV to MP4 (Docker, FFMpeg, AVConv)
#!/usr/bin/env bash
# Convert MKV to MP4
# Uses Docker FFMpeg, FFMpeg or AVConv.
#
# Usage:
# ./convert_video.sh
# ./convert_video.sh /path/to/videos
#
# Notes:
# Statistics: -stats, -nostats, -loglevel 0
# Skip Subtitles: -sn
# Copy Subtitles: -c:s mov_text
#
# Links:
# https://askubuntu.com/questions/396883/how-to-simply-convert-video-files-i-e-mkv-to-mp4
convert_video() {
local video="$1"
local opts="-nostats -loglevel 0 -c:v copy -c:a copy -c:s mov_text -movflags +faststart"
if [ -e "${video%.*}.mp4" ]; then
echo "Skipping '$(basename "$video")'"
return
fi
echo "Processing '$(basename "$video")'"
if which docker > /dev/null; then
local path=$(dirname "$video")
local file=$(basename "$video")
docker run --rm -v="$path:/tmp/workdir" -w="/tmp/workdir" jrottenberg/ffmpeg -i "${file}" $opts "${file%.*}.mp4"
elif which ffmpeg > /dev/null; then
ffmpeg -i "${video}" $opts "${video%.*}.mp4"
elif which avconv > /dev/null; then
avconv -i "${video}" $opts "${video%.*}.mp4"
else
echo "Could not locate ffmpeg or avconv"
exit 1
fi
if [ "$?" -ne "0" ]; then
echo "Failed to convert video '$video'"
exit 1
fi
}
VIDEO_PATH=${1:-.}
if [ ! -d "$VIDEO_PATH" ]; then
echo "Directory '$VIDEO_PATH' does not exist"
exit 1
fi
find "$VIDEO_PATH" \
-type f \
-name "*.mkv" \
-not -iwholename "*.AppleDouble*" \
-not -iwholename "*._*" \
| while read file; do convert_video "$file"; done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.