Skip to content

Instantly share code, notes, and snippets.

@darrenpmeyer
Created June 11, 2024 02:34
Show Gist options
  • Save darrenpmeyer/3f23d30a889b27f9a441e093e64dec11 to your computer and use it in GitHub Desktop.
Save darrenpmeyer/3f23d30a889b27f9a441e093e64dec11 to your computer and use it in GitHub Desktop.
Losslessly convert folder of MKV files from OBS to MP4 files

OBS and other tools produce video files that are h.264 video and AAC audio in an MKV container. This is useful for a lot of tools, but there are also a lot of things that only work with an MP4 container.

This script will convert a folder of MKV files to MP4 files without doing any transcoding, which makes this LOSSLESS (and quite fast). It also is smart enough not to repeat a conversion that's already complete, meaning you can run it after adding more MKV files and it will not perform rework.

Requirements

  • zsh (default on macOS, common on Linux)
  • ffmpeg - tested with brew install ffmpeg version on macOS

Tested on macOS, but should work on any *nix with above requirements.

#!env zsh
## if `env` is not in your path, change the above to point to
## the absolute path of `env` on your system; often `/usr/bin/env`
for mkv in *.mkv ; do
mp4="${mkv%.mkv}.mp4"
echo -n "'$mkv' => '$mp4'"
if [[ -f "$mp4" ]]; then
echo " [SKIPPED] (MP4 already exists)"
continue
fi
bytes=$(stat "$mkv" | cut -d ' ' -f 8)
printf " (%.1fMiB)" $(( $bytes / 1048576 ))
if (ffmpeg -i "$mkv" -codec copy -loglevel error "$mp4"); then
printf " [OK]\n"
## Uncomment the line below if you want to remove the original MKV file after successful conversion
# rm "$mkv"
else
printf "\nWARNING: exit code $? while encoding '$mkv', check for errors above\n"
rm "$mp4"
continue
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment