【FAQ】解决报错Could not initialize class org.bytedeco.javacpp.avutil

蒋星雨
2023-12-01

在利用FFmpegFrameGrabber处理视频流,将视频一帧一帧取出来的过程中

遇到一个报错:Could not initialize class org.bytedeco.javacpp.avutil

网上查找资料发现是少引入了包

但是增加之后又会有其他的报错,如:Exception in thread "main" java.lang.NoClassDefFoundError: org/bytedeco/javacpp/PointerScope

查来查去,发现是由于依赖包之间不匹配导致,修改成一致之后解决:

我是引入了下面两个就没问题了:

        <dependency>
            <groupId>org.bytedeco</groupId>
            <artifactId>javacv</artifactId>
            <version>1.4.3</version>
        </dependency>

        <dependency>
            <groupId>org.bytedeco.javacpp-presets</groupId>
            <artifactId>ffmpeg-platform</artifactId>
            <version>4.0.2-1.4.3</version>
        </dependency>

可以从这里查看对应版本:

http://bytedeco.org/download/

 

附视频流截取帧的核心代码:

            FFmpegFrameGrabber frameGrabber = new FFmpegFrameGrabber(new File(System.getProperty("user.dir") + "/test.h264"));

            frameGrabber.setFrameRate(100);
            frameGrabber.setFormat("h264");
            frameGrabber.setVideoBitrate(15);
            frameGrabber.setVideoOption("preset", "ultrafast");
            frameGrabber.setNumBuffers(25000000);

            Java2DFrameConverter converter = new Java2DFrameConverter();

            frameGrabber.start();

            Frame frame = frameGrabber.grab();
            BufferedImage bufferedImage = converter.convert(frame);
            File file = new File(System.getProperty("user.dir") + "/temp/image_" + count + ".jpg");
            ImageIO.write(bufferedImage, "jpg", file);
            baos.flush();
            baos.close();

 

 类似资料: