libx264 video codec and aac audio codec with a fast preset to ensure compatibility. This process creates a standard MP4 container that VLC players, including mobile devices, can read without error.
Understanding Container vs Codec Differences
An MKV file is a container format, similar to an MP4 file, but it supports a wider variety of video and audio codecs. Often, an MKV file contains H.265 (HEVC) video or AC3 audio, which older versions of VLC or specific hardware decoders might not support efficiently. An MP4 container typically expects H.264 video and AAC audio for maximum compatibility. Therefore, converting from MKV to MP4 often necessitates transcoding the streams rather than just repackaging them.
If your goal is simply to change the extension without altering the quality, you might try stream copying. However, this frequently fails if the internal codecs are incompatible with the MP4 specification. For seamless playback in VLC, especially on different platforms, transcoding is the safer route.
Basic Conversion Command
The fundamental command to convert an MKV file to MP4 using FFmpeg involves specifying the input file, output file, and the desired codecs. Here is the standard command structure:
ffmpeg -i input.mkv -c:v libx264 -crf 23 -c:a aac output.mp4
This command does several things. First, -i input.mkv specifies the source file. Next, -c:v libx264 tells FFmpeg to use the H.264 video encoder, which is universally supported by MP4 containers. The -crf 23 option sets the Constant Rate Factor, where lower values mean higher quality; 23 is a good balance between file size and visual fidelity. Finally, -c:a aac converts the audio to AAC format, ensuring broad compatibility.
Optimizing for Speed and Quality
For users who need faster conversion times, adding a preset option can significantly speed up the process at the cost of some compression efficiency. The -preset flag allows you to choose from options like ultrafast, fast, medium, and slow. Using -preset fast is recommended for most users as it provides a good trade-off between speed and file size.
ffmpeg -i input.mkv -c:v libx264 -preset fast -crf 23 -c:a aac output.mp4
If you are dealing with high-resolution footage, such as 4K, consider using the -pix_fmt yuv420p flag to ensure proper color space handling. Some older players or web browsers may struggle with other pixel formats, leading to green screens or black videos. Adding this flag ensures the pixel format is compatible with the widest range of devices.
Handling Audio Streams
MKV files often contain multiple audio tracks or subtitles. By default, FFmpeg will only copy the first audio track found in the input file. If you need to keep specific audio tracks or subtitles, you must explicitly map them. To copy all audio and subtitle streams while re-encoding the video, use the following command:
ffmpeg -i input.mkv -c:v libx264 -crf 23 -c:a copy -c:s mov_text output.mp4
In this example, -c:a copy leaves the audio streams unchanged, preserving their original quality and language. The -c:s mov_text flag converts subtitles to a format supported by MP4 containers. Note that not all subtitle formats are compatible with MP4, so converting them is often necessary.
Troubleshooting Common Issues
Sometimes, conversions fail due to unsupported codecs or corrupted files. If you encounter errors, check the input file using ffprobe to identify the codecs used. You can also refer to our guide on unable to convert mkv video file to mp4 container for more detailed troubleshooting steps related to unsupported codecs.
Another common issue is audio desynchronization. This can happen if the video and audio streams have different timestamps. Using the -async 1 flag can help adjust the audio timestamps to match the video, though it is rarely needed with modern FFmpeg versions.
If you experience issues with the output file not playing correctly despite successful conversion, check our article on troubleshooting convert error for changing output file format to diagnose potential OS-specific problems.
Frequently Asked Questions
Can I convert MKV to MP4 without re-encoding?
Yes, you can use stream copying by omitting the -c:v and -c:a flags, but this only works if the internal codecs are already compatible with the MP4 container. Most MKV files use codecs like HEVC or AC3 that require transcoding for MP4 compatibility.
What is the best CRF value for high-quality video?
A CRF value of 18-20 is considered visually lossless for most viewers, while 23 is a standard default. Lower values result in larger file sizes but better quality. For streaming purposes, 23-28 is often sufficient.
Why does my converted MP4 file have no audio?
This usually happens because the audio codec in the MKV file is not supported by the MP4 container, or FFmpeg failed to find an audio stream. Ensure you specify -c:a aac or use -map 0:a to explicitly select the audio track.
How do I convert multiple MKV files at once?
You can use a loop in your terminal or command prompt. For example, in Bash, you can iterate through all MKV files and run the FFmpeg command for each one. This automates the process for batch conversions.
Is FFmpeg free to use for commercial projects?
FFmpeg is primarily licensed under LGPL, which allows free use in many contexts, but some components are under GPL. Always check the specific license of the libraries you are linking against if you are distributing software commercially.