Want to code faster? Our Python Code Generator lets you create Python scripts with just a few clicks. Try it now!
There are many cases where you want to convert your audio file into a video, such as uploading audio to YouTube or something similar. In this tutorial, you will learn how to add a static image to an audio file to form a video file with Python using the MoviePy library.
Before we get started with the code, let's install MoviePy:
$ pip install moviepy Open up a new Python file and write the following:
from moviepy.editor import AudioFileClip, ImageClip def add_static_image_to_audio(image_path, audio_path, output_path): """Create and save a video file to `output_path` after combining a static image that is located in `image_path` with an audio file in `audio_path`""" # create the audio clip object audio_clip = AudioFileClip(audio_path) # create the image clip object image_clip = ImageClip(image_path) # use set_audio method from image clip to combine the audio with the image video_clip = image_clip.set_audio(audio_clip) # specify the duration of the new clip to be the duration of the audio clip video_clip.duration = audio_clip.duration # set the FPS to 1 video_clip.fps = 1 # write the resuling video clip video_clip.write_videofile(output_path) The add_static_image_to_audio() function does everything, it expects the image path, audio path, and the output video path, and then:
AudioFileClip() instance from the audio_path.ImageClip() instance from the image_path.set_audio() method which returns a new clip.write_videofile() method to save the resulting video file.Now let's use the argparse module to parse command-line arguments:
if __name__ == "__main__": import argparse parser = argparse.ArgumentParser(description="Simple Python script to add a static image to an audio to make a video") parser.add_argument("image", help="The image path") parser.add_argument("audio", help="The audio path") parser.add_argument("output", help="The output video file path") args = parser.parse_args() add_static_image_to_audio(args.image, args.audio, args.output) Awesome, let's test it:
$ python add_photo_to_audio.py --help Output:
usage: add_photo_to_audio.py [-h] image audio output Simple Python script to add a static image to an audio to make a video positional arguments: image The image path audio The audio path output The output video file path optional arguments: -h, --help show this help message and exit Great, let's try it out with this image and this audio file:
$ python add_photo_to_audio.py directed-by-robert-image.jpg "Directed-by-Robert-B.-Weide-theme.mp3" output.mp4 The output.mp4 file will appear in the current directory:

Alright, that's it for the tutorial! Check the full code here.
Learn also: How to Extract Audio from Video in Python.
Happy coding ♥
Let our Code Converter simplify your multi-language projects. It's like having a coding translator at your fingertips. Don't miss out!
View Full Code Generate Python Code
Got a coding query or need some guidance before you comment? Check out this Python Code Assistant for expert advice and handy tips. It's like having a coding tutor right in your fingertips!