Skip to content

Instantly share code, notes, and snippets.

@jamesmacwhite
Last active May 13, 2024 10:18
Show Gist options
  • Save jamesmacwhite/58aebfe4a82bb8d645a797a1ba975132 to your computer and use it in GitHub Desktop.
Save jamesmacwhite/58aebfe4a82bb8d645a797a1ba975132 to your computer and use it in GitHub Desktop.
Easy way to convert MKV to MP4 with ffmpeg

Converting mkv to mp4 with ffmpeg

Essentially just copy the existing video and audio stream as is into a new container, no funny business!

The easiest way to "convert" MKV to MP4, is to copy the existing video and audio streams and place them into a new container. This avoids any encoding task and hence no quality will be lost, it is also a fairly quick process and requires very little CPU power. The main factor is disk read/write speed.

With ffmpeg this can be achieved with -c copy. Older examples may use -vcodec copy -acodec copy which does the same thing.

These examples assume ffmpeg is in your PATH. If not just substitute with the full path to your ffmpeg binary.

Single file conversion example

ffmpeg -i example.mkv -c copy example.mp4

Batch conversion example

If you want to batch convert multiple MKV files, you can switch into the directory that contains MKV files and run the following, depending on OS. This can be run directly from command line. All MKV files found in the directory will be converted with their original filename.

Unix one-liner example i.e. MacOS/Linux:
for f in *.mkv; do ffmpeg -i "$f" -c copy "${f%.mkv}.mp4"; done
Windows one-liner example:
for /R %f IN (*.mkv) DO ffmpeg -i "%f" -c copy "%~nf.mp4"

/R is used to recursively search within sub directories at the target location.

If running within a batch script, you will need to double the percentage signs %% in order for the command to run properly.

In both examples, the original .mkv extension is removed from the final output to avoid the filename ending up as example.mkv.mp4 which would be a bit weird, although harmless.

@bluttmer
Copy link

Hello,

I am trying to get this to work with using folders within a folder. When run the script on a path it puts the mp4 files in the root folder instead of where the files were, that causes issues with files with same name.

example.

I run the script from this folder...

C:\Users\me\Downloads\Smallville (2001)

This folder has folders within folders and some files would have same name. ie "Disc 1.mkv" under Features

C:\Users\me\Downloads\Smallville (2001)\Featurettes\Season 2\Deleted Scenes\Disc 1.mkv
C:\Users\me\Downloads\Smallville (2001)\Featurettes\Season 2\Deleted Scenes\Disc 2.mkv
C:\Users\me\Downloads\Smallville (2001)\Featurettes\Season 2\Deleted Scenes\Disc 3.mkv
C:\Users\me\Downloads\Smallville (2001)\Featurettes\Season 2\Deleted Scenes\Disc 4.mkv

C:\Users\me\Downloads\Smallville (2001)\Featurettes\Season 3\Deleted Scenes\Disc 1.mkv
C:\Users\me\Downloads\Smallville (2001)\Featurettes\Season 3\Deleted Scenes\Disc 2.mkv
C:\Users\me\Downloads\Smallville (2001)\Featurettes\Season 3\Deleted Scenes\Disc 3.mkv
C:\Users\me\Downloads\Smallville (2001)\Featurettes\Season 3\Deleted Scenes\Disc 4.mkv

C:\Users\me\Downloads\Smallville (2001)\Featurettes\Season 4\Deleted Scenes\Disc 1.mkv
C:\Users\me\Downloads\Smallville (2001)\Featurettes\Season 4\Deleted Scenes\Disc 2.mkv
C:\Users\me\Downloads\Smallville (2001)\Featurettes\Season 4\Deleted Scenes\Disc 3.mkv
C:\Users\me\Downloads\Smallville (2001)\Featurettes\Season 4\Deleted Scenes\Disc 4.mkv

the converted files are put under so then it keeps asking me to rename files that have same name ie Disc 1.mkv is repeated several times.
C:\Users\me\Downloads\Smallville (2001)\

This is what is in the batch file...

for /R %%f in (*.mkv) do "ffmpeg.exe" -i "%%f" -vcodec copy -acodec copy "%%~nf.mp4"

Any help is appreciated.

Thank you

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment