Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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

share|improve this question
    
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? –  slhck Jun 23 '12 at 15:26
    
for a test you can try another player (mplayer/KMPlayer/BSPlayer) –  Remus Rigo Jun 23 '12 at 16:41
    
Handbrake, that is all. –  Sam Jan 7 '13 at 1:27
    
@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 Jan 21 '13 at 8:48
add comment

3 Answers 3

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. For Windows, you can download the Zeranoe builds. In Linux, check FFmpeg from package sources. In OS X, use brew install ffmpeg.

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
share|improve this answer
    
For the record, I tested FFmpeg and the subtitle extraction on OS X and it worked flawlessly. –  slhck Jun 23 '12 at 16:05
    
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 –  user141974 Jun 23 '12 at 20:07
add comment

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|improve this answer
    
There's no difference between -c:a and -acodec, or -c:v and -vcodec. In fact I'm still using the former today, as it's the recommended syntax, and both are mentioned in the FFmpeg documentation. Note that converting the container into AVI might break for many video and audio codecs, as AVI files lack support for predictive coding (which H.264 video uses—the one the iPhone gives you). If you don't use an MP4 or MOV container as the target, it's always better to re-encode. –  slhck Jan 21 '13 at 9:08
add comment

Old question, but here's my answer, for the record. MP4 can't do subtitles unless they're made part of the video frame, but this is how I convert .mkv files to .mp4:

foreach($inputFile in get-childitem -recurse -Filter *.mkv) {
    $outputFileName = [System.IO.Path]::GetFileNameWithoutExtension($inputFile.FullName) + ".mp4";
    $outputFileName = [System.IO.Path]::Combine($inputFile.DirectoryName, $outputFileName);

    $progDir = ${env:ProgramFiles(x86)};
    if ($progDir -eq $null) {
        $progDir = $env:ProgramFiles;
    }

    $processName = $progDir + "\VideoLAN\VLC\vlc.exe";
    $processArgs = "-I dummy -v `"$($inputFile.FullName)`" --sout=#transcode{}:standard{access=`"file`",mux=`"mp4`",dst=`"$outputFileName`"} vlc://quit"

    start-process $processName $processArgs -wait
}

Save it as mkv2mp4.ps1 and drop it in the folder you're working with. Right click and "Open in Powershell" and it will do its thing.

This is the first time I've even looked at Powershell (and the last I hope – why couldn't MS just port Bash?!) but it works for me and is based on the code I found here: https://wiki.videolan.org/How_to_Batch_Encode/#Powershell

share|improve this answer
add comment

protected by slhck Dec 2 '13 at 8:27

Thank you for your interest in this question. Because it has attracted low-quality answers, posting an answer now requires 10 reputation on this site.

Would you like to answer one of these unanswered questions instead?

Not the answer you're looking for? Browse other questions tagged or ask your own question.