Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Extracting Frames Opencv Java

Hello,

i am trying to extract all frames out of a video with opencv java.

The code seems to work so far in giving me the correct number of all frames and the fps, but i just get the same first frame 4000 times.

public class VideoCap {
public static void main (String args[]){
System.loadLibrary(Core.NATIVE_LIBRARY_NAME);

VideoCapture cap = new VideoCapture();

    String input = "/Users/Jan/Desktop/Video/Java.mp4";
    String output = "/Users/Jan/Desktop/Video/Output";

cap.open(input);

    int video_length = (int) cap.get(Videoio.CAP_PROP_FRAME_COUNT);
    int frames_per_second = (int) cap.get(Videoio.CAP_PROP_FPS);
    int frame_number = (int) cap.get(Videoio.CAP_PROP_POS_FRAMES);

Mat frame = new Mat();

if (cap.isOpened())
{
        System.out.println("Video is opened");
        System.out.println("Number of Frames: " + video_length);
        System.out.println(frames_per_second + " Frames per Second");
        System.out.println("Converting Video...");

    cap.read(frame);

    while(frame_number <= video_length)
    {
        Imgcodecs.imwrite(output + "/" + frame_number +".jpg", frame);
        frame_number++;
    }
    cap.release();

        System.out.println(video_length + " Frames extracted");

}

    else
    {
        System.out.println("Fail");
    }
} }