SciTech-AV-Video-DVP(Digital Video Processing)-CV/CG-ffmpeg-剪切/格式转换 & 视频拆帧图 & 图片合成视频 & 拼接 & time duration 格式 & 时间 起始点 时长 及 帧数设置
FFmpeg Image/Video/Audio Processing
按帧截图, 图片合成视频, 视频格式转换, 视频拼接:
video_file = 'Cartier.flv'
cmd_probe = "ffprobe -hide_banner -v quiet -print_format json -show_format -show_streams -i %s"
cmd_frame = "ffmpeg -i %s -y -s 1280x960 -ss %s -frames 1 %s"
vinfo =[ i.strip() for i in os.popen3(cmd_probe % video_file)[2].read().splitlines() ]
合成视频流
用 图片序列+音频 合成
MP3:/home/ffmpeg_data/001.mp3、/home/ffmpeg_data/002.mp3
ffmpeg -r 0.5 -i 001.jpg 001.mp3 -vcodec mpeg4 001.mp4
ffmpeg -r 0.5 -i 001.jpg 001.mp3 -vcodec mpeg4 001.mp4
# 或将需要合并的视频文件写入 inputs.txt( 一行一个), 接着执行:
ffmpeg -y -f concat -safe 0 -i inputs.txt -c copy output.mp4
ffprobe提取视频信息
- 取得
JSON格式的视频/音频信息( 帧一级高精度的Stream 字段, 格式字段)
ffprobe -hide_banner -v quiet -print_format json -show_format -show_streams -i VIDEO_FILE/AUDIO_FILE
-count_frames 选项会在结果里加入每个 Stream 总帧数字段信息: "nb_read_frames" ,
但因需要处理每一帧,会影响调用执行的性能;
-show_frames 选项会在结果里加入frames帧信息(所有Stream的所有帧),
但因需要处理每一帧,会影响调用执行的性能;
- 提取
TEXT格式的视频/音频信息:
Abael:flvs abael$ ffprobe -hide_banner -i Cartier.flv
Input #0, flv, from 'Cartier.flv':
Metadata:
major_brand : mp42
minor_version : 0
compatible_brands: isommp42
encoder : Lavf58.12.100
Duration: 00:00:31.45, start: 0.057000, bitrate: 888 kb/s
Stream #0:0: Video: h264 (High), yuv420p(progressive), 1280x720 [SAR 1:1 DAR 16:9], 25 fps, 25 tbr, 1k tbn, 50 tbc
Stream #0:1: Audio: aac (LC), 44100 Hz, stereo, fltp, 128 kb/s
由TEXT格式信息可以取得Duration(时长), start(开始时间), fps(frame per second, 视频 Stream 的帧率).
- 截取视频的1帧图片: 例如在0.0 时间截取一帧图片:
ffmpeg -i /www/data/nginx/rtmp/flvs/CartierAD.flv -y -s 1280x960 -ss 0.0 -frames 1 ./bg.jpg - 拆分出视频的全部帧图片:
ffmpeg -i video.avi frames_%05d.jpg
导出结果 用frame_为前缀,以5位数字为编号(不够前边用0补全),
至于frames_%Nd.jpg保留几位数字长度, 可以先用 ffprobe 得到视频/音频 的总帧数来确定;
裁剪/合并/拼接 视频
-
FFMPEG 裁剪视频:
ffmpeg -ss START -t DURATION -i INPUT -vcodec copy -acodec copy OUTPUT
-vcode copy和-acodec copy 表示使用的视频和音频的编码格式,copy表示原样拷贝视频源的。 -
FFMPEG 合并视频文件:
ffmpeg -i concat:"PART1.mpg|PART2.mpg" -vcodec copy -acodec copy OUTPUT.mpg
PART1:要合并的第一个视频文件名,
PART2:要合并的第二个视频文件名,
OUTPUT:合并后的视频文件名 -
FFMPEG 转换视频格式
ffmpeg -i INPUT -f OUTPUT_FOMRMAT OUTPUT
例如:ffmpeg -i /tmp/a.avi -f mpeg ./result.mpg -
图片 和 音频文件 合成视频:
- 照片(要提前准备好的):
/home/ffmpeg_data/001.jpg、/home/ffmpeg_data/002.jpg - MP3(要提前准备好的):
/home/ffmpeg_data/001.mp3、/home/ffmpeg_data/002.mp3 - 用FFmpeg转换:
ffmpeg -r 0.5 -i 001.jpg 001.mp3 -vcodec mpeg4 001.mp4
也可以将需要合并的视频文件放写在inputs.txt( 一行一个), 接着执行:
ffmpeg -y -f concat -safe 0 -i inputs.txt -c copy output.mp4
- 照片(要提前准备好的):
-
图片 合成 视频:
ffmpeg [-start_number START_NUMBER] -i img/frames_%05d.jpg -vcodec OUTPUT_VIDEO_CODEC test.avi
例如
ffmpeg -i img/frames_%05d.jpg -vcodec mpeg4 test.avi-vodec是视频编码格式,所有ffmpeg支持的格式都可以,具体可以ffmpeg -codecs查看。-i是输入文件名,上例意为读取img文件夹下文件名以frame_ 为前缀而且后接五位数字的jpg 图片序列;- 报错解决: Could find no file with path ‘img/frames_%05d.jpg’ and index in the range 0-4
这是因为ffmpeg默认图片编号是从0开始的。即如果找不到frames_00000.jpg则报错。
指定读取图片文件名的起始编号为:
ffmpeg -start_number 345 -i img/frames_%05d.jpg -vcodec mpeg4 test.avi - 注意:
- 读取图片文件序列时会
顺序读取,若出现编号中断,视频会就此结束。 - 默认
图片文件名从0开始编号读取。如不存在frames_00000.jpg文件则报错. - 还可以通过:
-r设置fps(frames per second, 帧率), 即每秒提取或集成几帧图片。-y若输出文件名冲突,直接覆盖。
参考 "Clarification for ffmpeg input option with image files as input"
- 读取图片文件序列时会
FFmpeg CommandLine Arguments and Parameters
Video options:
-vframes number **set the number of video frames to output**
-r rate **set frame rate (Hz value, fraction or abbreviation)**
-fpsmax rate **set max frame rate (Hz value, fraction or abbreviation)**
-s size **set frame size (WxH or abbreviation)**
-aspect aspect **set aspect ratio** (4:3, 16:9 or 1.3333, 1.7777)
-display_rotation angle **set pure counter-clockwise rotation in degrees for stream(s)**
-display_hflip **set display horizontal flip for stream(s)** (overrides any display rotation if it is not set)
-display_vflip **set display vertical flip for stream(s)** (overrides any display rotation if it is not set)
-vn **disable video**
-vcodec codec **force video codec** ('copy' to copy stream)
-timecode hh:mm:ss[:;.]ff **set initial TimeCode value**.
-pass n **select the pass number** (1 to 3)
-vf filter_graph **set video filters**
-b bitrate **video bitrate (please use -b:v)**
-dn **disable data**
Subtitle options:
-s size set frame size (WxH or abbreviation)
-sn disable subtitle
-scodec codec force subtitle codec ('copy' to copy stream)
-stag fourcc/tag force subtitle tag/fourcc
-fix_sub_duration fix subtitles duration
-canvas_size size set canvas size (WxH or abbreviation)
-spre preset set the subtitle options to the indicated preset
FFmpegTime Duration
FFmpeg时间段( time duration )格式,两种:
- [-][
:] : [. ...]
HH 小时, MM 分钟(最大两位数), SS 秒(最多两位数),
m 小数秒(高精度, 十进制); - [-]<S>+[.<m>...]
S 秒数, m 作小数秒(高精度, 十进制);;
上两种duration, 都可选前加-指示negative duration.
例如:
-13.567: **negative** 13.567 seconds
12:03:45: 12hours 03minutes 45 seconds
23.189: 23.189 seconds
00:01:00: 60 seconds
60: 60 seconds
FFmpegGlobal options
affect whole program instead of just one file:
-loglevel loglevel set logging level-v loglevel **set logging level**-report generate a report-max_alloc bytes **set maximum size of a single allocated block**-y **overwrite output files**-n never overwrite output files-ignore_unknown Ignore unknown stream types-filter_threads **number of non-complex filter threads**-filter_complex_threads **number of threads for -filter_complex**-stats print progress report during encoding-max_error_rate maximum error rate ratio of decoding errors (0.0: no errors, 1.0: 100% errors) above which ffmpeg returns an error instead of success.-frames[:stream_specifier] framecount (output,per-stream)
指定产出帧数: 设置产出视频的帧数 framecount .-f fmt (input/output)
指定文件格式:- 导入:会自动检测导入格式;
- 产出:文件扩展名自动推导产出格式 ( 因此这个 -f fmt 选项只在必要时使用. )
- FFmpeg 支持的所有 fmt 格式可以查看执行:
ffmpeg -formats
-ss position (input/output)
指定时间起点:- 在
-i参数的前面是作为导入设置, 从导入文件快进到指定position.
注️意:- 多数文件不真正支持seek, ffmpeg 会快进到 position 之前
最接近的seek point. - 转码(
transcoding)时并启用选项-accurate_seek(默认), 则解码并丢弃 前置seek point 和 position 之间的帧. - 流复制(
stream copy) 时并启用选项-noaccurate_seek(默认), 则保留seek point 和 position 之间的帧.
- 多数文件不真正支持seek, ffmpeg 会快进到 position 之前
- 在
-i参数的后面是作为产出选项(放在 output url 之前);
解码读入文件并丢弃导入, 直到产出流的 timestamp 到达这个指定的 position.
- 在
-sseof position (input/output):
类似 "-ss" 选项,但时间点相对于eof(end of file). 0 表示 EOF, 负数表示文件的stream上.-to position (input/output),
指定时间终点.- 在 写产出文件 / 读导入文件
到达指定时间终点 position后停止. ( ffmpeg Time duration 格式) -to和-t两个选项只能两选一,且-t的优先级更高.
- 在 写产出文件 / 读导入文件
-t duration (input/output)
指定时长.- 在 "-i" 的前面,作为导入设置, 指定只从导入文件读取的数据时间长度.
- 在 "-i" 的后面(output url前),作为产出设置, 指定只写指定时长的数据,就停止.
-to和-t两个选项只能两选一,且-t的优先级更高。
-fs limit_size (output)
Set thefile size limit, expressedin bytes.
No further chunk of bytes is writtenafterthe limit is exceeded.
Thesize of the output fileisslightly morethanthe requested file size.-itsoffset offset (input)
指定导入时间偏移.offset为ffmpeg time duration 格式
offset被添加到导入文件的timestamps(时间戳).
指定positive offset表示 streams 被 delayed 到指定offset的时间.-timestamp date (output)
指定录制时间戳
在 container(数据容器, 输出stream到存储的格式容器)上 设置录制timestamp.
FFmpegPer-file main options:
-f fmt **force format**
-c codec **codec name**
-codec codec codec name
-pre preset preset name
-map_metadata outfile[,metadata]:infile[,metadata] **set metadata information of outfile from infile**
-t duration **record or transcode "duration" seconds of audio/video**
-to time_stop **record or transcode stop time**
-fs limit_size **set the limit file size in bytes**
-ss time_off **set the start time offset**
-sseof time_off **set the start time offset relative to EOF**
-seek_timestamp enable/disable seeking by timestamp with -ss
-timestamp time set the recording timestamp ('now' to set the current time)
-metadata string=string **add metadata**
-program title=string:st=number... add program with specified streams
-target type specify target file type ("vcd", "svcd", "dvd", "dv" or "dv50" with optional prefixes "pal-", "ntsc-" or "film-")
-apad audio pad
-frames number **set the number of frames to output**
-filter filter_graph **set stream filtergraph**
-filter_script filename **read stream filtergraph description from a file**
-reinit_filter **reinit filtergraph on input parameter changes**
-discard discard
-disposition disposition
FFmpeg常用到PCM格式
DE s16be PCM signed 16-bit big-endian
DE s16le PCM signed 16-bit little-endian
DE s24be PCM signed 24-bit big-endian
DE s24le PCM signed 24-bit little-endian
DE s32be PCM signed 32-bit big-endian
DE s32le PCM signed 32-bit little-endian
DE s8 PCM signed 8-bit
DE f32be PCM 32-bit floating-point big-endian
DE f32le PCM 32-bit floating-point little-endian
DE f64be PCM 64-bit floating-point big-endian
DE f64le PCM 64-bit floating-point little-endian
DE mulaw PCM mu-law
DE u16be PCM unsigned 16-bit big-endian
DE u16le PCM unsigned 16-bit little-endian
DE u24be PCM unsigned 24-bit big-endian
DE u24le PCM unsigned 24-bit little-endian
DE u32be PCM unsigned 32-bit big-endian
DE u32le PCM unsigned 32-bit little-endian
DE u8 PCM unsigned 8-bit
ffmpeg 的解码编码格式
Common Pitfalls
- Dimensions must be even.
H.264 requires both width and height to be divisible by 2.
A 641x481 image will fail with height not divisible by 2. Fix it by scaling:
ffmpeg -framerate 1 -i frame_%03d.png -c:v libx264 -pix_fmt yuv420p -vf "scale=trunc(iw/2)*2:trunc(ih/2)*2" -r 30 output.mp4 - Mixed image sizes break the output.
If your images have different dimensions,
FFmpeg uses the first image's size and distorts the rest.
Normalize everything to a consistent resolution:
-vf "scale=1920:1080:force_original_aspect_ratio=decrease,pad=1920:1080:(ow-iw)/2:(oh-ih)/2"
This scales each image to fit within 1920x1080 while preserving its aspect ratio, then pads with black bars. - Alpha channels in PNGs cause playback failures.
PNGs with transparency produce yuv444p output by default,
which Chrome, Safari, and most mobile players can't decode.
The-pix_fmt yuv420pflag fixes this by stripping the alpha channel,
and converting to a universally compatible format. Don't skip it. - Numbering gaps skip images.
If your files jump from img_003.png to img_005.png, FFmpeg stops at the gap.
Either rename to fill the gap, or switch to a glob pattern. - Framerate confusion: input vs output.
-framerate(before-i) controls how many images FFmpeg reads per second.
-r(after-i) controls the output file's frame rate.
They're not the same thing.
If you set-framerate 1 -r 30,
each image displays for 1 second,
but the output contains 30 duplicate frames per image for smooth playback.
FAQ
What image formats does FFmpeg support for video creation?
FFmpeg reads JPEG, PNG, BMP, TIFF, WebP, and most standard image formats. JPEG is fastest to decode. PNG preserves sharp edges for screenshots and text. Use PNG for UI mockups and screenshots, JPEG for photographs.
How do I control how long each image appears in the video?
Set -framerate before the input. -framerate 1 means 1 image per second. -framerate 1/3 means each image displays for 3 seconds. For different durations per image, create individual clips with -loop 1 -t
Can I add transitions between images?
Yes. FFmpeg's xfade filter supports over 40 transition styles including fade, dissolve, wipe, and slide. See the slideshow from images guide for detailed xfade examples with multiple images.
Is there a maximum number of images FFmpeg can process?
No hard limit in FFmpeg itself, but memory usage scales with image resolution and count. For thousands of large images, process in batches of 100-200 and concatenate the resulting clips. FFmpeg Micro's API accepts up to 10 images per request.
How do I add background audio to an image video?
Add the audio file as a separate input and use -shortest to cut when the video ends: ffmpeg -framerate 1 -i img_%03d.png -i music.mp3 -c:v libx264 -c:a aac -pix_fmt yuv420p -shortest output.mp4.
Last verified: 2026-05-28 against FFmpeg 3.3.3 and FFmpeg Micro production API.
About Javid Jamae
Founder & CEO at FFmpeg Micro
Javid is a software engineer, author, and entrepreneur with over 25 years of professional software development experience across enterprise, startup, and consulting environments. He founded FFmpeg Micro to make video processing accessible to developers through a simple, automation-first REST API.

浙公网安备 33010602011771号