3

I have a lot of .mkv files that I'm trying to convert to .mp4, so I decided to try and program a solution in python. After a few hours, trying to figure out how to copy the subfolders too, I gave up on it and decided to stick with converting individual subfolders, and then copying them over to another directory.

I've made a simple script, that should convert .mkv files that are in the same folder as the script. However, I keep getting this error:

FileNotFoundError: [WinError 2] The system cannot find the file specified

Here's my code:

import os
import ffmpeg

start_dir = os.getcwd()

def convert_to_mp4(mkv_file):
    no_extension = str(os.path.splitext(mkv_file))
    with_mp4 = no_extension + ".mp4"
    ffmpeg.input(mkv_file).output(with_mp4).run()
    print("Finished converting {}".format(no_extension))

for path, folder, files in os.walk(start_dir):
    for file in files:
        if file.endswith('.mkv'):
            print("Found file: %s" % file)
            convert_to_mp4(file)
        else:
            pass

halfer
  • 19,824
  • 17
  • 99
  • 186
myth0s
  • 81
  • 1
  • 1
  • 4

1 Answers1

5

Well, the answer is always simpler than you expect.

It came down to this:

def convert_to_mp4(mkv_file):
    name, ext = os.path.splitext(mkv_file)
    out_name = name + ".mp4"
    ffmpeg.input(mkv_file).output(out_name).run()
    print("Finished converting {}".format(mkv_file))

for path, folder, files in os.walk(start_dir):
    for file in files:
        if file.endswith('.mkv'):
            print("Found file: %s" % file)
            convert_to_mp4(os.path.join(start_dir, file))
        else:
            pass

Make sure ffmpeg.exe is in the same directory as the script.

myth0s
  • 81
  • 1
  • 1
  • 4