0

I would like to save a file (avi) of some frames (in this case, 600) from my webcam using java opencv, but i have no idea how to do that, the code below gives me the avi file specified but with size 0, no frames inside whatsover, also in the directory i have those 600 frames in jpeg's.

It's important to use java for that, no python.

 Mat frame = new Mat();
    VideoWriter writer = new VideoWriter("c:/opencv/vid.avi", VideoWriter.fourcc('X','2','6','4'), 30 ,frame.size(), true);

    videoCapture = new VideoCapture();
    videoCapture.open(0);

    videoCapture.set(Videoio.CAP_PROP_FRAME_WIDTH, 1280);
    videoCapture.set(Videoio.CAP_PROP_FRAME_HEIGHT, 720);

    while (true){
        videoCapture.read(frame);
        if (!frame.empty())
            break;
    }

    int frameNo = 0;
    while (frameNo < 600){

        videoCapture.read(frame);
        writer.write(frame);
        Imgcodecs imageCodecs = new Imgcodecs();
        String file = "c:/opencv/i" + frameNo + ".jpg";
        Imgcodecs.imwrite(file,frame);
        frameNo++;
    }

    videoCapture.release(); // release device
2
  • 1
    In your second line you specified your video sizes as frame.size() what is frame, how did you initialize it. That sizes should be equal to resolution(width and height) which is desired video size. Commented Jun 13, 2020 at 7:20
  • Your code arrangement seems to need re-arrange and the initialization of the frame is also looks to have problem which @YunusTemurlenk also mentioned. Commented Jun 13, 2020 at 9:36

1 Answer 1

2

You didn't mention which openCV version are you using, I use OpenCV-3.4.2

AVI container use DIVX codec reference: https://wiki.videolan.org/DivX/

I modify a bit your code:

Mat frame = new Mat();
VideoWriter writer = new VideoWriter("c:/opencv/vid.avi", VideoWriter.fourcc('D', 'I', 'V', 'X'), 30, new Size(videoCapture.get(Videoio.CAP_PROP_FRAME_WIDTH), videoCapture.get(Videoio.CAP_PROP_FRAME_HEIGHT)));
while(runnable)
{
    if(videoCapture.grab())
    {
        try
        {
            //Decodes and returns the grabbed video frame.
            videoCapture.retrieve(frame);
            //encode to .jpg the frame to a MatOfByte
            Imgcodecs.imencode(".jpg", frame, mem);
            //read into an Image
            Image im = ImageIO.read(new ByteArrayInputStream(mem.toArray()));
            //Draw image to a Jpanel
            BufferedImage buff = (BufferedImage) im;
            Graphics g = jPanel1.getGraphics();
            g.drawImage(buff, 0, 0, getWidth(), getHeight(), 0, 0, buff.getWidth(), buff.getHeight(), null);
            //record the frame
            writer.write(frame);
        }
        catch(Exception ex)
        {
            System.out.println("Error");
            ex.printStackTrace();
        }
    }
}
videoCapture.release(); // release device
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.