SciTech-AV-Video-DVP(Digital Video Processing)-CV/CG-ffmpeg-图片+音频合成视频 & 拼接 & time duration 格式 & 时间 起始点 时长 及 帧数设置

SciTech-AV-Video-DVP(Digital Video Processing)-CV/CG-ffmpeg-图片+音频合成视频 & 拼接 & time duration 格式 & 时间 起始点 时长 及 帧数设置

图片+音频合成视频


Using ffmpeg to convert a set of images into a video

Apr 5, 2016, Original 2012-11-16, Updated 2016-04-05: cleanup and information about overlaying images.


When using ffmpeg to compress a video, I recommend using the libx264 codec,
from experience it has given me excellent quality for small video sizes.
I have noticed that different versions of ffmpeg will produce different output file sizes, so your mileage may vary.


Take a list of images

To take a list of images that are padded with zeros (pic0001.png, pic0002.png…. etc),
use the following command:

ffmpeg -r 60 -f image2 -s 1920x1080 -i pic%04d.png -vcodec libx264 -crf 25 -pix_fmt yuv420p test.mp4

where:

  • -y overwrite If output file is existing.
  • -vf "fps=25" specify The video framerate
  • -loop 1 loop infinitely, -loop 0 loop one pass.
  • -t time length.
  • the %04d means that zeros will be padded until the length of the string is 4 i.e 0001…0020…0030…2000 and so on. If no padding is needed use something similar to pic%d.png or %d.png.
  • -r is the framerate (fps)
  • -crf is the quality, lower means better quality, 15-25 is usually good
  • -s is the resolution
  • -pix_fmt yuv420p specifies the pixel format, change this as needed
  • the file will be output (in this case) to: test.mp4

Specifying start and end frames

ffmpeg -r 60 -f image2 -s 1920x1080 -start_number 1 -i pic%04d.png -vframes 1000 -vcodec libx264 -crf 25 -pix_fmt yuv420p test.mp4

  • -start_number specifies what image to start at
  • -vframes 1000 specifies the number frames/images in the video

Overlaying image on video

Assuming that you have an overlay image that is the same size as the video, you can use the following command to add it during the ffmpeg compression process.

ffmpeg -r 60 -f image2 -s 1920x1080 -i pic%04d.png -i ~/path_to_overlay.png -filter_complex "[0:v][1:v] overlay=0:0" -vcodec libx264 -crf 25 -pix_fmt yuv420p test_overlay.mp4
~/path_to_overlay.png is the full/relative path to the overlay image
[0:v][1:v] joins the two video streams together, stream 1 is the set of images, stream 2 is the overlay file
overlay=0:0 specifies the position of the overlay, in this case the overlay image is assumed to be the same size as the video so no offset is needed. The offset is specified as overlay=x:y where x is the x offset in pixels and y is the y offset in pixels
You can use this technique to overlay multiple files on top of each other, or even have a dynamic overlay. -filter_complex is a really flexible command and can do much much more than is shown here. See the ffmpeg filters documentation for more information.


Adding a mp3 to a video

Adding sound to a video is straightforward

ffmpeg -r 60 -f image2 -s 1280x720 -i pic%05d.png -i MP3FILE.mp3 -vcodec libx264 -b 4M -vpre normal -acodec copy OUTPUT.mp4

  • -i MP3FILE.mp3 The audio filename
  • -acodec copy Copies the audio from the input stream to the output stream

Converting a video to mp4 from a different format

If the video has already been compressed the following can be used to change the codmpression to h264:
ffmpeg -i INPUT.avi -vcodec libx264 -crf 25 OUTPUT.mp4


Playback Issues for Quicktime/Other Codecs

Quicktime and some other codecs have trouble playing certain pixel formats such as 4:4:4 Planar and 4:2:2 Planar while 4:2:0 seems to work fine

  • Add the following flag to force the pixel format:
    -pix_fmt yuv420p
  • Finer Bitrate control (to control size and quality)
    -b 4M
    you can use the -b flag to specify the target bitrate, in this case it is 4 megabits per second
  • Using -vpre with a setting file
    -vpre normal
    -vpre is the quality setting, better quality takes longer to encode,
    some alternatives are: default, normal, hq, max.
    Note that the -vpre command only works if the corresponding setting file is available.

© Hammad Mazhar 2016



How to Create a Video from Images with FFmpeg (and Add Audio)

A Beginners Guide to Create Slideshow with Audio using FFmpeg in Linux

Written by sk Published: March 26, 2025


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.


Table of Contents

  • Why Use FFmpeg for Image to Video Conversion?
  • Step 1: Prepare Your Images
  • Step 2: Create a Video from Images with FFmpeg
  • Step 3: Add Background Audio
  • Step 4: Loop Audio if It’s Too Short
  • Troubleshooting Tips
    1. Video Not Playing in Certain Players?
    2. Error: ‘Deprecated Pixel Format Used’
  • Conclusion

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:

20+ FFmpeg Commands For Beginners

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 incredibly versatile 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.


Author

Senthilkumar Palani (aka SK) is the Founder and Editor in chief of OSTechNix.
He is a Linux/Unix enthusiast and FOSS supporter. He lives in Tamilnadu, India.


posted @ 2025-08-07 09:37  abaelhe  阅读(18)  评论(0)    收藏  举报