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?
CodePudding user response:
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("d", frameNumber) " of " String.format("d", frameCount) " frames");
frameGrabber.setFrameNumber(frameNumber);
Frame f = frameGrabber.grab();
BufferedImage bi = c.convert(f);
ImageIO.write(bi, "png", new File("Frame " String.format("d", frameNumber) "-" String.format("d", 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.
CodePudding user response:
To grab the frames from a video and convert them into a collection of BufferedImage objects in Java, you can use the javax.imageio package. This package provides a number of classes and methods for reading and writing image files, including the ImageIO class, which has a read method that can be used to read an image file and return it as a BufferedImage object.
Here is an example of how you can use the ImageIO class to read a series of frames from a video file and store them in a List:
import java.awt.image.BufferedImage;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import javax.imageio.ImageIO;
public class FrameGrabber {
public static void main(String[] args) {
// Set the path to the video file
String videoFilePath = "path/to/video/file.mp4";
// Create an empty list to store the frames
List<BufferedImage> frames = new ArrayList<>();
// Read the frames from the video file and add them to the list
try {
for (int i = 0; i < 100; i ) {
// Read the frame at the given index
BufferedImage frame = ImageIO.read(new File(videoFilePath "frame" i ".jpg"));
// Add the frame to the list
frames.add(frame);
}
} catch (IOException e) {
e.printStackTrace();
}
// The list of frames is now available to use
}
}
This code reads the first 100 frames of a video file and stores them in a List of BufferedImage objects. You can adjust the number of frames that you want to read by changing the loop limit.