前言

  公司业务中有一些场景需要用到服务端音视频剪辑技术,最开始为了快速上线使用的是某公有云的商用解决方案,但由于费用太高所以我们团队经过一个星期的冲刺,给出了一个FFmpeg+Serverless的解决方案,更好地满足了业务方的视频剪辑需求。经过统计,自研方案成功地将剪辑失败率降到了万分之一左右,并且将费用成本降到了原本的15%左右,是一个非常大的突破。现在我们计划把该平台做得更加通用化,让更多的业务可以无缝接入,通过任务编排来实现更多定制化需求。

  题外话,为什么我们会选择Serverless来实现该业务呢,因为我们的业务高峰期特别明显,时效性要求也高,在使用某公有云解决方案期间经常触发系统QPS限制,经过多次沟通也只能临时调整,而且对方技术半夜还打电话来问我是否可以限制QPS,作为使用方肯定是不愿意的,所以除了成本外性能也是我们下决心自研系统的原因之一。Serverless在使用过程中遇到的问题我也会在后续时间里面记录下来。

  本编博客主要是记录在整个过程中涉及到的FFmpeg常见命令,以及一些坑,分享给大家使用,若有谬误请批评指正。

基本命令

音视频剪切

String.format("%s -i %s -vcodec libx264  -ss %s -to %s %s -y", ffmpegCommandPath, sourceFilePath, startTime, endTime, targetFilePath)

ffmpegCommandPath    表示FFmpeg执行命令的路径

sourceFilePath              表示源文件路径

startTime                     表示剪切的起始点,格式为 "00:00:00", 例如 "00:00:15" 表示剪切从第15秒开始

endTime                      表示剪切的终止点,格式为 "00:00:00", 例如 "00:00:20" 表示剪切截止到第20秒

targetFilePath               表示剪切后的输出文件

-y                               表示输出文件若存在则覆盖  

音频/视频简单拼接

String.format("%s -f concat -safe 0 -i %s -c copy %s", ffmpegCommandPath, concatListFilePath, destinationFilePath)

ffmpegCommandPath    表示FFmpeg执行命令的路径

concatListFilePath         表示拼接的配置文件,内容格式为

file 'filePath1.audio'

file 'filePath2.audio'

destinationFilePath        表示拼接后的输出文件

使用限制:该方式不涉及到文件的解码、编码,所以速度极快,但如果待处理文件的编码格式不同则请勿使用,否则输出的文件可能无法正常播放(或者只能播放一部分)。如果编码格式不同,请参考下文中的音频拼接/视频拼接方式,会更加可靠,当会更加消耗资源。

音频拼接

由于涉及到到参数拼接,所以直接上代码(Java方式)。

/**
* 音频文件拼接
* @param files 音频文件资源路径数组
* @param destinationFilePath 处理后输出文件路径
*/
public static void audioConcat(String[] files, String destinationFilePath) {
// command list
List<String> commandList = new ArrayList<>();
commandList.add("ffmpeg"); // input_options
for (String file : files) {
commandList.add("-i");
commandList.add(file);
} // filter_complex
StringBuilder filterComplexOptions = new StringBuilder();
for (int i = 0; i < files.length; i++) {
filterComplexOptions.append(String.format("[%s:0]", i));
}
filterComplexOptions.append(String.format("concat=n=%s:v=0:a=1[out]", files.length));
commandList.add("-filter_complex");
commandList.add(filterComplexOptions.toString());
commandList.add("-map");
commandList.add("[out]");
commandList.add(destinationFilePath);
Runtime.getRuntime().exec(commandList.toArray(new String[0])); // next process
}

视频拼接

由于涉及到到参数拼接,所以直接上代码(Java方式)。

