基于javacv ffmpeg 使用原生ffmpeg命令
- 1. ffmpeg
- 2. ffprobe
相关阅读:
javacv ffmpeg使用笔记
测试过程中,发现
ffmpeg-6.0-1.5.9-linux-x86_64.jar
存在问题(ffmpeg原生命令执行失败),降级到ffmpeg-5.1.2-1.5.8-linux-x86_64.jar
版本正常。
<javacv.version>1.5.8</javacv.version>
<dependencies>
<dependency>
<groupId>org.bytedeco</groupId>
<artifactId>javacv</artifactId>
<version>${javacv.version}</version>
</dependency>
<dependency>
<groupId>org.bytedeco</groupId>
<artifactId>ffmpeg</aifactId>
<version>5.1.2-${javacv.version}</version>
<classifier>linux-x86_64</classifier>
</dependency>
</dependencies>
<profiles>
<profile>
<id>Windows</id>
<activation>
<os>
<!-- mvn enforcer:display-info -->
<family>windows</family>
</os>
</activation>
<dependencies>
<dependency>
<groupId>org.bytedeco</groupId>
<artifactId>ffmpeg</artifactId>
<version>5.1.2-${javacv.version}</version>
<classifier>windows-x86_64</classifier>
</dependency>
</dependencies>
</profile>
<profile>
<id>Mac</id>
<activation>
<os>
<!-- mvn enforcer:display-info -->
<family>mac</family>
</os>
</activation>
<dependencies>
<dependency>
<groupId>org.bytedeco</groupId>
<artifactId>ffmpeg</artifactId>
<version>5.1.2-${javacv.version}</version>
<classifier>macosx-x86_64</classifier>
</dependency>
</dependencies>
</profile>
</profiles>
1. ffmpeg
org.bytedeco.ffmpeg.ffmpeg
org.bytedeco.ffmpeg.ffprobe
- 版本号
String ffmpeg = Loader.load(org.bytedeco.ffmpeg.ffmpeg.class);
System.out.println("ffmpeg path=>" + ffmpeg);
ProcessBuilder pb = new ProcessBuilder(ffmpeg, "-version");
pb.inheritIO().start().waitFor();
- 直接拼接视频(前提各个视频片段帧率等属性一致)
// 输入视频文件路径
List<String> inputFiles = Arrays.asList("video1.mp4", "video2.mp4");
// 输出视频文件路径
String outputFile = "output.mp4";
// 拼接模板文件
String modelFile = "modelFile.txt";
// 创建拼接模板文件
try (BufferedWriter writer = new BufferedWriter(new FileWriter(modelFile))) {
for (String inputFile : inputFiles) {
writer.write("file '" + inputFile + "'");
writer.newLine();
}
} catch (IOException e) {
e.printStackTrace();
return;
}
String ffmpeg = Loader.load(org.bytedeco.ffmpeg.ffmpeg.class);
ProcessBuilder pb = new ProcessBuilder(ffmpeg,
"-f", "concat",
"-safe", "0",
"-i", modelFile,
"-c", "copy",
outputFile);
pb.inheritIO().start().waitFor();
2. ffprobe
- 版本号
public static void main(String[] args) throws IOException, InterruptedException {
String ffprobe = Loader.load(org.bytedeco.ffmpeg.ffprobe.class);
System.out.println("ffprobe path=>" + ffprobe);
ProcessBuilder pb = new ProcessBuilder(ffprobe, "-version");
pb.inheritIO().start().waitFor();
}
- 视频属性
更多用法:help、大模型
ffprobe -h topic
String ffprobe = Loader.load(org.bytedeco.ffmpeg.ffprobe.class);
ProcessBuilder pb = new ProcessBuilder(ffprobe,
"-v", "error",
"-print_format", "json",
"-show_streams",
"video1.mp4");
pb.inheritIO().start().waitFor();