Batch Convert .mkv to .mp4

.mp4batchmatroskavideo conversionvlc-media-player

I want to batch convert all .mkv files in a folder into .mp4 with VLC.

It should use the original video-/audio stream and if possible the .ass subtitle of the .mkv. It's not really a conversion, it's more like changing the container – my player can't read the MKV videos.

If I do this conversion by hand (manually) it works, but I have a lot of MKV files to convert, so it would take a lot of time.

I have searched the internet for a batch file to do this and I found a few. I tried to modify them to my wish, but all attempts I tried just created a .mp4 file that doesn't contain the audio stream and the video stream also cannot be rendered by all my media players on the PC.

So could someone tell me how the batch has to look like, so it works with the original video and audio stream (and maybe .ass subtitles)?

Best Answer

mkvtomp4

Looks like mkvtomp4 is what you want?

Uses mpeg4ip or GPAC's MP4Box, mkvtoolnix and ffmpeg to convert troublesome mkv files to mp4. The conversion does not re-encode the video and only re-encodes the audio if it doesn't use AAC codec (one can override this behaviour using --audio-codec).

You can download Windows and Linux versions on the Google Code page. You will need additional software though.

Check the sites for downloads for either Windows or Linux. On OS X, you only need to brew install mkvtoolnix mp4box if you have Homebrew.

This will not copy your subtitles though. You'll need an additional step.


FFmpeg batch

If mkvtomp4 does not work for you, a simple FFmpeg batch file could also do.

Install ffmpeg (e.g. via Homebrew or options from https://ffmpeg.org/download.html). Then, just call:

ffmpeg -i input.mkv -c:v copy -c:a copy output.mp4

This will create a valid MP4 container without re-encoding. Loop over the files as needed and available by your operating system. This batch won't, however, copy subtitles.

In *nix, you could do the following. Just create a file convert.sh:

#!/bin/bash
find /path/to/input/folder -iname '*.mkv' -print0 | while read -d '' -r file; do
    ffmpeg -i "$file" -c:v copy -c:a copy ${file%%.mkv}.mp4
done

Replace the path to your video folder here. Make it executable with chmod +x convert.sh, then run it with ./convert.sh.

In Windows, you probably need two Batch files (shameless plug from here), one being startconvert.bat:

for %%i IN (*.mkv) DO (convert-to-mp4.bat "%%i") 
pause

And one that performs the conversion:

IF EXIST "%1.mp4" GOTO exit

@echo Conversion for %1 started on %DATE% %TIME% 
ffmpeg -i %1 -c:v copy -c:a copy %1.mp4

:exit 
@echo %1.mp4 already exists

Save both in the video folder. Simply run startconvert.bat from the folder you want to start the conversion from.


Subtitles

If you want to add the subtitles, you might need a manual procedure if the following doesn't work for you:

ffmpeg -i input.mkv -c:a copy -c:v copy -map 0 output.mp4

To extract subtitles, use:

mkvextract tracks input.mkv 3:subtitles.srt

This is assuming that your file really contains the subtitles at track ID number 3 and they can be exported to the SRT format. To check whether a file really contains subtitles at track 3, use mkvmerge -i input.mkv.

Then, use MP4Box to re-add the subtitles to the MP4 file.

mp4box -add input.mp4 -add subtitles.srt -new output.mp4
Related Question