import org.bytedeco.ffmpeg.global.avcodec;
import org.bytedeco.ffmpeg.global.avutil;
import org.bytedeco.javacv.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.util.HashMap;
import java.util.Map;
/**
- 功能:Java多个图片合成视频
*/
public class VideoDemo {
//视频格式
private final static String VIDEO_TYPE = "mp4";
//合成的MP4 存放的地址路径
private final static String SAVE_VIDEO = "E:\\video\\img.mp4";
//读取图片的地址路径
private final static String READ_PICTURE = "E:\\image";
//图片大小
private final static Integer WIDTH = 1600;
private final static Integer HEIGHT = 1200;
//日志
private static Logger logger = LoggerFactory.getLogger(VideoDemo.class);
public static void main(String[] args) throws Exception {
//合成的MP4 存放的地址路径 这里的路径并不会自动创建,需要手动提前创建好,否则会报错:Could not open 'null'
String mp4 ="E:\\video\\img.mp4";
//图片存放的地址路径
String img = "E:\\image";
int width = 1600;
int height = 900;
//读取所有图片
File file = new File(READ_PICTURE);
File[] files = file.listFiles();
Map<Integer, File> imgMap = new HashMap<>();
int num = 0;
for (File imgFile : files) {
imgMap.put(num, imgFile);
num++;
}
createMp4(SAVE_VIDEO, imgMap, WIDTH, HEIGHT);
}
private static void createMp4(String mp4SavePath, Map<Integer, File> imgMap, int width, int height) throws FrameRecorder.Exception {
//视频宽高最好是按照常见的视频的宽高 16:9 或者 9:16
FFmpegFrameRecorder recorder = new FFmpegFrameRecorder(mp4SavePath, width, height);
//设置视频编码层模式 import org.bytedeco.ffmpeg.global.avcodec;可能需要手动复制添加
recorder.setVideoCodec(avcodec.AV_CODEC_ID_H264);
//设置视频为25帧每秒
recorder.setFrameRate(25);
//设置视频图像数据格式 import org.bytedeco.ffmpeg.global.avutil;可能需要手动复制添加
recorder.setPixelFormat(avutil.AV_PIX_FMT_YUV420P);
recorder.setFormat(VIDEO_TYPE);
try {
recorder.start();
Java2DFrameConverter converter = new Java2DFrameConverter();
//录制一个22秒的视频,22秒为自定义的一个视频时间长度,图片少则在22秒内,多则到22秒停止
for (int i = 0; i < imgMap.size(); i++) {
BufferedImage read = ImageIO.read(imgMap.get(i));
//一秒是25帧 所以要记录25次
for (int j = 0; j < 25; j++) {
recorder.record(converter.getFrame(read));
}
}
} catch (Exception e) {
logger.error("图片转换视频错误:"+e.getMessage());
} finally {
//最后一定要结束并释放资源
recorder.stop();
recorder.release();
}
}
//插入音频
public static boolean mergeAudioAndVideo(String videoPath, String audioPath, String outPut) throws Exception {
boolean isCreated = true;
File file = new File(videoPath);
if (!file.exists()) {
return false;
}
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 ((frame1 = grabber1.grabFrame()) != null) {
recorder.record(frame1);
}
//然后录入音频
while ((frame2 = grabber2.grabFrame()) != null) {
recorder.record(frame2);
}
grabber1.stop();
grabber2.stop();
recorder.stop();
return isCreated;
} catch (Exception e) {
logger.error(e.getMessage());
} finally {
try {
if (recorder != null) {
recorder.release();
}
if (grabber1 != null) {
grabber1.release();
}
if (grabber2 != null) {
grabber2.release();
}
} catch (FrameRecorder.Exception e) {
e.printStackTrace();
}
}
return isCreated;
}
}