I. Short Videos: One Frame per Second
For quickly displaying images as snapshots, use this command:
- ffmpeg -r 1 -f concat -safe 0 -i list_of_frames -i AUDIO -vf fps=1 VIDEO
II. Long Videos: Overlay Audio with Multiple Images
This section explains creating a video with images displayed continuously across a longer audio track. We use a 5.57-minute (357-second) audio file and 20 images as an example.
A. Creating list_of_frames.txt
To ensure continuous image display throughout the audio's duration, we create a text file listing images and their display durations. Each image will be shown for approximately 18 seconds. The total image duration (18 seconds/image * 20 images = 360 seconds) slightly exceeds the audio length. This is advantageous as FFmpeg's -shortest option will stop the video when the audio ends.
The list_of_frames.txt file will contain:
file Image1.jpg
duration 18
file Image2.jpg
duration 18
file Image3.jpg
duration 18
...
file Image20.jpg
duration 18
file Image20.jpg
Note: The last image is duplicated to extend the video slightly beyond the audio length.
B. FFmpeg Command for Long Videos
The following command generates the final video:
- ffmpeg -f concat -safe 0 -i list_of_frames.txt -i Audio.wav -vf format=yuv420p -c:v libx264 -c:a aac -shortest Video.mp4
This resulted in a high-quality 180 MB video, roughly equivalent to the combined size of the images and audio.
C. Command Explanation:
-f concat -safe 0 -i list_of_frames.txt: Reads image list and durations from list_of_frames.txt.
-i Audio.wav: Adds the audio file.
-vf format=yuv420p: Sets pixel format for broad player compatibility.
-c:v libx264: Encodes video using the H.264 codec.
-c:a aac: Encodes audio using the AAC codec.
-shortest: Stops the video when the audio ends.
III. Verifying Audio Length
Use this command to determine the exact duration of your audio file in seconds:
- ffprobe -i Audio.wav -show_entries format=duration -v quiet -of csv="p=0"
For combining a single image and audio file, tested commands include:
- ffmpeg -loop 1 -i image.jpg -i input_audio.wav -c:v libx264 -tune stillimage -c:a aac -b:a 192k -pix_fmt yuv420p -shortest output.mp4
A simpler (but potentially less flexible) alternative:
- ffmpeg -loop 1 -i Image.png -i Audio.wav -shortest Video.mp4
(Note: This worked with .png but not .jpg in the original testing). The quality of output can be enhanced with optional parameters.
V. Compressing Large Videos
A. CRF Compression:
Instead of directly setting bitrate, use Constant Rate Factor (CRF) for compression. Start with a higher CRF (lower bitrate, smaller file size) and adjust accordingly:
- ffmpeg -i input.mp4 -c:v libx264 -crf 28 -preset medium output.mp4
-c:v libx264: Specifies the x264 video codec.
-crf 28: Sets the CRF value (adjust between 23-28). Lower values mean higher quality.
-preset medium: Controls encoding speed (adjust to slow or fast).
This is an iterative process: Encode, check the size, adjust CRF, and repeat until satisfied.
B. Alternative Compression using libx265:
Another option, suggested here, may also offer potentially better compression:
- ffmpeg -i input.mp4 -vcodec libx265 -crf 28 output.mp4
VI. Recommendations
- Begin with CRF-based compression, adjusting CRF to balance quality and file size.
- Use bitrate-based commands (see here) only if precise file size control is crucial.
- Employ two-pass encoding for optimal compression if time permits.