Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to combine The video and audio files in ffmpeg-python

I'm trying to combine a video(with no sound) and its separate audio file

I've tried ffmpeg ffmpeg -i video.mp4 -i audio.mp4 -c copy output.mp4 and it works fine.

i'm trying to achieve the same output from ffmpeg-python but with no luck. Any help on how to do this?

like image 826
Isaac Wassouf Avatar asked Jul 10 '19 14:07

Isaac Wassouf


2 Answers

I had the same problem.

Here is the python code after you have pip install ffmpeg-python in your environment:

import ffmpeg

input_video = ffmpeg.input('./test/test_video.webm')

input_audio = ffmpeg.input('./test/test_audio.webm')

ffmpeg.concat(input_video, input_audio, v=1, a=1).output('./processed_folder/finished_video.mp4').run()

v=1: Set the number of output video streams, that is also the number of video streams in each segment. Default is 1.

a=1: Set the number of output audio streams, that is also the number of audio streams in each segment. Default is 0.

For the details of ffmpeg.concat, check out: https://ffmpeg.org/ffmpeg-filters.html#concat.

You can check out more examples here: https://github.com/kkroening/ffmpeg-python/issues/281

Hope this helps!

PS. If you are using MacOS and have the error: FileNotFoundError: [Errno 2] No such file or directory: 'ffmpeg' while running the code, just brew install ffmpeg in your terminal.

like image 71
Novus Avatar answered Nov 08 '22 02:11

Novus


You could use subprocess:

import subprocess    
subprocess.run("ffmpeg -i video.mp4 -i audio.mp4 -c copy output.mp4")

You can also use fstrings to use variable names as input:

videofile = "video.mp4"
audiofile = "audio.mp4"
outputfile = "output.mp4"
codec = "copy"
subprocess.run(f"ffmpeg -i {videofile} -i {audiofile} -c {codec} {outputfile}")
like image 3
Ant Avatar answered Nov 08 '22 02:11

Ant