Extracting Frames Opencv Java [closed]
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");
}
} }
you're reading only one frame ;)
Could you elaborate on how to get the other frames? As far as i understood it, cap.read(frame) would iterate over the frames and give me one after the other. I am not really used to java but my teacher wants it that way.