ffmpeg基本功能使用

任务描述:由给定图像序列合成 24fps 视频

方案一

直接对图像进行操作,适用于图像名比较规范且默认即为所需顺序

ffmpeg -f image2 -i ./images_crop_%d.png -pix_fmt yuv420p -vcodec libx264 -r 24 -y output.mp4

方案二

将图像顺序写入一个文件列表,让ffmpeg从列表读入并合成视频,适用于自定义图像顺序

ffmpeg -f concat -safe 0 -i input.txt -pix_fmt yuv420p -vcodec libx264 -r 24 -y -an output.mp4

其中列表格式如下

file '/Users/cv/playground/data/cam_1_frame_00.png'
duration 0.5
file '/Users/cv/playground/data/cam_2_frame_01.png'
duration 0.5
……
input.txt

file 设置当前帧图像路径,使用绝对路径防止出错。

duration 设置当前帧与下一帧的时间间隔,单位是秒(s)。

可能出现的错误

如果图像的宽或高不是偶数,在转换过程中会出现下面的问题

[libx264 @ 0x7fec87003c00] height not divisible by 2 (2000x1125)
Error initializing output stream 0:0 -- Error while opening encoder for output stream #0:0 - maybe incorrect parameters such as bit_rate, rate, width or height
Conversion failed!
Error Occur

解决方案

在转换过程中对宽和高重新调整

ffmpeg -f concat -safe 0 -i input.txt -pix_fmt yuv420p -vcodec libx264 -r 24 -y -an -vf "scale=trunc(iw/2)*2:trunc(ih/2)*2" output.mp4

任务描述:将视频拆分成图像序列

直接输入需要分解的视频名,并指定输出图像的格式即可

ffmpeg -i video.mpg image%d.jpg
ffmpeg -i scene2frame1.mov frame%d.png

任务描述:将 YUV420P10LE 格式的图片/视频转换成 YUV420P 格式

先查看原图格式

cv:~ cv$ ffprobe -show_format cam-1-frame-1_yuv420ple.png 
ffprobe version 4.2.1 Copyright (c) 2007-2019 the FFmpeg developers
  built with Apple clang version 11.0.0 (clang-1100.0.33.8)
  configuration: --prefix=/usr/local/Cellar/ffmpeg/4.2.1_2 --enable-shared --enable-pthreads --enable-version3 --enable-avresample --cc=clang --host-cflags='-I/Library/Java/JavaVirtualMachines/adoptopenjdk-13.jdk/Contents/Home/include -I/Library/Java/JavaVirtualMachines/adoptopenjdk-13.jdk/Contents/Home/include/darwin -fno-stack-check' --host-ldflags= --enable-ffplay --enable-gnutls --enable-gpl --enable-libaom --enable-libbluray --enable-libmp3lame --enable-libopus --enable-librubberband --enable-libsnappy --enable-libtesseract --enable-libtheora --enable-libvidstab --enable-libvorbis --enable-libvpx --enable-libx264 --enable-libx265 --enable-libxvid --enable-lzma --enable-libfontconfig --enable-libfreetype --enable-frei0r --enable-libass --enable-libopencore-amrnb --enable-libopencore-amrwb --enable-libopenjpeg --enable-librtmp --enable-libspeex --enable-libsoxr --enable-videotoolbox --disable-libjack --disable-indev=jack
  libavutil      56. 31.100 / 56. 31.100
  libavcodec     58. 54.100 / 58. 54.100
  libavformat    58. 29.100 / 58. 29.100
  libavdevice    58.  8.100 / 58.  8.100
  libavfilter     7. 57.100 /  7. 57.100
  libavresample   4.  0.  0 /  4.  0.  0
  libswscale      5.  5.100 /  5.  5.100
  libswresample   3.  5.100 /  3.  5.100
  libpostproc    55.  5.100 / 55.  5.100
[png_pipe @ 0x7fc1b5807800] Stream #0: not enough frames to estimate rate; consider increasing probesize
Input #0, png_pipe, from 'cam-1-frame-1_yuv420ple.png':
  Duration: N/A, bitrate: N/A
    Stream #0:0: Video: png, rgb48be(pc), 3840x2160 [SAR 1:1 DAR 16:9], 25 tbr, 25 tbn, 25 tbc
[FORMAT]
filename=cam-1-frame-1_yuv420ple.png
nb_streams=1
nb_programs=0
format_name=png_pipe
format_long_name=piped png sequence
start_time=N/A
duration=N/A
size=35370650
bit_rate=N/A
probe_score=99
[/FORMAT]

然后使用 ffmpeg 转换格式

cv:~ cv$ ffmpeg -pix_fmt yuv420p10le -i cam-1-frame-1.png -pix_fmt yuv420p cam-1-frame-1_yuv420p.png

然后查看转换后的图像的格式

