在java中使用FFmpeg将图片和音频合成视频

 下面分享一个我之前在java中使用FFmpeg将图片和音频合成视频的demo。

package com.xxx.console.videoProcess;

import java.awt.image.BufferedImage;
import java.io.File;

import org.bytedeco.javacpp.avcodec;
import org.bytedeco.javacpp.opencv_core.IplImage;
import org.bytedeco.javacv.FFmpegFrameGrabber;
import org.bytedeco.javacv.FFmpegFrameRecorder;
import org.bytedeco.javacv.Frame;
import org.bytedeco.javacv.FrameGrabber;
import org.bytedeco.javacv.OpenCVFrameConverter;

import com.xxx.console.photoProcess.PhotoProcess;

import static org.bytedeco.javacpp.opencv_imgcodecs.cvLoadImage;

public class GenerateVideo {
	public void dojob(String picturesPath, String audioPath, String videoPath) throws Exception {
		FrameGrabber audioFrames = new FFmpegFrameGrabber(audioPath);
		audioFrames.start();
		
		int frameRate = 2;
		FFmpegFrameRecorder recorder = new FFmpegFrameRecorder(videoPath, 640, 480, audioFrames.getAudioChannels());
		recorder.setVideoCodec(avcodec.AV_CODEC_ID_H264); 
		recorder.setFormat("mp4");
		recorder.setFrameRate(frameRate);
		recorder.setPixelFormat(0); 
		recorder.start();

		OpenCVFrameConverter.ToIplImage conveter = new OpenCVFrameConverter.ToIplImage();
		
		File file = new File(picturesPath);
		File[] flist = file.listFiles();
		IplImage[] images = new IplImage[flist.length];
		
		PhotoProcess photoProcess = new PhotoProcess();
		int width = 640;
		
		for (int i = 0; i < flist.length; i++) {
			String fname = picturesPath + "/" + (i + 1) + ".jpg";
                        // 调整图片的大小,未贴出源码,可以删除跳过
			BufferedImage image = photoProcess.scalePhotoFromFile(fname, width);
			images[i] = photoProcess.toIplImage(image).clone();
		}
		
		Frame frameAudio;
		for (int j = 0; j < 80; ++j) {
			frameAudio = audioFrames.grabFrame();
		}
		
		for (int i = 0; i < flist.length; ++i) {
			for (int j = 0; j < frameRate * 2; ++j) {
				for (int k = 0; k < 80 / (frameRate * 2); ++k) {
					frameAudio = audioFrames.grabFrame();
					recorder.record(frameAudio);
				}
				recorder.record(conveter.convert(images[i]));
			}
		}

		audioFrames.stop();
		recorder.stop();
		recorder.release();
	}
}

 

你可能感兴趣的:(小问题,视频合成,java)