问题
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