java使用ffmpeg进行多个视频合并

1、导言

参考 使用ffmpeg合并视频文件的三种方法

项目需要使用FFmpeg进行MP4视频合并

2、代码

package com;
import java.io.*;
import java.text.SimpleDateFormat;
import java.util.*;
public class aaa {
private static final String ffmpegPath = "D:\\Program Files\\ffmpeg\\bin\\ffmpeg.exe";
private static final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
public static void main(String[] args) {
M4();
}
private static void M4() {
List list = new ArrayList<>();
list.add("D:\\VideoRec\\he1.mp4");
list.add("D:\\VideoRec\\he2.mp4");
String outputDir = "D:/VideoRec/";
String output = "D:/VideoRec/he3.mp4";
mergeVideo(list, outputDir, output);
}
public static String mergeVideo(List<String> list, String outputDir, String outputFile) {
try {
String format1 = "%s -i %s -c copy -bsf:v h264_mp4toannexb -f mpegts %s";
String command1 = String.format(format1, ffmpegPath, list.get(0), outputDir + "input1.ts");
String command2 = String.format(format1, ffmpegPath, list.get(1), outputDir + "input2.ts");
String format3 = "%s -i \"concat:%s|%s\" -c copy -bsf:a aac_adtstoasc -movflags +faststart %s";
String command3 = String.format(format3, ffmpegPath, outputDir + "input1.ts", outputDir + "input2.ts", outputFile);
if (execCommand(command1) > 0 && execCommand(command2) > 0 && execCommand(command3) > 0) {
File file1 = new File(outputDir + "input1.ts");
File file2 = new File(outputDir + "input2.ts");
file1.delete();
file2.delete();
return "1";
} else {
return "0";
}
} catch (Exception e) {
e.printStackTrace();
System.out.println("-----合并失败!!!!!!" + outputFile);
return "0";
}
}
private static Integer execCommand(String command) {
try {
Process process = Runtime.getRuntime().exec(command);
//获取进程的标准输入流
final InputStream is1 = process.getInputStream();
//获取进城的错误流
final InputStream is2 = process.getErrorStream();
//启动两个线程,一个线程负责读标准输出流,另一个负责读标准错误流
readInputStream(is1);
readInputStream(is2);
process.waitFor();
process.destroy();
System.out.println("-----操作成功" + command + " " + sdf.format(new Date()));
return 1;
} catch (Exception e) {
e.printStackTrace();
System.out.println("-----操作失败" + command);
return -1;
}
}
private static void readInputStream(InputStream inputStream) {
new Thread(() -> {
BufferedReader br1 = new BufferedReader(new InputStreamReader(inputStream));
try {
String line1 = null;
while ((line1 = br1.readLine()) != null) {
if (line1 != null) {
}
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}).start();
}
}
posted @   一只桔子2233  阅读(2309)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
阅读排行:
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 单元测试从入门到精通
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律
历史上的今天:
2020-02-14 【Struts 动态表单】DynaActionForm
2020-02-14 【Struts 分派Action】DispatchAction
2020-02-14 【struts 报错】 No action config found for the specified url
点击右上角即可分享
微信分享提示