java获取视频一帧

1、引入jar包,网上有引入0.8版本的jar包的在本地测试没有问题,但是发布到linux上会报错
java.lang.NoClassDefFoundError: Could not initialize class org.bytedeco.javacv.FFmpegFrameGrabber
所以这里引入的是1.4.2

   org.bytedeco
   javacv-platform
   1.4.2
public void fetchFrame( String videofile)throws Exception {
    FFmpegFrameGrabber ff = new FFmpegFrameGrabber(videofile);//videofile视频路径,我用的是网络路径
    ff.start();
    int lenght = ff.getLengthInFrames();
    int i = 0;
    Frame f = null;
    while (i < lenght) {
        // 过滤前5帧,避免出现全黑的图片
        f = ff.grabFrame();
        if ((i > 5) && (f.image != null)) {
            break;
        }
        i++;
    }
    int owidth = f.imageWidth ;
    int oheight = f.imageHeight ;
    // 对截取的帧进行等比例缩放
    int width = 800;
    int height = (int) (((double) width / owidth) * oheight);
    Java2DFrameConverter converter =new Java2DFrameConverter();
    BufferedImage fecthedImage =converter.getBufferedImage(f);
    BufferedImage bi = new BufferedImage(width, height, BufferedImage.TYPE_3BYTE_BGR);
    bi.getGraphics().drawImage(fecthedImage.getScaledInstance(width, height, Image.SCALE_SMOOTH),
            0, 0, null);
    File targetFile = new File("D:\\new\\test.jpg");
    ImageIO.write(bi, "jpg", targetFile);
    //ff.flush();
    ff.stop();
}

参考:https://blog.csdn.net/syz_mumu/article/details/77203247

你可能感兴趣的:(java获取视频一帧)