Batch Convert .mkv to .mp4

53,807

Solution 1

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

Solution 2

This is a tweak of part of the answer slhck gave (and thanks, slhck!) because I found that command didn't work for the latest (at this writing) ffmpeg--maybe the parameters have changed between versions?

On Windows 7, using ffmpeg v.20130103-git-9e36d9e-win64-static (decompress it with the 7zip utility), found via http://ffmpeg.zeranoe.com/builds/ in turn via http://ffmpeg.org/download.html, the following command converted a .mov file (taken from an iPhone) into a .mp4 file:

ffmpeg -i test.mov -vcodec copy -acodec copy test.mp4

I found that changing the .mp4 part of this to .avi also worked, to change it to an .avi container. With either target container, using these "-vcodec copy -acodec copy" paramaters seem to only copy the source streams into a new container (in the target), so it's lossless conversion (and I'm guessing that would be the case with a variety of target containers, too).

These tweaks of the batches slhck gave also worked for me:

callConvert.bat:

for %%i IN (*.mov) DO (convert.bat "%%i")
pause

convert.bat:

IF EXIST %1.mov GOTO exit

@echo Conversion for %1 started on %DATE% %TIME% 
ffmpeg -i %1 -vcodec copy -acodec copy %1.avi

:exit 
@echo %1.avi already exists
Share:
53,807

Related videos on Youtube

slhck
Author by

slhck

Updated on September 18, 2022

Comments

  • slhck
    slhck almost 2 years

    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)?

    • Admin
      Admin about 12 years
      What operating system are you on, and do you necessarily have to use VLC? Also, ISO MP4 doesn't support subtitles as far as I remember — what should be done about them?
    • Admin
      Admin about 12 years
      for a test you can try another player (mplayer/KMPlayer/BSPlayer)
    • Admin
      Admin over 11 years
      Handbrake, that is all.
    • Admin
      Admin over 11 years
      @Sam Handbrake will re-encode the video and audio streams, leaving you with bad quality as a result. Also, this is what the OP specifically didn't want.
  • slhck
    slhck about 12 years
    For the record, I tested FFmpeg and the subtitle extraction on OS X and it worked flawlessly.
  • Admin
    Admin about 12 years
    I used the ffmpeg for Windows. But is there a batch so it automatically converts/mux all files in the folder to mp4 with same filename? Or maybe a batch that converts the file on drag and drop
  • kjosh
    kjosh about 8 years
    "IF EXIST %1.mov GOTO exit" should read "%1.avi" (or whatever the target format is, instead of the source format) instead, no? Other than that this worked great for me, thank you!
  • Mikhail T.
    Mikhail T. over 7 years
    Whatever command-line you use, you should be putting it inside a Makefile -- in order to be able to rerun it and convert only the new sources as well as to be able to run multiple conversions in parallel (to make use of all your CPUs).
  • Mark Deven
    Mark Deven over 3 years
    also note you can use the linux subsystem on windows. Takes a hot sec to setup but stuff like this is so much easier with a linux shell