Home > Software engineering >  How to save video into a file using OpenCV and Java?
How to save video into a file using OpenCV and Java?

Time:09-23

Using OpenCV and Java I could live stream the video from the camera with the Java JFrame application, that means accessing the camera and capturing the video from the camera works as well.

I want to save the video into a file, below is my code:

import ...;

public final class Main {
   
    VideoCapture videoCapture;
    Size size;
    Mat matrix;
    VideoWriter videoWriter;

    public Main() throws SocketException, UnknownHostException, IOException {
        System.load( "C:\\opencv\\build\\java\\x64\\"   Core.NATIVE_LIBRARY_NAME   ".dll");
        System.load( "C:\\opencv\\build\\bin\\opencv_videoio_ffmpeg453_64.dll");

        videoCapture = new VideoCapture(CAMERA_ID, CV_CAP_DSHOW);

        if (videoCapture.isOpened()) {
            Mat m = new Mat();
            videoCapture.read(m);

            int fourcc = VideoWriter.fourcc('H','2','6','4');
            // int fourcc = VideoWriter.fourcc('x','2','6','4'); // Have tried, did not work.
            // int fource = (int) videoCapture.get(Videoio.CAP_PROP_FOURCC); // Have tried, did not work.
            // int fourcc = VideoWriter.fourcc('m','j','p','g'); // Have tried, did not work.
            // int fourcc = VideoWriter.fourcc('D', 'I', 'V', 'X'); // Have tried, did not work.

            double fps = videoCapture.get(Videoio.CAP_PROP_FPS);
            Size s =  new Size((int) videoCapture.get(Videoio.CAP_PROP_FRAME_WIDTH), (int) videoCapture.get(Videoio.CAP_PROP_FRAME_HEIGHT));
             videoWriter = new VideoWriter("C:\\Users\\oconnor\\Documents\\bbbbbbbbbbbbb_1000.h264", fourcc, fps, s, true);
            // Have tried, did not work
            // videoWriter = new VideoWriter("C:\\Users\\oconnor\\Documents\\bbbbbbbbbbbbb_1000.avi", fourcc, fps, s, true);
            // videoWriter = new VideoWriter("C:\\Users\\oconnor\\Documents\\bbbbbbbbbbbbb_1000.Mjpeg", fourcc, fps, s, true);
            // videoWriter = new VideoWriter("C:\\Users\\oconnor\\Documents\\bbbbbbbbbbbbb_1000.mjpeg", fourcc, fps, s, true);

            while (videoCapture.read(m)) {
                 videoWriter.write(m);
                 
                 // Have tried, did not work.
                 // int i = 0;
                 // Mat clonedMatrix = m.clone();
                 // Imgproc.putText(clonedMatrix, ("frame"   i), new Point(100,100), 1, 2, new Scalar(200,0,0), 3);
                 // videoWriter.write(clonedMatrix);
                 // i  ;
            }
        }

        videoCapture.release();
        videoWriter.release();
    }
}

When I ran the above code, there was no error at all and the file bbbbbbbbbbbbb_1000.h264 has been created as well, but the VLC Media Player and the Windows Media Player could not play the video.

I am using Windows 10. Below is the OpenCV 4.5.3 build information. I did not build the library from sources by myself.

Video I/O:
DC1394:                      NO
FFMPEG:                      YES (prebuilt binaries)
  avcodec:                   YES (58.134.100)
  avformat:                  YES (58.76.100)
  avutil:                    YES (56.70.100)
  swscale:                   YES (5.9.100)
  avresample:                YES (4.0.0)
GStreamer:                   NO
DirectShow:                  YES
Media Foundation:            YES
  DXVA:                      NO

How can I save the video into a file using OpenCV and Java. It can be in Mjpeg or h264 or whatever it is as long as I can have a file to play back.

CodePudding user response:

Just got it work to create an AVI file. To create an AVI file, the fourcc must be as following.

int fourcc = VideoWriter.fourcc('M','J','P','G');

The file name must have .avi as the extension.

In some OS, videoCapture.get(Videoio.CAP_PROP_FPS) might return 0.0. Set a default frame per second if videoCapture.get(Videoio.CAP_PROP_FPS) returns 0.0. I've set it to 25;

That's it. Yet I could not write the video frames into a .h264 file. If anyone could write the video frames into a .h264, please kindly share the knowledge!

CodePudding user response:

According to getBuildInformation(), your OpenCV was built with FFMPEG, so that's good.

The issue is the file extension you tried to use.

The file extension .h264 does not represent an actual container format. .h264 is the extension given to a naked H.264 stream that sits in a file without a proper container structure.

You need to give your file the extension .mp4 or some other suitable container format. MKV would be suitable. MPEG transport streams (.ts) would also be suitable.

Further, H264 may not be a valid "fourcc". avc1 is a valid fourcc.

OpenCV has builtin support for the AVI container (.avi) and an MJPEG video codec (MJPG). That will always work. More containers and codecs may be available if OpenCV was built with ffmpeg or it can use Media APIs of the operating system.

Here's some python but the API usage is the same:

import numpy as np
import cv2 as cv

# fourcc = cv.VideoWriter.fourcc(*"H264")# might not be supported
fourcc = cv.VideoWriter.fourcc(*"avc1") # that's the proper fourcc by standard
# calling with (*"avc1") is equivalent to calling with ('a', 'v', 'c', '1')

(width, height) = (640, 360)
vid = cv.VideoWriter("foo.mp4", fourcc=fourcc, fps=16, frameSize=(width, height), isColor=True)

assert vid.isOpened()

frame = np.zeros((height, width, 3), np.uint8)
for k in range(256):
    frame[:,:] = (0, k, 255) # BGR order
    vid.write(frame)

vid.release() # finalize the file (write headers and whatnot)

This video will be 256 frames, 16 fps, 16 seconds, and fade red to yellow.

It will also be MP4 and H.264.

  • Related