Last modified: Apr 16, 2026 By Alexander Williams

Add Audio to Video with Python

Merging audio and video is a common task. You might want to add a music track to a silent clip. Or replace the audio in a video file.

Python makes this process simple. With the right libraries, you can automate audio-video merging in just a few lines of code.

This guide will show you how. We will use a popular library called MoviePy.

Why Use Python for Audio-Video Tasks?

Python is a powerful scripting language. It is perfect for media automation. You can batch process hundreds of files.

It is also free and open-source. There is a large community for support. Libraries handle complex multimedia operations for you.

For more on handling sound, see our Python Audio Processing Guide for Beginners.

Setting Up Your Python Environment

First, you need to install the necessary tools. We will use MoviePy and FFmpeg.

MoviePy is a Python library for video editing. It can cut, concatenate, and add audio. FFmpeg is a backend tool that MoviePy uses.

Install MoviePy using pip, Python's package installer.


pip install moviepy
    

You also need FFmpeg installed on your system. On Windows, you can download it from the official website. On macOS, use Homebrew: brew install ffmpeg. On Linux, use your package manager, like sudo apt install ffmpeg.

Ensure FFmpeg is in your system's PATH. This allows MoviePy to find it.

Basic Code: Adding Audio to a Video

Let's write a simple script. We will load a video file and an audio file. Then we will combine them.

Create a new Python file, for example, add_audio.py.


# Import the VideoFileClip and AudioFileClip classes from moviepy
from moviepy.editor import VideoFileClip, AudioFileClip

# Load your video file (replace 'my_video.mp4' with your file)
video = VideoFileClip("my_video.mp4")

# Load your audio file (replace 'my_audio.mp3' with your file)
audio = AudioFileClip("my_audio.mp3")

# Set the audio of the video clip to the new audio
# This replaces any existing audio
final_video = video.set_audio(audio)

# Write the result to a new file
final_video.write_videofile("output_video_with_audio.mp4")

# Optional: Close the clips to free up resources
video.close()
audio.close()
final_video.close()
    

Run the script from your terminal.


python add_audio.py
    

The script will process the files. It will create a new file named output_video_with_audio.mp4. This file contains your video with the new audio track.

The set_audio method is key. It attaches the audio clip to the video clip.

Handling Different Audio and Video Lengths

What if your audio is shorter than the video? Or longer? You need to handle this to avoid errors.

MoviePy provides simple solutions. You can loop the audio or trim the video.

To loop a short audio track, use the loop method.


from moviepy.editor import VideoFileClip, AudioFileClip, afx

video = VideoFileClip("long_video.mp4")
audio = AudioFileClip("short_audio.mp3")

# Loop the audio to match the video's duration
# The duration is taken from the video clip
looped_audio = audio.loop(duration=video.duration)

final_video = video.set_audio(looped_audio)
final_video.write_videofile("output_looped.mp4")
    

If the audio is longer, you can trim it. Use the subclip method on the audio.


from moviepy.editor import VideoFileClip, AudioFileClip

video = VideoFileClip("short_video.mp4")
audio = AudioFileClip("long_audio.mp3")

# Trim the audio from 0 seconds to the video's duration
trimmed_audio = audio.subclip(0, video.duration)

final_video = video.set_audio(trimmed_audio)
final_video.write_videofile("output_trimmed.mp4")
    

Adjusting Audio Volume

Sometimes the new audio is too loud or too quiet. You can adjust its volume easily.

Use the volumex method. A factor of 1.0 is original volume. 0.5 is half volume. 2.0 is double volume.


from moviepy.editor import VideoFileClip, AudioFileClip

video = VideoFileClip("my_video.mp4")
audio = AudioFileClip("my_audio.mp3")

# Reduce the audio volume by half
quieter_audio = audio.volumex(0.5)

final_video = video.set_audio(quieter_audio)
final_video.write_videofile("output_quiet.mp4")
    

For more advanced audio manipulation, explore our guide on Python Audio Libraries: Play, Record, Process.

Adding a Background Music Track

A common use case is adding background music. You often want to keep the original video audio too.

You need to mix two audio tracks. MoviePy's CompositeAudioClip is perfect for this.

You can also lower the volume of the background track.


from moviepy.editor import VideoFileClip, AudioFileClip, CompositeAudioClip

video = VideoFileClip("vlog.mp4")
original_audio = video.audio  # Extract original audio from video
background_music = AudioFileClip("background_music.mp3")

# Lower the volume of the background music
background_music = background_music.volumex(0.3)

# Ensure background music is not longer than the video
if background_music.duration > video.duration:
    background_music = background_music.subclip(0, video.duration)

# Combine the two audio tracks into one
combined_audio = CompositeAudioClip([original_audio, background_music])

# Set the new combined audio to the video
final_video = video.set_audio(combined_audio)
final_video.write_videofile("vlog_with_bgm.mp4")
    

Conclusion

Adding audio to video with Python is straightforward. The MoviePy library handles the complex parts.

You learned to load files, merge tracks, and handle durations. You also saw how to adjust volume and mix audio.

This skill is useful for content creators and developers. You can automate editing workflows and process many files quickly.

Start with the basic script. Then experiment with looping and volume. You will soon master audio-video manipulation in Python.