/**
* 视频拼接
* @param files 音频文件资源路径数组
* @param destinationFilePath 处理后输出文件路径
* @param outputWidth 输出视频的宽度
* @param outputHeight 输出视频的高度
*/
public static void videoConcat(String[] files, String destinationFilePath, Integer outputWidth, Integer outputHeight) {
// command list
List<String> commandList = buildFfmpegCommand();
commandList.add("ffmpeg"); // input_options
for (String file : files) {
commandList.add("-i");
commandList.add(file);
} // filter_complex
StringBuilder filterComplexOptions = new StringBuilder();
StringBuilder streamsOptions = new StringBuilder();
for (int i = 0; i < files.length; i++) {
filterComplexOptions.append(String.format("[%s:v]scale=w=%s:h=%s,setsar=1/1[v%s];", i, outputWidth, outputHeight, i));
streamsOptions.append(String.format("[v%s][%s:a]", i, i));
}
streamsOptions.append(String.format("concat=n=%s:v=1:a=1 [vv] [aa]", files.length));
commandList.add("-filter_complex");
commandList.add(String.format("%s%s", filterComplexOptions.toString(), streamsOptions.toString()));
Collections.addAll(commandList, "-map", "[vv]", "-map", "[aa]", "-c:v", "libx264", "-x264-params",
"profile=main:level=3.1", "-crf", "18", "-y", "-vsync", "vfr");
commandList.add(destinationFilePath);
Runtime.getRuntime().exec(commandList.toArray(new String[0])); // next process
}

踩坑经验: 我们在拼接过程中遇到了视频拼接出错的情况,但数量比较少,通过FFprobe命令分析,发现这种情况出现在其中某个视频无音轨的情况,找了很多解决方案,最后采用的方式是为这个视频配一个音轨,相当于先把素材标准化处理,为视频注入音轨的方式见下文 "无音轨视频配音"。

音视频混合

String.format("%s -i %s -i %s -filter_complex amix -map 0:v -map 0:a -map 1:a -shortest -y %s", ffmpegCommandPath, videoFilePath, audioFilePath, targetFilePath)

ffmpegCommandPath    表示FFmpeg执行命令的路径

videoFilePath                表示视频文件路径

audioFilePath                表示音频文件路径

targetFilePath               表示输出文件路径

无音轨视频配音

String.format("%s -i %s -f lavfi -i aevalsrc=0 -shortest -y %s", ffmpegCommandPath, videoFilePath, targetFilePath)

ffmpegCommandPath    表示FFmpeg执行命令的路径

videoFilePath                表示视频文件路径

targetFilePath               表示输出文件路径

最新文章

  1. 双层路由设置,WAN口和LAN口连接的方法设置
  2. 解析导航栏的url--selnium,beautifulsoup实战
  3. scanf
  4. 查看jquery绑定的事件函数
  5. linux同步
  6. UVa11090 Going in Cycle!!
  7. PHPCMSV9 采集网址后,再采集内容,报错:“采集采集内容 没有找到网址列表,请先进行网址采集”
  8. COJ 0248 HDNOIP201408生成树
  9. 在CentOS 7 / Gnome 3 双屏时设置主屏
  10. 教你看懂C++类库函数定义之三---_stdcall
  11. android UI进阶之用【转】
  12. Android 增量更新
  13. .NET平台下,初步认识AutoMapper
  14. liunx-centos的安装学习篇
  15. 第3章 Data语意学
  16. Python基础【day01】:Hello World程序(二)
  17. Ubuntu 16.04 构建 Headless VNC 服务器
  18. SpringMVC @RequestParam和@RequestBody的区别
  19. RxJava学习(一)——简介及其优势
  20. 在webpack构建的项目中使用vue

热门文章

  1. C++逆向分析----多重继承和菱形继承
  2. 关于ollydbg的堆栈视图的使用(结合crackme2分析)
  3. Job for ssh.service failed because the control process exited with error code. See &quot;systemctl status ssh.service&quot; and &quot;journalctl -xe&quot; for details.
  4. lsblk         查看分区和磁盘
  5. shell基础之exit,break,continue
  6. Servlet中的过滤器和监听器
  7. Python break/continue - Python零基础入门教程
  8. 26.Qt Quick QML-RotationAnimation、PathAnimation、SmoothedAnimation、Behavior、PauseAnimation、SequentialAnimation和ParallelAnimation
  9. [LeetCode] 231. 2 的幂
  10. GO语言的JSON02---反序列化