設視頻置以下任意一行都會使視頻最后幾秒(我這邊是4秒總的20秒)丟失。視頻大小確實變大了。
recorder.setVideoBitrate(8000000);
recorder.setVideoQuality(0L);
源代碼:
package com.example.demo.controller;
import com.example.demo.service.ImageToVideoService;
import com.example.demo.util.SnowflakeIdWorker;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
/**
* @author Administrator
*/
@Api(tags = "圖片合成視頻")
@RestController
@RequestMapping("/imageToVideo")
@AllArgsConstructor
public class ImageToVideoTestController {
@Resource
private ImageToVideoService imageToVideoService;
@Resource
private SnowflakeIdWorker snowflakeIdWorker;
@ApiOperation(value = "獲取視頻路徑")
@GetMapping
public void generateVideo()throws Exception{
String picturesPath = "D:\\other\\imageToVideo\\picture";
String audioPath = "D:\\other\\imageToVideo\\audio\\bgm.mp3";
String fileName = snowflakeIdWorker.nextId()+"";
String videoPath = "D:\\other\\imageToVideo\\video\\"+fileName+".mp4";
imageToVideoService.geGenerateVideoPath(picturesPath,audioPath,videoPath);
}
}
package com.example.demo.service;
import org.bytedeco.javacpp.avcodec;
import org.bytedeco.javacpp.avutil;
import org.bytedeco.javacpp.opencv_core;
import org.bytedeco.javacv.*;
import org.springframework.stereotype.Service;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import static org.bytedeco.javacpp.opencv_imgcodecs.imread;
/**
* @author Administrator
*/
@Service
public class ImageToVideoService {
public void geGenerateVideoPath(String picturesPath,String audioPath,String videoPath)
throws IOException {
File file = new File(picturesPath);
File[] fileList = file.listFiles();
List<Integer> maxWHList = getImageMaxWH(fileList);
System.out.println("寬 = "+maxWHList.get(0)+" 高 = "+maxWHList.get(1));
//抓取音頻幀
FrameGrabber audioFrames = new FFmpegFrameGrabber(audioPath);
audioFrames.start();
//每秒2幀
int frameRate = 2;
//創建錄制 fileName可以是本地檔案(會自動創建),也可以是RTMP路徑(發布到流媒體服務器)
// audioChannels = 2(立體聲);1(單聲道);0(無音頻)
FFmpegFrameRecorder recorder = new FFmpegFrameRecorder(videoPath, maxWHList.get(0), maxWHList.get(1), audioFrames.getAudioChannels());
recorder.setVideoCodec(avcodec.AV_CODEC_ID_H264);
recorder.setFormat("mp4");
recorder.setFrameRate(frameRate);
recorder.setPixelFormat(avutil.AV_PIX_FMT_YUV420P);
recorder.setVideoQuality(0L);
recorder.start();
//圖片處理的變數
OpenCVFrameConverter.ToIplImage converter = new OpenCVFrameConverter.ToIplImage();
opencv_core.Mat mat;
//錄制一個20秒的視頻
//例如 : 30 FPS的情況下,要覆寫20秒的視頻,您需要呼叫record()30 * 20 = 600次。
for (int i = 0, index = 0; i < 20; i++) {
//一秒是2(frameRate)幀 所以要記錄2次
for (int j = 0; j < frameRate; j++) {
mat = imread(fileList[index].getPath(), 1);
recorder.record(converter.convert(mat));
// 釋放記憶體
mat.release();
index++;
}
}
Frame frameAudio;
//錄入音頻
while ((frameAudio = audioFrames.grabFrame()) != null) {
recorder.record(frameAudio);
}
audioFrames.stop();
audioFrames.release();
recorder.stop();
recorder.release();
}
/**
* description:獲取所有圖片最大的寬高
* @param files
* @return java.util.List<java.lang.Integer>
*/
public List<Integer> getImageMaxWH(File[] files) throws IOException {
BufferedImage bufferedImage = null;
int maxWidth = 0;
int maxHeight = 0;
List<Integer> maxWHList = new ArrayList<Integer>();
for (File file : files) {
bufferedImage = ImageIO.read(file);
if (bufferedImage.getWidth() > maxWidth) {
maxWidth = bufferedImage.getWidth();
}
if (bufferedImage.getHeight() > maxHeight) {
maxHeight = bufferedImage.getHeight();
}
}
if(bufferedImage != null){
bufferedImage.flush();
}
// 圖片寬:必須要被32整除
// 圖片高:必須要被2整除
maxWidth = maxWidth%32 == 0 ? maxWidth : maxWidth - maxWidth%32 + 32;
maxHeight = maxHeight%2 == 0 ? maxHeight : maxHeight - maxHeight%2 + 2;
maxWHList.add(maxWidth);
maxWHList.add(maxHeight);
return maxWHList;
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/223031.html
標籤:Java相關
上一篇:程式員何去何從?
下一篇:tomcat配置
