Home > Back-end >  Consult, when using FFmpegFrameRecorder realize video image synthesis, setting the highest quality o
Consult, when using FFmpegFrameRecorder realize video image synthesis, setting the highest quality o

Time:11-18

Set video buy any of the following lines will make video last few seconds (my side is 4 seconds total 20 seconds) is missing, the video size is bigger,
Recorder. SetVideoBitrate (8000000);
Recorder. SetVideoQuality (0, l);
Source:
Package com. Example. The demo. The controller;
The import com. Example. Demo. Service. ImageToVideoService;
The import com. Example. Demo. Util. SnowflakeIdWorker;
The import IO. Swagger. Annotations. Api;
The import IO. Swagger. Annotations. ApiOperation;
The import lombok. AllArgsConstructor;
The import org. Springframework. Web. Bind. The annotation. GetMapping;
The import org. Springframework. Web. Bind. The annotation. RequestMapping;
The import org. Springframework. Web. Bind. The annotation. RestController;

The import javax.mail. The annotation. The Resource;

/* *
* @ author Administrator
*/
@ Api (tags="video image synthesis")
@ RestController
@ RequestMapping ("/imageToVideo ")
@ AllArgsConstructor
Public class ImageToVideoTestController {

@ the Resource
Private ImageToVideoService ImageToVideoService;
@ the Resource
Private SnowflakeIdWorker SnowflakeIdWorker;

@ ApiOperation (value="https://bbs.csdn.net/topics/for video path")
@ GetMapping
Public void generateVideo () throws the Exception {
String picturesPath="D: \ \ other \ \ imageToVideo \ \ picture";
String audioPath="D: \ \ other \ \ imageToVideo \ \ audio \ \ BGM mp3";
The String fileName=snowflakeIdWorker. NextId () + "";
String videoPath="D: \ \ other \ \ imageToVideo \ \ video \ " + fileName + "mp4";
ImageToVideoService. GeGenerateVideoPath (picturesPath audioPath, videoPath);
}
}


Package com. Example. Demo. Service;

The import org. Bytedeco. Javacpp. Avcodec;
The import org. Bytedeco. Javacpp. Avutil;
The import org. Bytedeco. Javacpp. Opencv_core;
The import org. Bytedeco. Javacv. *;
The import org. Springframework. Stereotype. Service;

The import javax.mail. Imageio. Imageio;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

The 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 {
The File File=new File (picturesPath);
The File [] fileList=File. ListFiles ();
List MaxWHList=getImageMaxWH (fileList);
System. The out. Println (" width="+ maxWHList. Get (0) +" height="+ maxWHList. Get (1));
//grab audio frame
FrameGrabber audioFrames=new FFmpegFrameGrabber (audioPath);
AudioFrames. Start ();
//
2 frames per secondInt frameRate=2;
//create a recording fileName can be a local file (automatically created), can also be a RTMP path (published to the streaming media server)
//audioChannels=2 (stereo); 1 (mono); 0 (no audio)
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 (0, l);
Recorder. The start ();

//image processing variable
OpenCVFrameConverter. ToIplImage converter=new OpenCVFrameConverter. ToIplImage ();
Opencv_core. Mat Mat;
//a 20 seconds video recording
//such as: 30 FPS, covering 20 seconds video, you need to call record () 30 * 20=600 times,
For (int I=0, the index=0; i <20; I++) {
//one second is 2 (frameRate) frame should record twice so
For (int j=0; J & lt; FrameRate. J++) {
Mat=imread (fileList [index] getPath (), 1);
Recorder. Record (converter can convert (mat));
//release the memory
Mat. Release ();
index++;
}
}
Frame frameAudio;
//input audio
While ((frameAudio=audioFrames grabFrame ())!=null) {
Recorder. Record (frameAudio);
}
AudioFrames. Stop ();
AudioFrames. Release ();
Recorder. Stop ();
Recorder. The release ();
}

/* *
* description: get all the pictures the largest wide high
* @ param files
* @ return Java. Util. List
*/
Public List GetImageMaxWH (File [] files) throws IOException {
BufferedImage BufferedImage=null;
Int maxWidth=0;
Int maxHeight=0;
List MaxWHList=new ArrayList (a);
For (the File File: files) {
BufferedImage=ImageIO. Read (file);
If (bufferedImage. GetWidth () & gt; MaxWidth) {
MaxWidth=bufferedImage. GetWidth ();
}
If (bufferedImage. GetHeight () & gt; MaxHeight) {
MaxHeight=bufferedImage. GetHeight ();
}
}
If (bufferedImage!=null) {
BufferedImage. Flush ();
}
//picture width: must be divided exactly by 32
High//picture: must be divisible by 2
MaxWidth=32 maxWidth %==0? MaxWidth: maxWidth - maxWidth % 32 + 32;
MaxHeight=maxHeight % 2==0? MaxHeight: maxHeight - maxHeight % 2 + 2;
MaxWHList. Add (maxWidth);
MaxWHList. Add (maxHeight);
Return maxWHList;
}
}