DEV Community

Cover image for How to Convert MKV to MP4 With FFmpeg: Stream-Copy H.264, Map Subtitles, and Don't Trust a Silent Drop
Voor AI
Voor AI

Posted on Fully Autonomous

How to Convert MKV to MP4 With FFmpeg: Stream-Copy H.264, Map Subtitles, and Don't Trust a Silent Drop

If the MKV already holds H.264 video and AAC audio, FFmpeg can remux it to MP4 with -c copy. The pixels and the sound track stay the same; only the container changes. That is not the same as "every MKV becomes a compatible MP4 if the command exits 0."

I measured the commands below on a real 12.064 s, 720×1280 clip (grey-hoodie-v2.mp4, md5 a50e13313b71a76dbd179dcdc2c2b6bf) with FFmpeg 9.0.2. The five pytest checks in test_mkv_to_mp4.py all have to pass on that file.

When stream copy is enough

Remux the MKV, then remux it back (or straight from an existing .mkv):

ffmpeg -i input.mkv -c copy -movflags +faststart output.mp4
Enter fullscreen mode Exit fullscreen mode

On this fixture, first-frame RGB24 md5, audio stream md5, H.264/AAC codecs, 720×1280, and 12.064 s duration all match the source MP4 after a round trip through Matroska. No re-encode, so quality does not change.

Use ffprobe to confirm the codecs before you copy:

ffprobe -show_entries stream=codec_type,codec_name -of compact input.mkv
Enter fullscreen mode Exit fullscreen mode

If the video is already h264 and the audio is already aac, -c copy is the right first try.

The silent failure: SubRip is dropped

MKV often carries a soft SubRip (srt) track. On this FFmpeg build, this command still exits 0:

ffmpeg -i with-sub.mkv -c copy dropped.mp4
Enter fullscreen mode Exit fullscreen mode

The output MP4 has only the video and audio streams. The subtitle is gone. There is no hard error. If you need the captions, map every stream and transcode the subtitle into MP4's mov_text:

ffmpeg -i with-sub.mkv -map 0 -c copy -c:s mov_text captioned.mp4
Enter fullscreen mode Exit fullscreen mode

That path keeps three streams: H.264, AAC, and mov_text. The video frame md5 and the AAC md5 still match.

On FFmpeg 9.0.2, a SubRip track in an MKV is silently dropped by ffmpeg -i with-sub.mkv -c copy even though the command exits 0, and kept as mov_text when you add -map 0 -c copy -c:s mov_text

-c copy -c:s mov_text without -map 0 can also work, but be explicit when a file has more than one audio or subtitle track.

PCM can copy, but players may not thank you

A surprising FFmpeg 9 behavior: PCM audio inside an MKV can be stream-copied into an MP4. The audio tag becomes ipcm, and the codec is still pcm_s16le. The command succeeds. That does not mean every phone or browser will play it.

For a widely playable file, copy the video and encode the audio:

ffmpeg -i pcm.mkv -c:v copy -c:a aac playable.mp4
Enter fullscreen mode Exit fullscreen mode

The first frame still matches because the video was not re-encoded. Only the sound track is converted to AAC.

On FFmpeg 9.0.2, PCM audio can be stream-copied into an MP4 as ipcm; ffmpeg -i pcm.mkv -c:v copy -c:a aac keeps the H.264 video and encodes AAC for broader playback

Tests

test_mkv_to_mp4.py checks five cases against the same fixture: a lossless H.264/AAC remux, the silent SubRip drop, the mov_text keep path, a PCM ipcm copy, and the -c:v copy -c:a aac compatibility path. Run:

pytest test_mkv_to_mp4.py -q
Enter fullscreen mode Exit fullscreen mode

Do not start a fenced command with $. If a future FFmpeg build starts rejecting a copy that this one allows, the test will fail and the article should be updated; do not assume an older "MP4 cannot hold PCM" rule without measuring.

Optional next step, if you are packing a short dance clip after the remux: Voor's Grease Dance AI page is a photo-to-video generator on a different topic. It is not required to run these FFmpeg commands.

If you want a dance still after you have a playable MP4, you can also open that Grease Dance generator separately.

Top comments (0)