Blenra LogoBlenra

How to Assemble AI-Generated Images & Voiceovers into Video with Python

By Naveen Teja Palle5 min read
Python AI Video Assembly Hero

If you are building a faceless YouTube channel or orchestrating an automated social media agent, creating the assets is only half the battle. Once you have your Midjourney images and ElevenLabs voiceovers, you need a way to stitch them together programmatically.

In this tutorial, we will cover exactly how to assemble AI-generated images and voiceovers into a seamless video using Python, FFmpeg, and the MoviePy library.

Prerequisites

Before we write the code, you need to install the required Python libraries. MoviePy relies on FFmpeg under the hood to handle the heavy video encoding.

pip install moviepy

The Python Script

Here is the complete script. This code takes a single AI-generated image, stretches its duration to match the exact length of your AI voiceover, and exports it as an MP4 file formatted perfectly for platforms like YouTube or Instagram.

import os
from moviepy.editor import AudioFileClip, ImageClip

def create_ai_video(image_path, audio_path, output_path):
    print(f"Loading assets...")
    
    # 1. Load the AI generated voiceover
    audio_clip = AudioFileClip(audio_path)
    audio_duration = audio_clip.duration
    
    # 2. Load the AI image and match its duration to the audio
    image_clip = ImageClip(image_path).set_duration(audio_duration)
    
    # 3. Combine the audio and image
    video_clip = image_clip.set_audio(audio_clip)
    
    print(f"Exporting video to {output_path}...")
    
    # 4. Export the final video using FFmpeg codec
    video_clip.write_videofile(
        output_path, 
        fps=24, 
        codec="libx264", 
        audio_codec="aac"
    )
    
    print("Video assembly complete!")

if __name__ == "__main__":
    # Ensure your assets are in the same directory, or provide full paths
    create_ai_video(
        image_path="midjourney_art.png", 
        audio_path="elevenlabs_voice.mp3", 
        output_path="final_ai_video.mp4"
    )

How the FFmpeg Export Works

The magic happens in the write_videofile function. By explicitly setting the codec="libx264" and audio_codec="aac", we are instructing FFmpeg to format the video in the universal standard for web delivery. This ensures your video will upload smoothly to YouTube, TikTok, and Instagram Reels without audio syncing issues.

Need Better Prompts for Your Video Assets?

Stop guessing what prompts work. Explore our curated directory of AI prompts for lighting, professional headshots, and cinematic scenes.

Explore the Prompt Gallery