videowriter的opencv无法打开

videowriter的opencv无法打开

本文介绍了java videowriter的opencv无法打开的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 java 中使用 opencv3.1.0.我想将视频保存到文件,但视频编写器无法打开.我的代码如下:

I'm using opencv3.1.0 in java. I want to save video to file, but videowriter can't open. My code below:

Size size = new Size(capture.get(Videoio.CAP_PROP_FRAME_WIDTH), capture.get(Videoio.CAP_PROP_FRAME_HEIGHT));
double fps = capture.get(Videoio.CAP_PROP_FPS);
VideoWriter vw = new VideoWriter("/home/sify/1.mp4", VideoWriter.fourcc('X', '2', '6', '4'), fps, size, true);

vw.isOpened() 返回 false.没有创建文件.

vw.isOpened() returns false. No file is created.

我怀疑fourcc 有问题.

I'm suspecting it's something wrong with fourcc.

我尝试使用 H264/XVID/FMP4/MPEG,并尝试用 (int)capture.get(Videoio.CAP_PROP_FOURCC) 替换第二个参数,也无法正常工作.

I tried to use H264/XVID/FMP4/MPEG, and tried to replace the second parameter with (int)capture.get(Videoio.CAP_PROP_FOURCC), also not working.

推荐答案

openCV 下载包含动态链接库文件 opencv_ffmpeg343_64.dll 中的 mp4 编解码器.要使其对 JVM 可见,请在打开 VideoWriter 之前运行这样的行.

The openCV download contains the mp4 codec in the dynamic link library file opencv_ffmpeg343_64.dll. To make that visible to the JVM, run a lines like this before you open the VideoWriter.

System.setProperty("java.library.path", "C:\pathToFolderContainingDLL")
val fieldSysPath = ClassLoader::class.java.getDeclaredField("sys_paths")
fieldSysPath.isAccessible = true
fieldSysPath.set(null, null)
//next time path is accessed, the new path will be imported

System.loadLibrary("opencv_ffmpeg343_64")

这篇关于java videowriter的opencv无法打开的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-30 17:33