https://ostechnix.com/create-a-video-from-images-with-ffmpeg
How to Create a Video from Images with FFmpeg (and Add Audio)
FFmpeg is a powerful open-source multimedia framework that allows users to convert, edit, and process audio and video files. One of its most useful features is the ability to create a video from a sequence of images. This guide will walk you through the process of generating a high-quality video from images and adding background audio for a professional touch.
Why Use FFmpeg for Image to Video Conversion?
FFmpeg is lightweight, efficient, and works on almost any operating system. It supports various image and video formats, making it ideal for:
- Creating time-lapse videos from sequential images.
- Converting slideshow presentations into video formats.
- Automating video creation for social media or presentations.
Step 1: Prepare Your Images
Before running FFmpeg, ensure your images are properly formatted:
- Number them sequentially (e.g.,
1.jpg
,2.jpg
,3.jpg
, …100.jpg
). - Place them in the same directory.
- Ensure they have the same resolution to avoid scaling issues.
Step 2: Create a Video from Images with FFmpeg
Run the following FFmpeg command to generate a video from the images:
ffmpeg -framerate 5 -i %d.jpg -vf "scale=1920:-2" -c:v libx264 -pix_fmt yuv420p -movflags +faststart output.mp4
Let us breakdown the above command and see what each option does:
-framerate 5
→ Sets the frame rate to 5 FPS (adjust as needed).-i %d.jpg
→ Tells FFmpeg to use numbered images as input.-vf "scale=1920:-2"
→ Scales width to 1920px and keeps aspect ratio.-c:v libx264
→ Uses the H.264 codec for high-quality compression.-pix_fmt yuv420p
→ Ensures broad media player compatibility.-movflags +faststart
→ Optimizes the video for web streaming.
Step 3: Add Background Audio
To include an audio file (e.g., audio.mp3
) in the video, use:
ffmpeg -framerate 5 -i %d.jpg -i audio.mp3 -vf "scale=1920:-2" -c:v libx264 -pix_fmt yuv420p -c:a aac -b:a 192k -shortest -movflags +faststart output_with_audio.mp4
Here's the breakdown of the extra options:
-i audio.mp3
→ Adds an audio track. You need to place the audio file in the same directory. If it is in the other directory, mention its explicit path.-c:a aac -b:a 192k
→ Uses AAC audio codec at 192kbps bitrate.-shortest
→ Ensures the video ends when the shorter of the two (audio or video) finishes.
Step 4: Loop Audio if It’s Too Short
If the audio file is shorter than the video, you can make it loop using:
ffmpeg -framerate 5 -i %d.jpg -stream_loop -1 -i audio.mp3 -vf "scale=1920:-2" -c:v libx264 -pix_fmt yuv420p -c:a aac -b:a 192k -shortest -movflags +faststart output_with_looped_audio.mp4
I have been using this command to convert the images to video with audio. FFmpeg has a vast number of options. For more FFmpeg command examples. please visit the following link:
Troubleshooting Tips
1. Video Not Playing in Certain Players?
Some media players (like Parole) may not support the default encoding settings. Try using -profile:v baseline -level 3.0
for better compatibility:
ffmpeg -framerate 5 -i %d.jpg -i audio.mp3 -vf "scale=1920:-2" -c:v libx264 -profile:v baseline -level 3.0 -pix_fmt yuv420p -c:a aac -b:a 192k -shortest -movflags +faststart output.mp4
2. Error: ‘Deprecated Pixel Format Used’
If you see this warning, try specifying a color range manually:
ffmpeg -framerate 5 -i %d.jpg -vf "scale=1920:-2,format=yuv420p" -c:v libx264 -pix_fmt yuv420p -movflags +faststart output.mp4
Conclusion
FFmpeg is an incredibly versatile tool for creating videos from images. Whether you're making slideshows, animations, or time-lapse videos, these FFmpeg commands ensure a smooth workflow with high-quality results.