9

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?

Nick ODell
  • 15,465
  • 3
  • 32
  • 66
Isaac Wassouf
  • 93
  • 1
  • 1
  • 5

4 Answers4

26

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.

Novus
  • 791
  • 8
  • 13
  • thanks this helped me alot. can you also write tell me a method so that after the merging of video and audio file is completed it deletes the original video and audio file from the directory. please help me. – UNRIVALLEDKING Jun 25 '22 at 07:06
  • my video file is 40mb and my audio file is 5mb but when i combine them i am getting 90mb which is abnormal. i was expecting the output file to be the size of the video file + the size of the audio file. My question is, how can I maintain the size of the output file to be equal to the size of the input video and audio file? – prince david Sep 05 '22 at 21:38
4

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}")
Ant
  • 151
  • 5
  • 5
    Welcome to Stack Overflow! This is not what the person is asking for. He knows how to use ''ffmpeg" to combine audio with video but he wants to use the "ffmpeg-python" package to combine the audio with the video. – Armster Dec 15 '19 at 20:52
  • 1
    This might not be exactly what OP asked for but this works SIGNIFICANTLY faster than the accepted answer. Like thousands of times faster. I think it avoids re-encoding the entire video. – Frobot May 04 '23 at 20:53
0
import ffmpeg
input_video = ffmpeg.input("../resources/video_with_audio.mp4")
added_audio = ffmpeg.input("../resources/dance_beat.ogg").audio.filter('adelay', "1500|1500")
merged_audio = ffmpeg.filter([input_video.audio, added_audio], 'amix')
(ffmpeg
.concat(input_video, merged_audio, v=1, a=1)
.output("mix_delayed_audio.mp4")
.run(overwrite_output=True))

you can review this link https://github.com/kkroening/ffmpeg-python/issues/281#issuecomment-546724993

  • I get this error: `ValueError: Encountered atempo(2) <721e83834c8e> with multiple outgoing edges with same upstream label None; a `split` filter is probably required` – Damien Oct 28 '21 at 12:42
-1

Added the following code: https://github.com/russellstrei/combineViaFFMPEG

It walks the directory, finds ".mp4" files, adds it to the file to be used by ffmpeg, then executes the command.

for name in glob.glob(directory +"\\"+ '*.mp4'):
    print(name)
    file1 = open(processList, "a")  # append mode
    file1.write("file '" + name + "'\n")
    file1.close()
    execute()

def execute():
    cmd = "ffmpeg -f concat -safe 0 -i " + processList + " -c copy "+ dir + "output.mp4"
    os.system(cmd)
Gino Mempin
  • 25,369
  • 29
  • 96
  • 135