0

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.

Hong Wang
  • 3
  • 2
  • did you consider that it's an information frame that has no graphical content? – Shark May 24 '19 at 16:32
  • No. I never considered that. I didn't even know that was possible. I'll add something to catch it and see if the program runs normally then. – Hong Wang May 24 '19 at 16:47
  • I just added a conditional to check if Bufferedimage was null. Now the program is running fine. Thank you for the help! Also, how do I marked this as answered? – Hong Wang May 24 '19 at 16:50
  • lemme convert this comment into an answer :) – Shark May 27 '19 at 10:03

1 Answers1

0

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

Shark
  • 6,513
  • 3
  • 28
  • 50