Combining an audio file with video file in python
Asked Answered
I

4

15

I am writing a program in Python on RaspberryPi(Raspbian), to combine / merge an audio file with video file.

Format of Audio file is WAVE Format of Video file is h264

Audio and video already recorded and created at same time successfully, I just need to merge them now.

Can you please guide me on how do I do that?

Immobile answered 29/1, 2015 at 14:53 Comment(0)
I
12

I got the answer of my Question, you can also try it and let me know if need further assistance

cmd = 'ffmpeg -y -i Audio.wav  -r 30 -i Video.h264  -filter:a aresample=async=1 -c:a flac -c:v copy av.mkv'
subprocess.call(cmd, shell=True)                                     # "Muxing Done
print('Muxing Done')
Immobile answered 3/2, 2015 at 15:20 Comment(2)
I know this is an old question, but I'd like to ask if FFMPEG pre-installed on most systems.Chordophone
No, mostly you’ll have to install ffmpegDwt
W
5

The best tool for manipulating audio and video stream is ffmpeg/libav. Do you have to use Python? You could use command-line binaries from these projects.

For example, taken from https://wiki.libav.org/Snippets/avconv:

avconv -v debug -i audio.wav -i video.mp4 -c:a libmp3lame -qscale 20 -shortest output.mov

(Of course you'll want to tweak the parameters for your files, and qscale for the quality you want.)

You can call this from within python using the subprocess module. If you have to do it in python directly, you could use PyAV (https://pypi.python.org/pypi/av/0.1.0), but this would involve more effort.

Welkin answered 29/1, 2015 at 16:33 Comment(0)
A
5
def combine_audio(vidname, audname, outname, fps=25):
    import moviepy.editor as mpe
    my_clip = mpe.VideoFileClip(vidname)
    audio_background = mpe.AudioFileClip(audname)
    final_clip = my_clip.set_audio(audio_background)
    final_clip.write_videofile(outname,fps=fps)

source::https://www.programcreek.com/python/example/105718/moviepy.editor.VideoFileClip Example no 6

Argeliaargent answered 6/4, 2020 at 15:31 Comment(1)
Hey it generated the video file but it actually didnt combine the video and the audioHaigh
A
0

Using the ffmpeg Python package (e.g. install via conda install ffmpeg)

import ffmpeg
def video_audio_mux(path_audiosource, path_imagesource, out_video_path):
    video = ffmpeg.input(path_imagesource).video
    audio = ffmpeg.input(path_audiosource).audio
    ffmpeg.output(audio, video, out_video_path, vcodec='copy', acodec='copy').run()
Availability answered 9/5, 2024 at 11:46 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.