前两天看了ffmpeg,真的好多东西,把我看到的用到的分享一下叭~
一
获取关键帧
ffmpeg -i input.mp4 -an -vf select='eq(pict_type\,I)' -vsync 2 -s 720*480 -f image2 dstPath/image-%03d.jpg
获取第一个关键帧
ffmpeg -i tmp.mp4 -vframes 1 -f image2 1.jpg
指定时间点导出帧到图片
ffmpeg -i tmp.mp4 -ss 00:00:05.333 -f image2 -vframes 1 tmp.png
指定时间点导出帧到图片(jpeg格式)
ffmpeg -i tmp.mp4 -ss 00:00:05.333 -qscale:v 2 -f image2 -vframes 1 tmp.jpg
指定特定帧导出到图片
ffmpeg -i tmp.mp4 -filter:v select="between(n\,128\,128)" -qscale:v 2 -f image2 -vframes 1 tmp.jpg
指定图片替换特定帧
ffmpeg -i tmp.mp4 -i tmp.jpg -filter_complex "[0:v][1:v]overlay=enable='between(n,128,128)'" -acodec copy tmp.out.mp4
以特定波特率提取特定时间段的视频
ffmpeg -r 1 -ss 0 -t 30 -i F:\tmp.mp4 -c:v copy -b:v 2472k F:\tmp-out.mp4
ffmpeg -i F:\tmp02.mp4 -b 2472k F:\tmpout1.mp4
ffmpeg -i F:\tmp-out0.mp4 -b:v 2472k F:\tmp-out1.mp4
(排查了一下,如果想要得到总比特率是2472的话,用-b 2472k会出现偏差,需要分析视频的转换编码,可以调成总比特率2472kbps)
二
转码
ffmpeg -i input.mkv -codec copy output.mp4
剪切
ffmpeg -ss 00:00:00 -t 00:02:42 -i input.mp4 -acodec copy -vcodec copy output.mp4
合并
ffmpeg -f concat -safe 0 -i input.txt -c copy output.mp4
input.txt文件内格式为:
file input1.mp4
file input2.mp4
给视频添加字幕
创建一个srt
格式的文件,一个典型的srt
文件如下
1
00:00:39,770 --> 00:00:41,880
在经历了一场人生巨变之后
When I was lying there in the VA hospital ...
2
00:00:42,550 --> 00:00:44,690
我被送进了退伍军人管理局医院
... with a big hole blown through the middle of my life,
3
00:00:45,590 --> 00:00:48,120
那段时间我经常会梦到自己在飞翔
... I started having these dreams of flying.
添加字幕的 ffmpeg 命令为:
ffmpeg -i input.mp4 -vf subtitles=input.srt output.mp4
ffmpeg 更多给视频添加字幕的配置请参考:https://www.jianshu.com/p/f33910818a1c
多数srt
支持一些特定格式化,比如斜体、粗体、下划线以及字体颜色。使用时需要基于 HTML 的标签,具体用法可参考:http://www.360doc.com/content/17/0527/14/57493_657716572.shtml
更多使用操作请参考:
- http://slhck.info/ffmpeg-encoding-course/#/
- https://www.ruanyifeng.com/blog/2020/01/ffmpeg.html