Last active
October 10, 2019 03:40
-
-
Save ziadoz/233a9318aea459d25b3f585bc7aeacfd to your computer and use it in GitHub Desktop.
MKV to MP4 (Docker, FFMpeg, AVConv)
This file contains hidden or 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
#!/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