opencv for java videowriter can't open

|▌冷眼眸甩不掉的悲伤 提交于 2021-01-28 18:00:56

问题


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() returns false. No file is created.

I'm suspecting it's something wrong with 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.


回答1:


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")



回答2:


Problem is codec, try this.

int fourcc = VideoWriter.fourcc('m','j','p','g');
Size frameSize = new Size((int) videoCapture.get(Videoio.CAP_PROP_FRAME_WIDTH),(int) videoCapture.get(Videoio.CAP_PROP_FRAME_HEIGHT));
VideoWriter videoWriter = new VideoWriter("testfile.avi", fourcc, 20, frameSize, true);
 if (videoCapture.read(frame))
      videoWriter.write(frame);


来源:https://stackoverflow.com/questions/36041735/opencv-for-java-videowriter-cant-open

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!