Convert Audio to Video using Static Images in Python
Last Updated :
28 Apr, 2025
In this article, we are going to convert an audio file(mp3) to a video file(mp4) using the images provided by the user to be shown during the duration of the video using Python. To do this, we will first convert the images to a GIF file and then combining with the audio file to produce the final video file.
Packages Required
Mutagen: This Python package is used to handle audio metadata. It supports various audio formats like ASF, FLAC, MP3, MP4, Musepack, Ogg Opus, and many others. Here, We will be using its MP3 class to get the duration of the audio file. This will be used to decide the duration for which every image will be displayed in the video output file.
pip install mutagen
Pillow: The Pillow(also known as, PIL) package is used to deal with all formats of images like png, jpeg, etc. The most important class in the Python Imaging Library is the Image class used to read, create, resize images, and more.
pip install pillow
Moviepy: MoviePy is a Python library for video editing: cutting, concatenations, video processing, and others. Here, we will be using it to combine the image's GIF file with the audio input file to build the required video file. We will be using its editor class to combine the gif file with the audio file in the final step to give the final result as a video file.
pip install moviepy
ImageIO: Imageio is a Python library that provides an easy interface to read and write a wide range of image data, including animated images, volumetric data, and scientific formats. It is cross-platform, runs on Python 3.5+, and is easy to install. Here, we have used it to used to create a gif from a list of images.
Steps to Convert Audio to Video using Static Images in Python
Step 1: Now, let's import all the required packages in our Python File.
Python3
from mutagen.mp3 import MP3
from PIL import Image
import imageio
from moviepy import editor
from pathlib import Path
import os
Step 2: For this step, we will be requiring three things as follows:
- An audio file: The audio file is to be played in the background.
- The video folder: The folder where the final video file will be saved.
- The images folder: This is the folder from where images are to be picked to display in the video.
Here, all the files and folders are present in the current path. So, we have used the os.path.join function to get the current working directory and appended their names to that path. Otherwise, we can use os.chdir function to change the current working directory and then do the same.
Python3
'''Here,we are using the os module to get the
current working directory and then get the audio
file,images folder and the video folder
(where the final video will be saved)'''
audio_path = os.path.join(os.getcwd(), "audio.mp3")
video_path = os.path.join(os.getcwd(), "videos")
images_path = os.path.join(os.getcwd(), "images")
Step 3: In this step, we will get the duration of the audio file and create a list of all images to be used in the video from the paths provided in the previous step. Get the duration of the audio file and images, After that, we have all the images let's create a GIF File out of them to be played as a video.
Python3
audio = MP3(audio_path)
# To get the total duration in milliseconds
audio_length = audio.info.length
# Get all images from the folder
# Create a list to store all the images
list_of_images = []
for image_file in os.listdir(images_path):
if image_file.endswith('.png') or image_file.
endswith('.jpg'):
image_path = os.path.join(images_path,
image_file)
image = Image.open(image_path).resize
((400, 400), Image.ANTIALIAS)
list_of_images.append(image)
Step 4: Get the duration for which each static image is to be displayed in the video.
Python3
duration = audio_length/len(list_of_images)
imageio.mimsave('images.gif', list_of_images, fps=1/duration)
Step 5: Converting all the images into a GIF. To make the GIF from a list of images, we have used the mimsave function from imageio which takes the following parameters:
Syntax: imageio.mimsave('images.gif',list_of_images,fps=1/duration)
Parameter:
- Path where the gif would be saved(optional)
- A list of images to create the GIF.
- Frame per second: Here, we want to display each image for a duration of seconds. Hence, we specify the frame per second as 1/duration.
Finally, we are done with creating the images.gif.
Python3
'''Converts all images from the images list into an images.gif file
which will be saved in the same directory.Every image will be played for duration
seconds.(calculated in the previous step'''
imageio.mimsave('images.gif',list_of_images,fps=1/duration)
Step 5: In this step, we will be combining the image GIF with the audio file to produce the required video output using the library functions from the imported packages.
Python3
video = editor.VideoFileClip("images.gif")
audio = editor.AudioFileClip(audio_path)
final_video = video.set_audio(audio)
os.chdir(video_path)
final_video.write_videofile(fps=60, codec="libx264", filename="video.mp4")
Complete Code
Python3
from mutagen.mp3 import MP3
from PIL import Image
from pathlib import Path
import os
import imageio
from moviepy import editor
audio_path = os.path.join(os.getcwd(), "audio.mp3")
video_path = os.path.join(os.getcwd(), "videos")
images_path = os.path.join(os.getcwd(), "images")
audio = MP3(audio_path)
audio_length = audio.info.length
list_of_images = []
for image_file in os.listdir(images_path):
if image_file.endswith('.png') or image_file.endswith('.jpg'):
image_path = os.path.join(images_path, image_file)
image = Image.open(image_path).resize((400, 400), Image.ANTIALIAS)
list_of_images.append(image)
duration = audio_length/len(list_of_images)
imageio.mimsave('images.gif', list_of_images, fps=1/duration)
video = editor.VideoFileClip("images.gif")
audio = editor.AudioFileClip(audio_path)
final_video = video.set_audio(audio)
os.chdir(video_path)
final_video.write_videofile(fps=60, codec="libx264", filename="video.mp4")
Output:
After running the code, we can see the video created in the video path specified. Complete directory structure(after running the code)
Similar Reads
Video to Audio convert using Python Prerequisites: Python Programming Language There are several libraries and techniques available in Python for the conversion of Video to Audio. One such library is Movie Editor. MoviePy can read and write all the most common audio and video formats, including GIF, and runs on Windows/Mac/Linux, with
1 min read
Convert Blob Image to PNG and JPG Using Python We are given a task to convert blob images to png and jpg with Python. In this article, we will see how we can convert blob images to PNG and JPG with Python. Convert Blob Image to PNG and JPG With PythonBelow are step-by-step procedures by which we can convert blob images to PNG and JPG with Python
3 min read
Extract Video Frames from Webcam and Save to Images using Python There are two libraries you can use: OpenCV and ImageIO. Which one to choose is situation-dependent and it is usually best to use the one you are already more familiar with. If you are new to both then ImageIO is easier to learn, so it could be a good starting point. Whichever one you choose, you ca
2 min read
Python | Create video using multiple images using OpenCV Creating videos from multiple images is a great way for creating time-lapse videos. In this tutorial, weâll explore how to create a video from multiple images using Python and OpenCV. Creating a video from images involves combining multiple image frames, each captured at a specific moment in time, i
5 min read
Convert files from jpg to png and vice versa using Python Prerequisite: Pillow Library Sometime it is required to attach the Image where we required an image file with the specified extension. And we have the image with a different extension which needs to be converted with a specified extension like in this we will convert the image having an Extension o
3 min read
Convert PNG to JPG using Python PNG and JPG formats are used for image illustrations. Both the formats are used to provide good compatibilities with certain types of images like PNG works better with line drawings and icon graphics whereas JPG works well with photographs. However, both are interconvertible with respect to each oth
3 min read