目前已验证支持avi,flv,mpg,mpeg,mov,rmvb,asf,ogv等视频文件格式转成mp4,至于mp4转成其他类型,目前只验证了一种mp4->mov, 感兴趣的小伙伴可以验证下mp4转成其他类型。
首先引入maven依赖
<dependency>
<groupId>org.bytedeco</groupId>
<artifactId>javacv</artifactId>
<version>1.5.10</version>
</dependency>
<dependency>
<groupId>org.bytedeco</groupId>
<artifactId>javacv-platform</artifactId>
<version>1.5.10</version>
</dependency>
工具类和main方法:
package com.person.ai.config;
import lombok.extern.slf4j.Slf4j;
import org.bytedeco.javacv.*;
@Slf4j
public class CovertVideoUtils {
/**
*
* @param inputPath 源文件
* @param outputPath 目标文件
* @param format 生成目标文件的格式
* @throws FrameRecorder.Exception 异常类
*/
public static void convert(String inputPath, String outputPath, String format) throws FrameRecorder.Exception {
try {
FFmpegFrameGrabber frameGrabber = new FFmpegFrameGrabber(inputPath);
frameGrabber.start();
FFmpegFrameRecorder frameRecorder = new FFmpegFrameRecorder(outputPath, frameGrabber.getImageWidth(), frameGrabber.getImageHeight(), frameGrabber.getAudioChannels());
frameRecorder.setFormat(format);
frameRecorder.setFrameRate(frameGrabber.getFrameRate());
frameRecorder.setSampleRate(frameGrabber.getSampleRate());
frameRecorder.start();
Frame frame;
while ((frame = frameGrabber.grabFrame()) != null) {
frameRecorder.record(frame);
}
frameRecorder.stop();
frameGrabber.stop();
}
catch (FrameGrabber.Exception e) {
log.error("convert failed", e);
}
}
public static void main(String[] args) throws FrameRecorder.Exception {
CovertVideoUtils.convert("E:/video/output4.mp4", "E:/video/output6.mov", "mov");
}
}