java实现视频和配音音频的合成
生活随笔
收集整理的這篇文章主要介紹了
java实现视频和配音音频的合成
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
音頻和視屏?xí)r長一致
一、依賴
<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();//創(chuàng)建錄制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;}}三、測試
@Testvoid 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);}總結(jié)
以上是生活随笔為你收集整理的java实现视频和配音音频的合成的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: c++中strtok函数
- 下一篇: 米兔机器人第三代测评_精致易玩,米兔积木