I need a Python script that automates the creation of a video using FFmpeg. The script should do the following:
Use a voiceover audio file (voiceover.mp3) as the base for the video length. Select images randomly from the "images" folder (supports .jpeg and .png formats). Display each image for 10 seconds before transitioning to the next. Apply a slow zoom-in effect (Ken Burns effect) on each image from start to finish. Concatenate images sequentially until the total duration matches the voiceover. Export the final video in .mp4 format with: Resolution: 1280x720 (720p) Framerate: 24 FPS Hardware Acceleration: Utilize my NVIDIA RTX 3060 GPU for encoding. Ensure the output video length matches the voiceover duration by either trimming or looping images if necessary.
use python: A Python script will be the best choice for this task because:
FFmpeg Automation – Python can easily call FFmpeg commands using subprocess. Random Image Selection – Python's random and os modules can select images dynamically. Voiceover Duration Matching – Python can analyze the audio duration (pydub, mutagen, or ffmpeg-python). Keyframe Animation (Zoom-In Effect) – FFmpeg can handle this directly using the zoompan filter. GPU Acceleration – Python can configure FFmpeg to use NVIDIA NVENC for faster video encoding. Key Libraries to Use: subprocess (to run FFmpeg commands) os (to scan image files) random (to shuffle images) ffmpeg-python or pydub (to get audio duration)
I'll create a Python script that automates the video creation process using FFmpeg, incorporating all the requirements you've specified. This script will use the subprocess module to run FFmpeg commands, os and random modules for file handling, and ffmpeg-python to get the audio duration.
Here's the Python script that accomplishes your task:
create_video.py
This script does the following:
ffmpeg-python
library to get the duration of the voiceover audio file.It prepares the FFmpeg command, including:
subprocess.run()
.To use this script:
Install the required Python library:
pip install ffmpeg-python
Run the script:
python create_video.py
The script will create a video named "output.mp4" in the same directory, matching the duration of the voiceover, with images changing every 10 seconds and a slow zoom-in effect applied to each image. The video will be encoded using your NVIDIA RTX 3060 GPU for faster processing.
Note: If the voiceover is shorter than the total duration of all images, the video will be trimmed to match the voiceover length. If the voiceover is longer, images will loop to fill the entire duration.
The script is working as expected, but I need two issues to be fixed:
Issues: Output Directory Issue:
Currently, the script exports the final video in the same directory as the script. Instead, it should save the output inside a folder named "output", which is located in the same directory as the script. If the "output" folder does not exist, the script should create it automatically before exporting. Image Selection Issue:
The script is repeatedly using the same image multiple times. Instead, it should randomly select an image from the "images" folder (which is in the same directory as the script) for each 10-second segment.
I understand. I'll modify the script to address these two issues. Here's the updated version of the script:
create_video.py
Here are the changes I've made to address the issues:
Output Directory Issue:
OUTPUT_DIR
that points to an "output" folder in the same directory as the script.os.makedirs(OUTPUT_DIR, exist_ok=True)
.Image Selection Issue:
random.choice(image_files)
inside the loop that prepares the FFmpeg inputs.Additional improvements:
os.path.dirname(os.path.abspath(__file__))
to get the directory of the script, ensuring that all paths are relative to the script's location.os.path.join()
for better cross-platform compatibility.To use this updated script:
Ensure you have the required Python library installed:
pip install ffmpeg-python
Run the script:
python create_video.py
The script will create an "output" folder (if it doesn't exist) and save the final video as "output.mp4" inside this folder. Each 10-second segment of the video will now use a randomly selected image from the "images" folder, addressing both issues you mentioned.
The output directory issue is fixed, but the image selection issue still persists. The script is still using the same image multiple times instead of selecting different images.
Issue to Fix: Random Image Selection Still Not Working Properly The script should randomly choose a different image from the "images" folder for each 10-second segment. No image should repeat until all available images have been used. Once all images have been used, it should reshuffle and continue selecting randomly until the video reaches the full duration of the voiceover. If there is only one image available, it should be reused.