怎么在Java中使用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();
}
}
关于怎么在Java中使用FFmpeg 处理视频文件问题的解答就分享到这里了,希望以上内容可以对大家有一定的帮助,如果你还有很多疑惑没有解开,可以关注亿速云行业资讯频道了解更多相关知识。
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。