Skip to content

How to Convert All MKV Files to MP4 using Python

Converting video files from one format to another can be essential for compatibility, editing, or storage purposes. In this tutorial, we’ll explore how to convert a collection of MKV files to the MP4 format using Python.


MKV (Matroska Multimedia Container) and MP4 (MPEG-4 Part 14) are both popular video file formats. While MKV is known for its ability to hold multiple audio, video, and subtitle tracks in a single file, MP4 is widely supported across various devices and platforms. Converting MKV files to MP4 can ensure better compatibility and ease of use in many situations.

Understanding the Code

We’ll be using Python along with the ffmpeg library to perform the conversion.

Step 1: Installing Required Libraries

Ensure you have the ffmpeg-python library installed. You can install it via pip:

pip install ffmpeg-python

Step 2: Importing Necessary Libraries

import os
import ffmpeg

We import the necessary libraries:

    • os for file operations.
  • ffmpeg for video conversion.

Step 3: Converting MKV to MP4

def mkv_to_mp4(mkv_file):
output_file = os.path.splitext(mkv_file)[0] + ".mp4"
print(f"Converted {mkv_file} to {output_file}")

def convert_all_mkv_to_mp4():
mkv_files = [f for f in os.listdir() if f.endswith(".mkv")]

for mkv_file in mkv_files:

Here, the mkv_to_mp4 function takes an MKV file path, converts it to MP4 using ffmpeg, and saves the output as an MP4 file. The convert_all_mkv_to_mp4 function finds all MKV files in the current directory and converts them to MP4 one by one.

Step 4: Running the Conversion

if __name__ == "__main__":

By running this script, all MKV files in the current directory will be converted to MP4.


In this tutorial, we’ve learned how to convert a collection of MKV files to the MP4 format using Python. Whether you’re dealing with video compatibility issues or need to prepare files for editing or distribution, Python provides powerful tools like ffmpeg to handle video conversion tasks efficiently.

Experiment with different video file formats and explore the capabilities of Python libraries to streamline your multimedia workflows.

Happy coding!