查询了网上很多资料,发现没有比较靠谱的实现,JavaCV的资料还是比较少的
安装依赖
org.bytedeco
javacv-platform
1.5.3
代码实现
/**
* 视频转码
*/
public static void recode() throws FrameGrabber.Exception, FrameRecorder.Exception {
String filePath = "ef568e9b96be750fba2e918b651aa70c.avi";
String ext = filePath.substring(filePath.lastIndexOf("."));
String newFilePath = filePath.replace(ext, "_recode.mp4");
FFmpegFrameGrabber grabber = new FFmpegFrameGrabber(filePath);
grabber.start();
FFmpegFrameRecorder recorder = new FFmpegFrameRecorder(newFilePath, grabber.getImageWidth(),
grabber.getImageHeight(), grabber.getAudioChannels());
recorder.setVideoCodec(avcodec.AV_CODEC_ID_H264);
recorder.setFormat("mp4");
recorder.setPixelFormat(avutil.AV_PIX_FMT_YUV420P);