Ask Your Question
0

VideoWriter problem - video not playable (java)

asked 2019-06-23 05:19:14 -0600

rom gravatar image

Hi experts,

i am new here and don't find something simular. I have a Webcam (later 2) and want to get the stream saved as a video an my disk. I thought it would be very simple, but i have the problem, that the VideoWriter saves a video but it has no size and i can't play it.... Here is my source code

import java.awt.image.BufferedImage;
import java.awt.image.DataBufferByte;
import java.io.File;

import org.opencv.core.Mat;
import org.opencv.core.Size;
import org.opencv.videoio.VideoCapture;
import org.opencv.videoio.VideoWriter;
import org.opencv.videoio.Videoio;

public class main {

    public static void main(String[] args) {
        // TODO Auto-generated method stub

        System.load(new File("lib/opencv_java341.dll").getAbsolutePath());
        System.load(new File("lib/opencv_ffmpeg341_64.dll").getAbsolutePath());
        System.load(new File("lib/opencv_world341.dll").getAbsolutePath());

        Mat mat = new Mat();

        VideoCapture videoCapture = new VideoCapture("http://192.168.0.90/mjpg/video.mjpg");
        Size frameSize = new Size((int) videoCapture.get(Videoio.CAP_PROP_FRAME_WIDTH), (int)   videoCapture.get(Videoio.CAP_PROP_FRAME_HEIGHT));
        VideoWriter videoWriter = new VideoWriter("test.avi", VideoWriter.fourcc('x', '2','6','4'),
                        videoCapture.get(Videoio.CAP_PROP_FPS), frameSize, true);
        while (videoCapture.read(mat)) {
                    videoWriter.write(mat);         
                }
                videoCapture.release();
                videoWriter.release();
        }   
}

do i have to make a cut and let it work on the release or is it enough to press the stop button? Later i want to start it by pressing a key and stop it with an other key... But as long as it don't produce a playable video it makes no sense.

Do you have an idea?

Thank you ;)

rom

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
0

answered 2019-07-03 07:26:10 -0600

rom gravatar image

okay i got it myself ;) I will add the source code. It works in an loop but you could change that easily ;)

    import java.awt.event.KeyListener;
    import java.awt.image.BufferedImage;
import java.awt.image.DataBufferByte;
import java.io.File;

import org.opencv.core.Mat;
import org.opencv.core.Size;
import org.opencv.videoio.VideoCapture;
import org.opencv.videoio.VideoWriter;
import org.opencv.videoio.Videoio;

public class main {

    public static void main(String[] args) {
        // TODO Auto-generated method stub

        System.load(new File("lib/opencv_java341.dll").getAbsolutePath());
        System.load(new File("lib/opencv_ffmpeg341_64.dll").getAbsolutePath());
        System.load(new File("lib/opencv_world341.dll").getAbsolutePath());
        System.load(new File("lib/openh264-1.7.0-win64.dll").getAbsolutePath());

        Mat image = new Mat();

        VideoCapture videoCapture = new VideoCapture("http://192.168.0.90/mjpg/video.mjpg");
        Size frameSize = new Size((int) videoCapture.get(Videoio.CAP_PROP_FRAME_WIDTH), (int) videoCapture.get(Videoio.CAP_PROP_FRAME_HEIGHT));
        VideoWriter videoWriter = new VideoWriter("heuter.avi", VideoWriter.fourcc('x', '2','6','4'),
                        videoCapture.get(Videoio.CAP_PROP_FPS), frameSize, true);
        int i = 0;
        while (i < 1000) {
                    videoCapture.read(image);;
                    videoWriter.write(image); 
                    System.out.println(i);
                    i++;

                }
                videoWriter.release();
                videoCapture.release();
                System.out.println("hier");
    }

}
edit flag offensive delete link more

Question Tools

1 follower

Stats

Asked: 2019-06-23 05:19:14 -0600

Seen: 565 times

Last updated: Jul 03 '19