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

方案一

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

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

方案二

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

ffmpeg -f concat -safe  -i input.txt -pix_fmt yuv420p -vcodec libx264 -r  -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  (2000x1125)
Error initializing output stream : -- Error while opening encoder for output stream #: - maybe incorrect parameters such as bit_rate, rate, width or height
Conversion failed!

Error Occur

解决方案

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

ffmpeg -f concat -safe  -i input.txt -pix_fmt yuv420p -vcodec libx264 -r  -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--frame-1_yuv420ple.png
ffprobe version 4.2. Copyright (c) - the FFmpeg developers
built with Apple clang version 11.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 . 31.100 / . 31.100
libavcodec . 54.100 / . 54.100
libavformat . 29.100 / . 29.100
libavdevice . 8.100 / . 8.100
libavfilter . 57.100 / . 57.100
libavresample . . / . .
libswscale . 5.100 / . 5.100
libswresample . 5.100 / . 5.100
libpostproc . 5.100 / . 5.100
[png_pipe @ 0x7fc1b5807800] Stream #: not enough frames to estimate rate; consider increasing probesize
Input #, png_pipe, from 'cam-1-frame-1_yuv420ple.png':
Duration: N/A, bitrate: N/A
Stream #:: Video: png, rgb48be(pc), 3840x2160 [SAR : DAR :], tbr, tbn, tbc
[FORMAT]
filename=cam--frame-1_yuv420ple.png
nb_streams=
nb_programs=
format_name=png_pipe
format_long_name=piped png sequence
start_time=N/A
duration=N/A
size=
bit_rate=N/A
probe_score=
[/FORMAT]

然后使用 ffmpeg 转换格式

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

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

