Java 使用 FFmpeg 处理视频文件示例代码详解
作者:Bridge Li 时间:2023-03-19 00:54:37
目前在公司做一个小东西,里面用到了 FFmpeg 简单处理音视频,感觉功能特别强大,在做之前我写了一个小例子,现在记录一下分享给大家,希望大家遇到这个问题知道解决方案。
FFmpeg是一套可以用来记录、转换数字音频、视频,并能将其转化为流的开源计算机程序。采用LGPL或GPL许可证。它提供了录制、转换以及流化音视频的完整解决方案。它包含了非常先进的音频/视频编解码库libavcodec,为了保证高可移植性和编解码质量,libavcodec里很多code都是从头开发的。
FFmpeg在Linux平台下开发,但它同样也可以在其它操作系统环境中编译运行,包括Windows、Mac OS X等。这个项目最早由Fabrice Bellard发起,2004年至2015年间由Michael Niedermayer主要负责维护。许多FFmpeg的开发人员都来自MPlayer项目,而且当前FFmpeg也是放在MPlayer项目组的服务器上。项目的名称来自MPEG视频编码标准,前面的"FF"代表"Fast Forward"。
首先说明,我是在 https://ffmpeg.zeranoe.com/builds/ 这个地方下载的软件,Windows 和 Mac 解压之后即可使用。具体代码如下:
package cn.bridgeli.demo;
import org.junit.Test;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
/**
* @author BridgeLi
* @date 2020/2/29 15:40
*/
public class FfmpegTest {
private static final String OS = System.getProperty("os.name").toLowerCase();
private static final String FFMPEG_PATH = "/Users/bridgeli/ffmpeg-20200216-8578433-macos64-static/bin/ffmpeg";
@Test
public void testFfmpeg() {
String inputWavFile = "/Users/bridgeli/inputWavFile.wav";
String inputMp3File = "/Users/bridgeli/inputMp3File.mp3";
String inputMp4File = "/Users/bridgeli/inputMp4File.mp4";
String outMergeMp3File = "/Users/bridgeli/outMergeMp3File.mp3";
String outMergeMp3AndMp4File = "/Users/bridgeli/outMergeMp3AndMp4File.mp4";
String outConcatMp3File = "/Users/bridgeli/outConcatMp3File.mp3";
// 拼接
String command = null;
if (OS.contains("mac") || OS.contains("linux")) {
command = FFMPEG_PATH + " -i " + inputMp3File + " -i " + inputWavFile + " -filter_complex [0:0][1:0]concat=n=2:v=0:a=1[a] -map [a] " + outConcatMp3File;
} else if (OS.contains("windows")) {
command = FFMPEG_PATH + " -i " + inputMp3File + " -i " + inputWavFile + " -filter_complex \"[0:0][1:0]concat=n=2:v=0:a=1[a]\" -map \"[a]\" " + outConcatMp3File;
}
// 合并(视频和音频)
// String command = FFMPEG_PATH + " -i " + inputMp4File + " -i " + outConcatMp3File + " -c:v copy -c:a aac -strict experimental " + outMergeMp3AndMp4File;
// 合并
// String command = FFMPEG_PATH + " -i " + inputMp3File + " -i " + inputWavFile + " -filter_complex amerge -ac 2 -c:a libmp3lame -q:a 4 " + outMergeMp3File;
System.out.println(command);
Process process = null;
try {
process = Runtime.getRuntime().exec(command);
} catch (IOException e) {
e.printStackTrace();
}
if (null == process) {
return;
}
try {
process.waitFor();
} catch (InterruptedException e) {
e.printStackTrace();
}
try (InputStream errorStream = process.getErrorStream();
InputStreamReader inputStreamReader = new InputStreamReader(errorStream);
BufferedReader br = new BufferedReader(inputStreamReader)) {
String line = null;
StringBuffer context = new StringBuffer();
while ((line = br.readLine()) != null) {
context.append(line);
}
System.out.println("error message: " + context);
} catch (IOException e) {
e.printStackTrace();
}
process.destroy();
}
}
在我的认知中,完成任务是第一位的,所以按照这个简单处理一下音视频是没有问题的,具体更强大的语法,大家可以自己查询相关文档,也可以参考 https://www.jb51.net/article/181662.htm这篇文中,其中我个人也在学习中。下面说两个在使用的过程中遇到的问题。
1. 我在测试的时候,DOS 和 bash 都没有问题,但是 Java 一调用就出错,仔细看报错信息都是什么参数无效之类的,后面参考https://www.jb51.net/article/181668.htm这篇文章,原来都是一些单双引号和空格什么之类的导致的,大家在用的时候可以注意下,也多看看报错信息。
2. 因为我是从上面的文中提到的网址中直接下载解压使用的,但是在部署测试环境的时候是让运维帮忙部署的,因为上面也没有运维直接使用的可执行文件,所以个人猜测运维是直接源码安装的,所以在使用的过程过中遇到了一个问题,没有安装 mp3 编码库导致的,具体参考 https://www.jb51.net/article/181671.htm 这篇文章解决,所以大家在安装好环境之后可以先自己试着直接执行一下命令看看是否成功。
来源:https://www.bridgeli.cn/archives/652
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Spring Boot 2结合Spring security + JWT实现微信小程序登录
Spring JPA联表查询之注解属性详解
![](https://img.aspxhome.com/file/2023/9/67599_0s.jpg)
spring cloud gateway网关路由分配代码实例解析
SpringMVC 向jsp页面传递数据库读取到的值方法
springboot html调用js无效400问题及解决
![](https://img.aspxhome.com/file/2023/3/97943_0s.png)
Zookeeper连接超时问题与拒绝连接的解决方案
![](https://img.aspxhome.com/file/2023/3/78863_0s.png)
JAVAlogback日志管理详解
![](https://img.aspxhome.com/file/2023/1/78861_0s.png)
Android XRecyclerView实现多条目加载
![](https://img.aspxhome.com/file/2023/6/130356_0s.gif)
C#使用List类实现动态变长数组的方法
MyEclipse2018中安装Mybatis generator插件的实现步骤
![](https://img.aspxhome.com/file/2023/9/63649_0s.png)
Android studio 混淆配置详解
使用java.nio.file 库优雅的操作文件详解
![](https://img.aspxhome.com/file/2023/7/132077_0s.png)
springboot项目访问静态资源的配置代码实例
解决java文件流处理异常 mark/reset not supported问题
![](https://img.aspxhome.com/file/2023/8/81228_0s.jpg)
springboot框架阿里开源低代码工具LowCodeEngine
![](https://img.aspxhome.com/file/2023/7/131887_0s.png)
Struts2中接收表单数据的三种驱动方式
java设计模式之工厂方法详解
![](https://img.aspxhome.com/file/2023/8/76238_0s.png)
C# Assembly.Load案例详解
![](https://img.aspxhome.com/file/2023/2/106482_0s.png)
ThreadLocal工作原理及用法案例
Android开发之ListView的简单用法及定制ListView界面操作示例
![](https://img.aspxhome.com/file/2023/4/110724_0s.gif)