Current location - Plastic Surgery and Aesthetics Network - Plastic surgery and medical aesthetics - How java synthesizes multiple JPG images into video files, such as avi format or mpg format.
How java synthesizes multiple JPG images into video files, such as avi format or mpg format.
I have done the function of combining pictures into videos before, and the code is probably like this. You can have a look.

/**

* picture synthesis video

* @param mp4SavePath video save path

* @param imageDir picture address

* @param rate This can be understood as the number of pictures played by the video per second.

*/

public static boolean jpg tomp 4(String MP4 save path,String imageDir,double rate) {

FFmpegFrameRecorder = null

Boolean flag = true;

Try {

file[]files = file utils . filesort(imageDir);

int[]width array = new int[files . length];

int[]height array = new int[files . length];

/**

* Get the maximum width and height of the synthesized video picture, so as to avoid inconsistent picture proportions and poor final synthesis effect.

*/

for(int I = 0; I & ltfiles.lengthi++) {

buffered image buffered image = imageio . read(files[I]);

width array[I]= buffered image . getwidth();

height array[I]= buffered image . get height();

}

/**

* This method is mainly to prevent the picture ratio from reaching the requirement of video composition ratio. If the following conditions are not met, the video cannot be played.

* Picture width: It must be divisible by 32.

* Image height: It must be divisible by 2.

*/

int[]maxWH = getImgMaxWH(width array,height array);

recorder = new FFmpegFrameRecorder(MP 4 save path,maxWH[0],maxWH[ 1]);

recorder . setvideocode(avcodec。 AV _ CODEC _ ID _ H264);

/**

* Video quality: At present, 25-30 is the clearest, and the video quality range seems to be 0-40, so you can measure it yourself slowly.

*/

recorder . setvideoquality(25);

recorder . set format(" MP4 ");

recorder . set frame rate(rate & gt; 0 ? Rate:1);

recorder . setpixelformat(0);

Recorder.start ();

OpenCVFrameConverter。 toip image converter = new OpenCVFrameConverter。 toip image();

/**

* Synthetic video

*/

for(int I = 0; I & ltfiles.lengthi++ ){

opencv_core。 IPL image image = cvLoadImage(files[I].getPath());

recorder . record(conveter . convert(image));

opencv _ core . cvreleaseimage(image);

}

Logger.info ("successful synthesis");

Catch (exception e) {

e . printstacktrace();

flag = false

Logger.error ("synthesis failed");

} Finally {

Try {

If (tape recorder! = null){

Recorder. stop ();

Recorder.release ();

}

} catch (FrameRecorder。 Exception e) {

e . printstacktrace();

}

}

Return flag;

}