Batch Convert MP4 to MKV

I ran into an issue where a number of my MP4 files on my fileserver were stuttering when I tried to play them back using Plex on my TV. After a lot of googling, I found out that this was due to some header information in the MP4 files and that the simple solution was to mux them into MKV, which wipes the header information in the process.

This wouldn’t be a huge deal if I only had a couple of MP4 files on my fileserver. But I have several hundred. Muxing the MP4 files into MKV one at a time would take too long. So, I wrote a simple bash script that will go through all of the video files on my fileserver and convert them.

First, install MKVToolNix. You can find it here. I’m running Xubuntu 22.04.4 LTS on my fileserver and MKVToolNix was in the repository. That made installing it for me pretty easy from the command line or console:

sudo apt-get install mkvtoolnix mkvtoolnix-gui

(FYI, the GUI isn’t necessary if you’re just running this from the command line. I installed it to get some hints on how to run this from the command line.)

Next, navigate in your console to the folder where one of your MP4 files is stored and try this line of code:

find . -name *.mp4 -exec mkvmerge -v -o {}.mkv {} \;

Here’s what the code does. First, the “find” tells the computer to search through that folder and all subdirectories, looking for any file that ends in “.mp4”. That’s the “-name *.mp4”. Once it finds a file, it then executes (“-exec”) the program “mkvmerge” which comes from the mkvtoolnix package you just installed. The “-v” is just to tell the command to use “verbose” output so you can see the progress of the conversion. The “-o” is the output file, which is “{}.mkv”. The double brackets just mean to keep the name of the input file. The input file is specified with the empty double brackets “{}”. The last two characters just tell the find function to end “\;”.

I’d suggest trying this first in the folder where you have a single file. It should work fine. But if you want to batch convert all of your videos in MP4 format to MKV, then go to the parent folder where they are all stored and run the same command. It should then search through all of the subfiles looking for MP4 videos and converting them.

NOTE: The resulting file will now end with “mp4.mkv.” I haven’t figured out how to rename the new file so it doesn’t include “mp4” in the name. I’ll play around with that a bit but welcome any suggestions.

Loading


Posted

in

by

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *