How to Combine a Static Image with Audio in Python

Learn how to add a static photo to an audio file to form a video file using MoviePy library in Python.
  · 3 min read · Updated sep 2021 · Python for Multimedia

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

The add_static_image_to_audio() function does everything, it expects the image path, audio path, and the output video path, and then:

  • It creates the AudioFileClip() instance from the audio_path.
  • It also created the ImageClip() instance from the image_path.
  • We add the audio to the ImageClip instance using the set_audio() method which returns a new clip.
  • We set the duration of this new video clip to the duration of the audio clip (you can change it to any length you want, in seconds).
  • We also set the FPS, setting to 1 means there is one frame per second. It's required for any video clip by the way.
  • Finally, we use the 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.output)

Awesome, let's test it:

$ python --help


usage: [-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 directed-by-robert-image.jpg "Directed-by-Robert-B.-Weide-theme.mp3" output.mp4

The output.mp4 file will appear in the current directory:

Resulting video

Alright, that's it for the tutorial! Check the full code here.

Learn also: How to Extract Audio from Video in Python.

Happy coding ♥

View Full Code
Sharing is caring!

Read Also

Comment panel