Open In App

Convert Audio to Video using Static Images in Python

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.




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:

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.




'''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.




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.




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. 




'''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.




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 




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)

 


Article Tags :