javaCV learning -1- environment to build and test multiple pictures to synthesize a mp4 video

Recently, I taught myself how to process audio and video in java language.

When I was studying, I found a piece of code on the Internet and tested multiple pictures to synthesize an mp4 video.

The code is:

import org.bytedeco.ffmpeg.global.avcodec;
import org.bytedeco.ffmpeg.global.avutil;
import org.bytedeco.javacv.FFmpegFrameRecorder;
import org.bytedeco.javacv.FrameRecorder;
import org.bytedeco.javacv.Java2DFrameConverter;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.util.HashMap;
import java.util.Map;

/**
 * Description:
 *
 * @author 诸葛小猿
 * @date 2020-10-24
 */
public class Test {
    
    

    public static void main(String[] args) throws Exception {
    
    
        //合成的MP4
        String mp4SavePath = "C:\\Users\\WuXiaoLong\\Desktop\\img\\img.mp4";
        //图片地址 这里面放了22张图片
        String img = "C:\\Users\\WuXiaoLong\\Desktop\\img";
        int width = 1600;
        int height = 900;
        //读取所有图片
        File file = new File(img);
        File[] files = file.listFiles();
        Map<Integer, File> imgMap = new HashMap<Integer, File>();
        int num = 0;
        for (File imgFile : files) {
    
    
            imgMap.put(num, imgFile);
            num++;
        }
        createMp4(mp4SavePath, 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);
        //设置视频编码层模式
        recorder.setVideoCodec(avcodec.AV_CODEC_ID_H264);
        //设置视频为25帧每秒
        recorder.setFrameRate(25);
        //设置视频图像数据格式
        recorder.setPixelFormat(avutil.AV_PIX_FMT_YUV420P);
        recorder.setFormat("mp4");
        try {
    
    
            recorder.start();
            Java2DFrameConverter converter = new Java2DFrameConverter();
            //录制一个22秒的视频
            for (int i = 0; i < 22; 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) {
    
    
            e.printStackTrace();
        } finally {
    
    
            //最后一定要结束并释放资源
            recorder.stop();
            recorder.release();
        }
    }

}

According to the introduction of the blog, all kinds of Sao operations are always unworkable. I read a few blogs in a row, all of which were given to a piece of code, given to a jar package, and a test was all overturned.

Anyone who learns javaVC knows that there is very little information related to javaVC on the Internet, and many bloggers are not professionally doing computer vision processing. The blogs written are either very simple or copy a piece of code, which is not very referenced.

This reminds me of a sentence: The best learning address is the official git repository, and there is no one.

Then take a look at the official git: https://github.com/bytedeco/javacv

According to the official guidance, just introduce the maven dependency.

  <dependency>
    <groupId>org.bytedeco</groupId>
    <artifactId>javacv-platform</artifactId>
    <version>1.5.4</version>
  </dependency>

In the actual test, it was very slapped:

Exception in thread "main" java.lang.NoClassDefFoundError: Could not initialize class org.bytedeco.ffmpeg.global.avutil
	at java.lang.Class.forName0(Native Method)
	at java.lang.Class.forName(Class.java:348)
	at org.bytedeco.javacpp.Loader.load(Loader.java:1190)
	at org.bytedeco.javacpp.Loader.load(Loader.java:1123)
	at org.bytedeco.ffmpeg.avformat.Write_packet_Pointer_BytePointer_int.<clinit>(Write_packet_Pointer_BytePointer_int.java:21)
	at org.bytedeco.javacv.FFmpegFrameRecorder.<clinit>(FFmpegFrameRecorder.java:329)
	at com.wuxl.Image2Mp4.createMp4(Image2Mp4.java:48)
	at com.wuxl.Image2Mp4.main(Image2Mp4.java:43)

The key is that I opened this ffmpeg.jar to see that org.bytedeco.ffmpeg.global.avutilit exists. big head.

I guess it is because: this jar package is too big to download completely, so I deleted the jar file of org.bytedeco in the local maven warehouse and prepared to download it again.

bytedeco warehouse

Open this directory while downloading and keep staring at it, good guy, so many dependent jar packages. At the same time pay attention to the download speed of IDEA.

image-20201024151036734

After about half an hour, the download stopped. There is no error message in the code. So I hurried to run it again, the result is still'Could not initialize class org.bytedeco.ffmpeg.global.avutil'. what the F. . . . .

I'm stuck and can't go on. What should I do? Is the official guidance not good enough? ? ? Forget it, it's so difficult, don't do it. Silently lit a cigarette.

It feels uncomfortable if you don't make it out, and this gives up the spirit of exploration that doesn't meet the programmer's expectations. . . After smoking a cigarette, continue.

Although the error is still reported, I still believe that there is still something wrong with the javaCV environment. So I went to github to find answers.

image-20201024152322682

Seeing that there is a compiled zip on github, I decided to use the jar in the zip package to import it directly into the project instead of using maven coordinates.

image-20201024152354773

So I downloaded the 755M zip package, then unzipped it, deleted the original maven coordinates, and manually imported all the jar packages in the zip package:

image-20201024152806324

Seeing 6 above, it means that the import is complete.

Let's run it again, the result. . . . Hahahahahahaha, it succeeded. Oye! ! ! !

Let me show you the result of the mp4 synthesized by the picture, the color of the video is not very good, and sometimes I will do related tuning.

javaCV combines pictures into video

The above is to import all the jar packages in the zip package. In fact, you don't need all of them. You can select some packages to import according to the specific development platform (windows/linux/mac) and the bit number of the operating system (32/64).

Finally, when I saw a blog on the Internet, I felt that it was right. Share some of the content:

There are many tutorials on the construction of javacv environment on the Internet, and then they are all similar and small, first teach you to build the opencv environment, and then give you an example to test whether your own opencv environment is successfully built. Speaking of this, I want to scold people. Those teachers who need to build an opencv environment on a computer, don't you know that after javacv0.8 version, there is no need to build an opencv environment? If you know it, please change the blog content as soon as possible. Don't delay the time of us java developers, okay? I read your tutorial, but it took two days to build an opencv environment...

If you have any questions, you can pay attention to the official account exchange.

Finished, call it a day!

[ Dissemination of knowledge, sharing of value ], thank you friends for your attention and support. I am [ Zhuge Xiaoyuan ], an Internet migrant worker struggling in hesitation.

Guess you like

Origin blog.csdn.net/wuxiaolongah/article/details/109260947