50
0

mkv to mp4 using Python | Python Project | FFMPEG

50
0
mkv to mp4 using Python

What are MKV files?

MKV( Matroska Video File) is a videotape container format that supports further than one audio and subtitle track in the same file. It’s based on Extensible Binary Meta Language. It supports several videotape and audio compression formats. The difference between MKV file types with utmost other videotape extensions is that it’s a container, not a codec.

What are MP4 files?

MP4, also known as MPEG4 is a videotape format that’s used to store videotape and audio data. Also, it can store images and subtitles. Usually, it’s used to share videos over the internet. MP4 can embed any data over private streams. Streaming information is included in MP4 using a distinct hint.


MKV to MP4 using Python

Instead of using different-different software, we can create our tool to convert any video. In this article, i have shown how to convert MKV video into MP4 using Python Programming Language + FFMPEG tool.

Requirements:

  • Any Code editor or IDE (Pycharm or VS code).
  • Python Interpreter.
  • No external module installation is required.

Source Code

"""
###################################
Developer: Xalgord (Krishna Kaushal)
Youtube: https://youtube.com/xalgord
github: https://github.com/xalgord
Instagram: https://instagram.com/xalgord
website: https://xalgord.in
###################################
"""

import os
import subprocess

if not os.path.exists("assets"):
    raise Exception("Please create and put all vudeos in assets folder!")

mkv_list = os.listdir("assets")

if not os.path.exists("results"):
    os.mkdir("results")

for mkv in mkv_list:
    name, ext = os.path.splitext(mkv)
    if ext != ".mkv":
        raise Exception("Please add MKV files only!")

    output_name = name + ".mp4"
    try:
        subprocess.run(
            ['ffmpeg', '-i', f"assets/{mkv}", "-codec", "copy", f"results/{output_name}"], check=True
        )
    except:
        raise Exception(
            "Please Download, Install and Add the path of FFMPEG to Environment Variable!"
        )
print(f"{len(mkv_list)} video(s) converted to mp4")
os.startfile("results")

Watch this video for Demo

 

If you liked this, click the 💚 below so other people will see this here on Xalgord. Please let me know if you have any comments! Feel free to connect on Instagram.

Leave a Reply