In this content, we will learn the converting of audio to video using static images in python.
Introduction
We are going to convert an audio file to video file using 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 and the combine with audio file to produce the final video file. There are many cases where you want to convert your audio file into a video, such as uploading audio to you tube.
from moviepy.editor import AudioFileClip, ImageClip image_path = 'path/to/your/image.jpg' audio_path = 'path/to/your/audio.mp3' output_video_path = 'path/to/your/video.mp4' audio_clip = AudioFileClip(audio_path) image_clip = ImageClip(image_path, duration = audio_clip.duration) video_clip = image_clip.set_audio(audio_clip) video_clip.write_videofile(output_video_path, fps=24)
replaceĀ ‘path/to/your/image.jpg’, ‘path/to/your/audio.mp3′, path/to/your/video.mp4’ with the actual paths to your image file, audio file and desired output video file.
Steps to convert Audio to Video using Static Images in Python
Step 1: Let’s import all the required packages in our python file.
Step 2: We create the AudioFileClip() instance from the audio_path. We create the ImageClip() instance from the image_path.
Step 3: We will get the duration of the audio file and create the list of images to be displayed as the final video file. We add the audio to ImageClip instance which returns a new clip.
Step 4: We set the duration of this new video clip to the duration of the audio clip. You can change the length you want.
Step 5: Finally , we use the write_videofile() method to save the resulting video file.
Step 4: After running the code, we see the video created in the video path.