java使用ffmpeg处理视频的方法
作者:XlikeQ 时间:2023-07-28 22:58:08
FFmpeg是一套可以用来记录、转换数字音频、视频,并能将其转化为流的开源计算机程序。采用LGPL或GPL许可证。它提供了录制、转换以及流化音视频的完整解决方案。
官网链接http://ffmpeg.org/
1.下载并解压windows版本安装包
2.windows本地使用命令行测试
1.修改格式测试(转码)
将需要修改的视频A.avi 提前放在bin目录下
在bin目录下cmd进入命令行
输入命令完成转码成B.mp4
ffmpeg.exe -i A.avi -y B.mp4
2.视频音频结合测试
将需要修改的视频A.avi和bgm.mp3 提前放在bin目录下
在bin目录下cmd进入命令行
输入命令完成合并成8秒的new.avi
ffmpeg.exe -i A.avi -i bgm.mp3 -t 8 -y new.avi
3.java中建立工具测试类
package com.xc.utils;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
public class FFMpegTest {
private String ffmpegEXE;
public FFMpegTest(String ffmpegEXE) {
super();
this.ffmpegEXE = ffmpegEXE;
}
public void convertor(String videoInputPath, String videoOutputPath) throws Exception {
//ffmpeg -i input.mp4 -y output.avi
List<String> command = new ArrayList<>();
command.add(ffmpegEXE);
command.add("-i");
command.add(videoInputPath);
command.add("-y");
command.add(videoOutputPath);
for (String c : command) {
System.out.print(c + " ");
}
ProcessBuilder builder = new ProcessBuilder(command);
Process process = builder.start();
InputStream errorStream = process.getErrorStream();
InputStreamReader inputStreamReader = new InputStreamReader(errorStream);
BufferedReader br = new BufferedReader(inputStreamReader);
String line = "";
while ( (line = br.readLine()) != null ) {
}
if (br != null) {
br.close();
}
if (inputStreamReader != null) {
inputStreamReader.close();
}
if (errorStream != null) {
errorStream.close();
}
}
public static void main(String[] args) {
FFMpegTest ffmpeg = new FFMpegTest("C:\\ffmpeg\\bin\\ffmpeg.exe");
try {
ffmpeg.convertor("C:\\a.mp4", "C:\\b.avi");
} catch (Exception e) {
e.printStackTrace();
}
}
}
package com.xc.utils;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
public class MergeVideoMp3 {
private String ffmpegEXE;
public MergeVideoMp3(String ffmpegEXE) {
super();
this.ffmpegEXE = ffmpegEXE;
}
public void convertor(String videoInputPath, String mp3InputPath,
double seconds, String videoOutputPath) throws Exception {
//ffmpeg.exe -i A.avi -i bgm.mp3 -t 7 -y new.avi
List<String> command = new ArrayList<>();
command.add(ffmpegEXE);
command.add("-i");
command.add(videoInputPath);
command.add("-i");
command.add(mp3InputPath);
command.add("-t");
command.add(String.valueOf(seconds));
command.add("-y");
command.add(videoOutputPath);
//for (String c : command) {
//System.out.print(c + " ");
//}
ProcessBuilder builder = new ProcessBuilder(command);
Process process = builder.start();
InputStream errorStream = process.getErrorStream();
InputStreamReader inputStreamReader = new InputStreamReader(errorStream);
BufferedReader br = new BufferedReader(inputStreamReader);
String line = "";
while ( (line = br.readLine()) != null ) {
}
if (br != null) {
br.close();
}
if (inputStreamReader != null) {
inputStreamReader.close();
}
if (errorStream != null) {
errorStream.close();
}
}
public static void main(String[] args) {
MergeVideoMp3 ffmpeg = new MergeVideoMp3("C:\\ffmpeg\\bin\\ffmpeg.exe");
try {
ffmpeg.convertor("C:\\a.avi", "C:\\bgm.mp3", 7.1, "C:\\javaNew.mp4");
} catch (Exception e) {
e.printStackTrace();
}
}
}
来源:https://blog.csdn.net/xlikec/article/details/84553260
标签:java,ffmpeg,视频
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
java web项目里ehcache.xml介绍
2022-02-25 20:46:25
Android调用google地图生成路线图实现代码
2023-06-04 09:37:25
C# WPF 自定义按钮的方法
2021-08-30 23:42:11
![](https://img.aspxhome.com/file/2023/0/69240_0s.gif)
Java 中 synchronized的用法详解(四种用法)
2022-03-11 08:55:05
spring boot2结合mybatis增删改查的实现
2023-07-07 01:17:31
![](https://img.aspxhome.com/file/2023/4/61644_0s.png)
JAVA中JSONObject对象和Map对象之间的相互转换
2023-07-13 15:04:28
浅谈MyBatis 事务管理
2022-03-22 16:17:11
Android UI实现广告Banner轮播效果
2023-10-26 14:04:14
![](https://img.aspxhome.com/file/2023/7/93257_0s.jpg)
Unity3D UGUI实现翻书特效
2022-03-30 23:02:59
![](https://img.aspxhome.com/file/2023/0/82120_0s.jpg)
简单说说Java SE、Java EE、Java ME三者之间的区别
2022-01-20 06:05:10
Struts2 Result 参数详解
2022-04-28 07:54:35
Android开发笔记之:一分钟学会使用Logcat调试程序的详解
2022-07-19 05:17:22
![](https://img.aspxhome.com/file/2023/8/139408_0s.png)
Kotlin 基础教程之类、对象、接口
2022-03-12 04:22:15
Android 应用启动欢迎界面广告的实现实例
2023-01-26 07:34:11
java反射遍历实体类属性和类型,并赋值和获取值的简单方法
2023-07-15 04:37:18
android手机端与PC端使用adb forword通信
2022-09-15 14:17:35
Android开发实现圆形图片功能示例
2023-07-12 08:43:54
![](https://img.aspxhome.com/file/2023/4/116004_0s.gif)
Android Support Palette使用详解
2023-04-05 15:47:24
![](https://img.aspxhome.com/file/2023/4/108264_0s.jpg)
如何使用Android注解处理器
2023-08-16 16:49:05
![](https://img.aspxhome.com/file/2023/8/139418_0s.jpg)
微信小程序 navigator 跳转url传递参数
2022-03-17 05:46:56
![](https://img.aspxhome.com/file/2023/1/92651_0s.png)