cv:~ cv$ ffprobe -show_format cam--frame-.png
ffprobe version 4.2. Copyright (c) - the FFmpeg developers
built with Apple clang version 11.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 . 31.100 / . 31.100
libavcodec . 54.100 / . 54.100
libavformat . 29.100 / . 29.100
libavdevice . 8.100 / . 8.100
libavfilter . 57.100 / . 57.100
libavresample . . / . .
libswscale . 5.100 / . 5.100
libswresample . 5.100 / . 5.100
libpostproc . 5.100 / . 5.100
[png_pipe @ 0x7fa067802a00] Stream #: not enough frames to estimate rate; consider increasing probesize
Input #, png_pipe, from 'cam-1-frame-1.png':
Duration: N/A, bitrate: N/A
Stream #:: Video: png, rgb24(pc), 3840x2160 [SAR : DAR :], tbr, tbn, tbc
[FORMAT]
filename=cam--frame-.png
nb_streams=
nb_programs=
format_name=png_pipe
format_long_name=piped png sequence
start_time=N/A
duration=N/A
size=
bit_rate=N/A
probe_score=
[/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  -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

ffmpeg基本功能使用的更多相关文章

  1. Unity 利用FFmpeg实现录屏、直播推流、音频视频格式转换、剪裁等功能

    目录 一.FFmpeg简介. 二.FFmpeg常用参数及命令. 三.FFmpeg在Unity 3D中的使用. 1.FFmpeg 录屏. 2.FFmpeg 推流. 3.FFmpeg 其他功能简述. 一. ...

  2. FFmpeg滤镜实现区域视频增强 及 D3D实现视频播放区的拉大缩小

    1.区域视频增强 FFmpeg滤镜功能十分强大,用滤镜可以实现视频的区域增强功能. 用eq滤镜就可以实现亮度.对比度.饱和度等的常用视频增强功能. 推荐两篇写得不错的博文: (1)ffmpeg综合应用 ...

  3. WebRTC VideoEngine超详细教程(三)——集成X264编码和ffmpeg解码

    转自:http://blog.csdn.net/nonmarking/article/details/47958395 本系列目前共三篇文章,后续还会更新 WebRTC VideoEngine超详细教 ...

  4. 转:Nginx RTMP 功能研究

    看点: 1.    Nginx 配置信息与使用.  (支持 rtmp与HLS配置) 2.    有ffmpeg 编译与使用,    命令行方式来测试验证客户端使用. 转自:http://blog.cs ...

  5. FFMPEG视音频解码【一】

    多媒体的时代,得多了解点编解码的技术才行,而ffmpeg为我们提供了一系列多媒体编解码的接口,如何用好这些接口达到自己所需要的目的,这也是一门重要的学问. 要是了解得不够,总是会遇到一堆又一堆问题:网 ...

  6. FFmpeg详解

    认识FFMPEG FFMPEG堪称自由软件中最完备的一套多媒体支持库,它几乎实现了所有当下常见的数据封装格式.多媒体传输协议以及音视频编解码器.因此,对于从事多媒体技术开发的工程师来说,深入研究FFM ...

  7. Ubuntu下,在Eclipse中使用JNI调用ffmpeg

    Android的应用层开发大部分还是采用JAVA,如果想使用ffmpeg库,就必须利用JNI,使得Java可以调用C/C++的库. JNI其实就是定义的一个转接接口,可以让Java的代码调用C/C++ ...

  8. 视频编辑SDK---我们只提供API,任你自由设计炫酷的功能

    面对相对复杂的视频编辑处理技术,你是否束手无策? 在短视频应用中,有一定技术难度的视频编辑技术中,我们提出了一种全新的解决方法:画板和画笔.短视频处理,用画板和画笔,就够了! 我们设计了极其简单易懂的 ...

  9. 搭建rtmp直播流服务之3:java开发ffmpeg实现rtsp转rtmp并实现ffmpeg命令的接口化管理架构设计及代码实现

    上一篇文章简单介绍了java如何调用ffmpeg的命令:http://blog.csdn.net/eguid_1/article/details/51777716 上上一篇介绍了nginx-rtmp服 ...

随机推荐

  1. supervisor管理tomcat

    操作目的:用supervisor工具管理tomcat服务 配置环境,安装服务,以及多实例 脚本编辑: 前提 机器的opt目录下必须有jdk-8u131-linux-x64_.rpm 以及apache- ...

  2. HDU_1166_树状数组

    http://acm.hdu.edu.cn/showproblem.php?pid=1166 树状数组入门题. #include<iostream> #include<cstring ...

  3. HDU6395 Sequence(矩阵快速幂+数论分块)

    题意: F(1)=A,F(2)=B,F(n)=C*F(n-2)+D*F(n-1)+P/n 给定ABCDPn,求F(n) mod 1e9+7 思路: P/n在一段n里是不变的,可以数论分块,再在每一段里 ...

  4. 《Python学习手册 第五版》 -第5章 数值类型

    本章是承接第四章整体说明之后,将对”数值类型“展开详细的说明 数值类型这一章主要通过一下几个内容来讲解: 1.数值类型有哪些? 2.表达式运算符:有哪些?有什么规范? 3.数值的显示格式 接下来,从第 ...

  5. (二)maven依赖,两个项目之间如何依赖,继承实现

    maven的jar之间存在依赖关系的,我们在引入一个时,其他有依赖关系的也会被引入 依赖排除: 比如现在有两个依赖关系,A(x,java,y.java,z.java)  B(a,java,b,java ...

  6. 利用selenium模拟登陆

    第一部:利用selenium登陆 导入selenium库 from selenium import webdriver 明确模拟浏览器在电脑中存放的位置,比如我存在当前目录 chromePath = ...

  7. drf路由分发、解析/渲染模块配置、使用admin、自动序列化配置

    目录 drf路由分发配置 解析模块配置 渲染模块配置 浏览器渲染打开 浏览器渲染关闭 结论 drf使用后台admin drf序列化模块 serializers.py: views.py:单查群查 测试 ...

  8. 13-MyBatis03(逆向工程)

    MyBatis逆向工程 1.导入jar包 <dependency> <groupId>org.mybatis</groupId> <artifactId> ...

  9. oneweb and starlink

    2019.7.16,一网在首尔开展在轨测试,最高速度400Mbps,延时32ms:地面终端由韩国Intellian生产. https://www.oneweb.world/media-center/o ...

  10. 向C++之父Bjarne Stroustrup致敬

    2013-04-25 21:30 (分类:社会人生) 非常好的文章 C ++ 的 背 影                                     ——C++之父Bjarne Strou ...