简体   繁体   中英

How do I extract all the frames from a video in Java?

I'm trying to grab the frames from a video and put them into a collection of BufferedImage so I can use the images later. I tried to use the code in the second answer for this question, but it wasn't really helpful because the code they provided only grabs the first frame. How would I extract all the frames and put them into a collection?

So, a little bit of Googling (and reading the source code), was I able to hobble together this basic concept.

The "obvious" solution was to loop over the frames in the video and extract them, the "un-obvious" solution was "how"?!

The two methods you need are FFmpegFrameGrabber#getLengthInFrames and FFmpegFrameGrabber#setFrameNumber , from there it's just a simple method of converting the current frame and writing it to a file (which has already been demonstrated from the previous question)

File videoFile = new File("Storm - 106630.mp4");
FFmpegFrameGrabber frameGrabber = new FFmpegFrameGrabber(videoFile.getAbsoluteFile());
frameGrabber.start();
Java2DFrameConverter c = new Java2DFrameConverter();
int frameCount = frameGrabber.getLengthInFrames();
for (int frameNumber = 0; frameNumber < frameCount; frameNumber++) {
    System.out.println("Extracting " + String.format("%04d", frameNumber) + " of " + String.format("%04d", frameCount) + " frames");
    frameGrabber.setFrameNumber(frameNumber);
    Frame f = frameGrabber.grab();
    BufferedImage bi = c.convert(f);
    ImageIO.write(bi, "png", new File("Frame " + String.format("%04d", frameNumber) + "-" + String.format("%04d", frameCount) + ".png"));
}
frameGrabber.stop();

Now, obviously, you could store the images into some kind of List in memory, just beware, depending on the size and length of the original video, this might run you into memory issues.

You can use OpenCV to extract frames in a video, try the below code. input is the video file path of your computer

 VideoCapture cap = new VideoCapture();

    String output = "resources/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);

    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 to images...");

        cap.read(frame);

        int frame_number = 0;

        while (cap.read(frame)) {
            Imgcodecs.imwrite(output + "/" + frame_number + ".jpg", frame);
            frame_number++;
        }

        cap.release();

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

    } else {
        processingStatus = "Failed";
        System.out.println(processingStatus);
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM