音频和视屏时长一致
一、依赖
<dependency> <groupId>org.bytedeco</groupId> <artifactId>javacv-platform</artifactId> <version>1.5.4</version> </dependency> <dependency> <groupId>org.bytedeco</groupId> <artifactId>javacv</artifactId> <version>1.5.2</version> </dependency>
二、工具类
package com.xx.demo.unit; import org.bytedeco.javacv.*; import java.io.File; public class MergeMovieAndVoiceUtil { public static String mergeAudioAndVideo(String videoPath, String audioPath, String outPut) throws Exception { # FrameRecorder recorder = null; FrameGrabber grabber1 = null; FrameGrabber grabber2 = null; try { //抓取视频帧 grabber1 = new FFmpegFrameGrabber(videoPath); //抓取音频帧 grabber2 = new FFmpegFrameGrabber(audioPath); grabber1.start(); grabber2.start(); //创建录制 recorder = new FFmpegFrameRecorder(outPut, grabber1.getImageWidth(), grabber1.getImageHeight(), grabber2.getAudioChannels()); recorder.setFormat("mp4"); recorder.setFrameRate(grabber1.getFrameRate()); recorder.setSampleRate(grabber2.getSampleRate()); recorder.start(); Frame frame1; Frame frame2; //然后录入音频 while ((frame2 = grabber2.grabFrame()) != null) { recorder.record(frame2); } //先录入视频 while ((frame1 = grabber1.grabFrame()) != null) { recorder.record(frame1); } grabber1.stop(); grabber2.stop(); recorder.stop(); } catch (Exception e) { e.printStackTrace(); } finally { try { if (recorder != null) { recorder.release(); } if (grabber1 != null) { grabber1.release(); } if (grabber2 != null) { grabber2.release(); } } catch (FrameRecorder.Exception e) { e.printStackTrace(); } } return outPut; } }
三、测试
@Test void contextl() throws Exception { String movie = "C:\\Users\\yjtzf\\Desktop\\1.mp4"; String audio = "C:\\Users\\yjtzf\\Desktop\\22.mp3"; String resultPath = "C:\\Users\\yjtzf\\Desktop\\d666.mp4"; MergeMovieAndVoiceUtil.mergeAudioAndVideo(movie, audio, resultPath); }