目录
- Javacv ffmpeg使用原生ffmpeg命令
- 1. ffmpeg
- 2. ffprobe
- 总结
javacv ffmpeg使用原生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> android </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> <www.devze.com;/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,js "-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();
总结
以上为个人经验,希望能给大家一个参考,也希望大家多多支持编程客栈(www.devze.com)。
精彩评论