NullPointerException in FrameGrab.getFrame

不羁岁月 提交于 2019-12-12 04:34:55

问题


I would like to get frame from video using jcodec, but I get NullPointerException.

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
import org.jcodec.api.FrameGrab;
import org.jcodec.api.JCodecException;

public class App 
{
    public static void main( String[] args )
    {
        int frameNumber = 150;  

        try {
            BufferedImage frame = new BufferedImage(1, 1, BufferedImage.TYPE_INT_ARGB);

            try {
                frame = FrameGrab.getFrame(new File("/Users/AG/Downloads/video.mp4"), frameNumber);
            } catch (JCodecException ex) {
                Logger.getLogger(App.class.getName()).log(Level.SEVERE, null, ex);
            }

            ImageIO.write(frame, "png", new File("frame_150.png"));

        } catch (IOException ex) {
            Logger.getLogger(App.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}

Stacktrace:

Exception in thread "main" java.lang.NullPointerException
at org.jcodec.codecs.h264.decode.deblock.DeblockingFilter.calcBsH(DeblockingFilter.java:167)
at org.jcodec.codecs.h264.decode.deblock.DeblockingFilter.deblockFrame(DeblockingFilter.java:80)
at org.jcodec.codecs.h264.H264Decoder$FrameDecoder.decodeFrame(H264Decoder.java:101)
at org.jcodec.codecs.h264.H264Decoder.decodeFrame(H264Decoder.java:61)
at org.jcodec.api.specific.AVCMP4Adaptor.decodeFrame(AVCMP4Adaptor.java:40)
at org.jcodec.api.FrameGrab.decodeLeadingFrames(FrameGrab.java:184)
at org.jcodec.api.FrameGrab.seekToFramePrecise(FrameGrab.java:111)
at org.jcodec.api.FrameGrab.getFrame(FrameGrab.java:331)
at com.mycompany.video_extractor.App.main(App.java:27)

回答1:


Hard to say without seeing your stacktrace, but this line.-

ImageIO.write(frame, "png", new File("frame_150.png"));

may be throwing the Exception it frame happens to be null. I'd rewrite the code like this.-

try {
    BufferedImage frame = new BufferedImage(1, 1, BufferedImage.TYPE_INT_ARGB);
    frame = FrameGrab.getFrame(new File("/Users/AG/Downloads/video.mp4"), frameNumber);
    if (frame != null) {
        ImageIO.write(frame, "png", new File("frame_150.png"));
    } 

} catch (IOException ex) {
    Logger.getLogger(App.class.getName()).log(Level.SEVERE, null, ex);
} catch (JCodecException ex) {
    Logger.getLogger(App.class.getName()).log(Level.SEVERE, null, ex);
}

Notice that there's no sense in instantiating a new BufferedImage if you're reassigning it in the next line, and there's no sense in running the third line (ImageIO.write) if FrameGrab.getFrame returns null (which is most probably what's happening here).



来源:https://stackoverflow.com/questions/19344051/nullpointerexception-in-framegrab-getframe

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!