Keywords: FFmpeg | video_creation | image_sequence | multi-folder_processing | concat_protocol
Abstract: This article provides a comprehensive exploration of using FFmpeg to create videos from images stored in different folders, focusing on the -f concat and -pattern_type glob methods. It covers input path specification, frame rate control, video encoding parameters, and common issue resolution through practical command examples and in-depth technical analysis.
Introduction
FFmpeg, as a powerful multimedia processing tool, efficiently converts image sequences into video files. In practical applications, users often need to select images from different folders to create videos, which involves complex path management and file organization challenges. This article systematically introduces multiple implementation methods based on high-scoring Stack Overflow answers and official documentation.
Basic Command Structure Analysis
The fundamental FFmpeg image-to-video command includes several key parameters: -framerate controls the reading speed of input images, -i specifies the input file pattern, -c:v sets the video encoder, and -r defines the output video frame rate. When processing images from different folders, traditional positional parameter methods face path resolution challenges.
Using Concat Protocol for Multi-Folder Images
FFmpeg's concat protocol provides a systematic solution for integrating images from multiple folders. First, create a text file (e.g., imagepaths.txt) listing all required image file paths:
file 'E:\images\folder1\img%03d.jpg'
file 'E:\images\folder2\img%03d.jpg'Then execute the following command:
ffmpeg -y -r 1/5 -f concat -safe 0 -i "imagepaths.txt" -c:v libx264 -vf "fps=25,format=yuv420p" "output.mp4"The -safe 0 parameter is crucial here, as it disables path safety checks, allowing processing of Windows paths containing special characters. -f concat instructs FFmpeg to use the concat demuxer, enabling sequential connection of multiple input sources.
Advanced Applications of Glob Pattern
For non-sequentially named images within a single folder, -pattern_type glob offers a convenient solution:
ffmpeg -framerate 1 -pattern_type glob -i '*.png' -c:v libx264 -r 30 -pix_fmt yuv420p output.mp4The glob pattern supports wildcard matching but has limitations: it cannot search across folders, and matching patterns are affected by file extensions. Testing shows that -i '*' fails due to inability to infer file types.
Deep Principles of Frame Rate Control
FFmpeg involves two types of frame rate settings: input frame rate (-framerate) and output frame rate (-r). In slideshow scenarios, setting -framerate 1 means displaying one image per second, while -r 30 ensures the output video meets standard playback requirements. When input and output frame rates mismatch, FFmpeg achieves rate conversion through frame repetition or dropping.
Optimized Configuration of Video Filter Chains
Video filters (-vf) provide more flexible processing capabilities than standalone parameters. Combining fps=25,format=yuv420p not only implements frame rate control but also ensures color format compatibility:
ffmpeg -r 1/5 -i img%03d.png -c:v libx264 -vf "fps=25,format=yuv420p" output.mp4The order of filter chains affects processing results; placing format after fps avoids color space conversion errors.
Path Handling and Cross-Platform Compatibility
Special attention is needed for backslashes in Windows paths. Using C:\mypics\img%03d.png directly in commands may cause parsing errors; it's recommended to wrap paths in quotes or use forward slashes:
ffmpeg -r 1/5 -i "C:/mypics/img%03d.png" -c:v libx264 -r 30 output.mp4For paths containing spaces, quotes are essential to prevent the Shell from splitting the path into multiple parameters.
Error Diagnosis and Solutions
The commonly reported issue of "only the first frame being generated" typically stems from path resolution errors or improper frame rate settings. Replacing simple -r parameters with -vf fps=25 can resolve player compatibility issues:
ffmpeg -r 1/5 -i img%03d.png -c:v libx264 -vf fps=25 -pix_fmt yuv420p output.mp4Players like VLC have known bugs with low-frame-rate videos; forcing output frame rates to 25-30 fps ensures smooth playback.
Performance Optimization and Quality Control
The libx264 encoder combined with yuv420p pixel format achieves the best balance between file size and video quality. Further optimization can be achieved by adjusting the CRF (Constant Rate Factor) parameter: lower CRF values (e.g., 18) improve quality but increase file size, while higher values (e.g., 23) reduce size but may lose detail.
Expansion of Practical Application Scenarios
Combining with audio addition functionality enables creation of complete slide presentations:
ffmpeg -framerate 1 -pattern_type glob -i '*.png' -i audio.mp3 -c:a copy -shortest -c:v libx264 -r 30 output.mp4-shortest ensures the video terminates when the shorter of the image or audio streams ends, avoiding blank frames or audio truncation.
Conclusion
FFmpeg offers multiple flexible methods for processing image sequences from different folders. The concat protocol is suitable for precise control of multi-source inputs, the glob pattern simplifies single-folder operations, and video filter chains enhance processing flexibility. Correct understanding of path resolution mechanisms and frame rate control principles is key to successful implementation.