cv:~ cv$ ffprobe -show_format cam-1-frame-1.png 
ffprobe version 4.2.1 Copyright (c) 2007-2019 the FFmpeg developers
  built with Apple clang version 11.0.0 (clang-1100.0.33.8)
  configuration: --prefix=/usr/local/Cellar/ffmpeg/4.2.1_2 --enable-shared --enable-pthreads --enable-version3 --enable-avresample --cc=clang --host-cflags='-I/Library/Java/JavaVirtualMachines/adoptopenjdk-13.jdk/Contents/Home/include -I/Library/Java/JavaVirtualMachines/adoptopenjdk-13.jdk/Contents/Home/include/darwin -fno-stack-check' --host-ldflags= --enable-ffplay --enable-gnutls --enable-gpl --enable-libaom --enable-libbluray --enable-libmp3lame --enable-libopus --enable-librubberband --enable-libsnappy --enable-libtesseract --enable-libtheora --enable-libvidstab --enable-libvorbis --enable-libvpx --enable-libx264 --enable-libx265 --enable-libxvid --enable-lzma --enable-libfontconfig --enable-libfreetype --enable-frei0r --enable-libass --enable-libopencore-amrnb --enable-libopencore-amrwb --enable-libopenjpeg --enable-librtmp --enable-libspeex --enable-libsoxr --enable-videotoolbox --disable-libjack --disable-indev=jack
  libavutil      56. 31.100 / 56. 31.100
  libavcodec     58. 54.100 / 58. 54.100
  libavformat    58. 29.100 / 58. 29.100
  libavdevice    58.  8.100 / 58.  8.100
  libavfilter     7. 57.100 /  7. 57.100
  libavresample   4.  0.  0 /  4.  0.  0
  libswscale      5.  5.100 /  5.  5.100
  libswresample   3.  5.100 /  3.  5.100
  libpostproc    55.  5.100 / 55.  5.100
[png_pipe @ 0x7fa067802a00] Stream #0: not enough frames to estimate rate; consider increasing probesize
Input #0, png_pipe, from 'cam-1-frame-1.png':
  Duration: N/A, bitrate: N/A
    Stream #0:0: Video: png, rgb24(pc), 3840x2160 [SAR 1:1 DAR 16:9], 25 tbr, 25 tbn, 25 tbc
[FORMAT]
filename=cam-1-frame-1.png
nb_streams=1
nb_programs=0
format_name=png_pipe
format_long_name=piped png sequence
start_time=N/A
duration=N/A
size=11496668
bit_rate=N/A
probe_score=99
[/FORMAT]

任务描述:两路/多路视频拼接,同时播放

ffmpeg -i test_1.mp4 -i test_2.mp4 -filter_complex "[0:v]pad=iw*2:ih[a];[a][1:v]overlay=w:0" -pix_fmt yuv420p -y output_result.mp4

从前往后参数的含义分别是:

-i test_1.mp4 指定输入文件的名字,即打算放在左侧播放的视频1

-i test_2.mp4 指定输入文件的名字,及打算放在右侧播放的视频2

-filter_complex "[0:v]pad=iw*2:ih[a];[a][1:v]overlay=w:0"

指的是复杂滤波器的设置。

其中[0:v] [1:v]表示输入的第一个和第二个编号。

pad 用于边界扩充,iw/ih分别是输入视频的宽度和高度。

[0:v]pad=iw*2:ih[a] 表示将第一个输入视频边界扩充,并将扩充好的命名为 [a],方便后续操作。

[a][1:v]overlay=w*1 中的 [a][1:v] 表示将 [1:v] 叠加到 [a] 上去,并且位置从 w 开始,默认是 width=w 处,h未写表示 0。

同理可得三个视频水平并列放置的命令

ffmpeg -i test_1.mp4 -i test_2.mp4 -i test_3.mp4 -filter_complex "[0:v]pad=iw*3:ih[a];[a][1:v]overlay=w[b];[b][2:v]overlay=w*2:0" -pix_fmt yuv420p -y output_result.mp4

任务描述:对给定视频在指定位置添加文字水印

ffmpeg -i ctest.mp4 -vf "drawtext=fontfile=simhei.ttf: text='Original':x=620:y=1920:fontsize=72:fontcolor=yellow:shadowy=2" -y test_dst.mp4

其中:

fontfile 表示字体类型,要确保存在对应的字体库

text 表示要添加的字符串形式的文字内容

fontsize 用来设置字体大小,默认大小为 16

fontcolor 用来设置字体颜色,默认为 Black

x=620:y=1920 表示文字水印放置的位置

如果有多个水印需要放置,中间用逗号隔开即可。

ffmpeg -i test.mp4 -vf "drawtext=fontfile=simhei.ttf: text='Original':x=520:y=1950:fontsize=72:fontcolor=yellow:shadowy=2,drawtext=fontfile=simhei.ttf: text='DaVinci Resolve':x=1770:y=1950:fontsize=72:fontcolor=yellow:shadowy=2, drawtext=fontfile=simhei.ttf: text='Ours':x=3320:y=1950:fontsize=72:fontcolor=yellow:shadowy=2"  -y test_dst.mp4

其它常用功能

1. 获取视频格式信息

ffmpeg -i video.avi

2. 把视频的前30帧转换成一个Animated Gif

ffmpeg -i test.asf -vframes 30 -y -f gif a.gif

参考资料

[1] 每天学习一个命令:ffprobe 查看多媒体信息 http://einverne.github.io/post/2015/02/ffprobe-show-media-info.html

[2] ffprobe,ffplay ffmpeg常用的命令行命令 https://juejin.im/post/5a59993cf265da3e4f0a1e4b

[3] ffmpeg 10bit 8bit yuv格式转换 https://blog.csdn.net/baoyongshuai1509/article/details/83927538

[4] YUV420P格式分析 https://blog.csdn.net/yhc166188/article/details/81016916

[5] FFmpeg命令行实现两路/多路视频拼接 合并 合成 同时播放 https://blog.csdn.net/a386115360/article/details/89465633

[6] https://ffmpeg.org/ffmpeg-filters.html#drawtext-1

原文地址:https://www.cnblogs.com/phillee/p/12244970.html