Search code examples
javajavacv

Why is Bufferedimage equaling null after a few iterations?


After a few iterations through my for loop, the Bufferedimage variable just receives a null. The first 2-3 times are usually fine.

I tried different mp4 files to see if it was just the videos having issues.

public void vidimg()throws IOException{
    Java2DFrameConverter converter = new Java2DFrameConverter();
    FFmpegFrameGrabber frameGrabber = new FFmpegFrameGrabber(mp4Path);

    Frame frame;
    int imgNum = 0;

    frameGrabber.start();

    System.out.println("Video has " + frameGrabber.getLengthInFrames() + " frames and has frame rate of "+ frameGrabber.getFrameRate());

    try {
        for(int i=frameJump;i<=frameGrabber.getLengthInFrames();i+=frameJump){
            imgNum++;
            frameGrabber.setFrameNumber(i);
            frame = frameGrabber.grab();
            BufferedImage bi = converter.convert(frame);
            String path = imgPath + File.separator + imgNum + ".jpg";
            ImageIO.write(bi, imgType, new File(path));
        }
        frameGrabber.stop();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

I expected to receive images every 2 frames of the video, but I received a null error for Bufferedimage.


Solution

  • did you consider that it's an information frame that has no graphical content?

    Some data streams have no graphical content as briefly mentioned here

    https://en.wikipedia.org/wiki/MPEG-4_Part_14

    More info can be found by looking at a better, more in-depth format specification

    This SO question has better pointers and links to the specification: MP4 File Format Specification