Himanshu Sharma
Himanshu Sharma

Reputation: 55

Video editing in python. Combining a .mp3 and .mp4 file in python using mhmovie

I am trying to combine audio and video files in python and I have experimented quite a few ways. The only way that worked was this, where 'combine' is the path of a folder and 'name' is the name of both the audio and video file in the folder.

import ffmpeg-python
infile1 = ffmpeg.input(combine + "/" + name + ".mp4")
infile2 = ffmpeg.input(combine + "/" + name + ".mp3")

ffmpeg.concat(infile1, infile2, v=1, a=1).output(final_save_path + "/" + name + ".mp4").run()

However, this takes too much processing time I was forced to look for another method. I tried using mhmovie from what I read online but I keep getting an error

from mhmovie.code import *
m = movie(combine + "/" + name + ".mp4")
mu = music(combine + "/" + name + ".mp3")
final = m + mu
final.save(final_save_path + "/" + name + ".mp4")

This is the error

FileNotFoundError: the path \Users\himanshusharma\PycharmProjects\APIs\Youtube\Combine\Best Of Elon Musk 2018 (ITS ALL OVER NOW).mp3 is not found 

And this is exact path of the .mp3 file

/Users/himanshusharma/PycharmProjects/APIs/Youtube/Combine/Best Of Elon Musk 2018 (ITS ALL OVER NOW).mp3

How do I solve this? Or is there another way to combine audio and video? Thanks

Upvotes: 1

Views: 1562

Answers (3)

Guest
Guest

Reputation: 1

Use

import ffmpeg
infile1 = ffmpeg.input(combine + "/" + name + ".mp4")
infile2 = ffmpeg.input(combine + "/" + name + ".mp3")

ffmpeg.concat(infile1, infile2, v=1, a=1).output(final_save_path + "/" + name + ".mp4").run()

Upvotes: 0

Danny Meyer
Danny Meyer

Reputation: 381

An approach I often take is to simply shell out to ffmpeg.

import os

cmd = "ffmpeg -i video.mp4 -i audio.wav -c:v copy -c:a aac output.mp4"
os.system(cmd)

Upvotes: 2

Danielle
Danielle

Reputation: 58

What operating system are you using? If Windows, /Users is a shortcut for C:/Users. Also I notice that the slashes in your question aren't the same: In the error the slashes are "\" and in the path it's "/"

I see that your mp4 and mp3 are in the same folder, have you tried the folder option?

f = folder(combine)  
f.save()

This worked for me on Windows 10, using PyCharm:

f = folder("C:\\Users\\Danielle PC\\Pictures\\testfolder")  
f.save()

where testfolder has two files: movie.mp4 and music.mp3. The result is in output.mp4 in the same folder.

Upvotes: 1

Related Questions