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

标签:java,FFmpeg,视频
0
投稿

猜你喜欢

  • Spring Boot 2结合Spring security + JWT实现微信小程序登录

    2022-07-14 08:25:54
  • Spring JPA联表查询之注解属性详解

    2021-11-04 14:19:04
  • spring cloud gateway网关路由分配代码实例解析

    2021-06-09 02:54:08
  • SpringMVC 向jsp页面传递数据库读取到的值方法

    2022-03-29 00:51:15
  • springboot html调用js无效400问题及解决

    2023-06-24 02:11:54
  • Zookeeper连接超时问题与拒绝连接的解决方案

    2023-11-20 03:41:29
  • JAVAlogback日志管理详解

    2023-01-11 22:33:54
  • Android XRecyclerView实现多条目加载

    2021-10-15 07:32:21
  • C#使用List类实现动态变长数组的方法

    2022-11-30 03:44:35
  • MyEclipse2018中安装Mybatis generator插件的实现步骤

    2022-02-17 03:47:37
  • Android studio 混淆配置详解

    2023-02-16 19:17:22
  • 使用java.nio.file 库优雅的操作文件详解

    2022-09-20 00:51:12
  • springboot项目访问静态资源的配置代码实例

    2021-11-16 02:07:49
  • 解决java文件流处理异常 mark/reset not supported问题

    2022-10-05 14:28:08
  • springboot框架阿里开源低代码工具LowCodeEngine

    2022-09-01 09:40:41
  • Struts2中接收表单数据的三种驱动方式

    2022-04-21 09:23:11
  • java设计模式之工厂方法详解

    2023-09-09 01:18:56
  • C# Assembly.Load案例详解

    2021-11-06 03:20:33
  • ThreadLocal工作原理及用法案例

    2021-11-13 04:22:39
  • Android开发之ListView的简单用法及定制ListView界面操作示例

    2021-10-17 13:26:15
  • asp之家 软件编程 m.aspxhome.com