ffmpegmp4mkv

ffmpeg mkv to mp4 converter drops part of the audio, how to configure in python?


I am converting several mkv files to mp4 via ffmpeg and python. However when I go to play them, the background audio is synced up perfect, but it's as if it's dropped all the voices. Is there a way to set all the audio to mono?

or how do I specify the number of channels?

The following audio and video settings work in vlc:

video settings:
Codec = H-265
bitrate = 800 kb/s

audio settings:
Codec = MP3
bitrate = 128 kb/s
channels = 2
sample rate = 8000Hz

my python code:

import os
import ffmpeg
import glob

start_dir = os.chdir("C:/Users/Me/Downloads/")
files = [file for file in glob.glob("*.mkv")]

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))

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

Solution

  • I was also using FFMPEG to convert files from .mkv to .mp4 and was facing the same issue with the audio. So, I've instead used moviepy library for converting it without losing audio.

    You can simply use it by mentioning video-codec and audio-codec in the arguments of clip.write_videofile() as mentioned below:

    import moviepy.editor as movpy
    
    clip = movpy.VideoFileClip("video.mkv") #Reading .mkv file
    clip.write_videofile("video.mp4", codec="libx264",audio_codec="aac") #Writing .mp4 file