java使用ffmpeg处理视频的方法
时间:2021-11-13 15:29:53|栏目:JAVA代码|点击: 次
FFmpeg是一套可以用来记录、转换数字音频、视频,并能将其转化为流的开源计算机程序。采用LGPL或GPL许可证。它提供了录制、转换以及流化音视频的完整解决方案。

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();
}
}
}
总结
栏 目:JAVA代码
下一篇:springboot使用filter获取自定义请求头的实现代码
本文标题:java使用ffmpeg处理视频的方法
本文地址:http://www.codeinn.net/misctech/183372.html


阅读排行
- 1Java Swing组件BoxLayout布局用法示例
- 2java中-jar 与nohup的对比
- 3Java邮件发送程序(可以同时发给多个地址、可以带附件)
- 4Caused by: java.lang.ClassNotFoundException: org.objectweb.asm.Type异常
- 5Java中自定义异常详解及实例代码
- 6深入理解Java中的克隆
- 7java读取excel文件的两种方法
- 8解析SpringSecurity+JWT认证流程实现
- 9spring boot里增加表单验证hibernate-validator并在freemarker模板里显示错误信息(推荐)
- 10深入解析java虚拟机




