width 使用 iplimage 如果 nta public idt 需要 截取

話不多說,直接上代碼:

/**
 * Created by jinwentao on 2018/4/8
 */
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.File;

import javax.imageio.ImageIO;

import org.bytedeco.javacpp.opencv_core.IplImage;
import org.bytedeco.javacv.FFmpegFrameGrabber;
import org.bytedeco.javacv.Frame;

public class demo {

    /**
     * 獲取指定視頻的幀並保存為圖片至指定目錄
     * @param videofile  源視頻文件路徑
     * @param framefile  截取幀的圖片存放路徑
     * @throws Exception
     */
    public static void fetchFrame(String videofile, String framefile)
            throws Exception {
        long start = System.currentTimeMillis();
        File targetFile = new File(framefile);
        FFmpegFrameGrabber ff = new FFmpegFrameGrabber(videofile);
        ff.start();
        int lenght = ff.getLengthInFrames();
        int i = 0;
        Frame f = null;
        while (i < lenght) {
            // 過濾前100幀
            f = ff.grabFrame();
            if ((i > 100) && (f.image != null)) {
                break;
            }
            i++;
        }
        IplImage img = f.image;
        int owidth = img.width();
        int oheight = img.height();
        // 對截取的幀進行等比例縮放
        int width = 1600;
        int height = (int) (((double) width / owidth) * oheight);
        BufferedImage bi = new BufferedImage(width, height, BufferedImage.TYPE_3BYTE_BGR);
        bi.getGraphics().drawImage(f.image.getBufferedImage().getScaledInstance(width, height, Image.SCALE_SMOOTH),
                0, 0, null);
        ImageIO.write(bi, "jpg", targetFile);
        //ff.flush();
        ff.stop();
//        System.out.println(System.currentTimeMillis() - start);
    }

    public static void main(String[] args) {
        try {
            demo.fetchFrame("http://101.132.110.90/group1/M00/00/05/rBN4LFq8p5SAJT0wA5k4vpHKf7Q325.mp4", "D:\\images\\test2.jpg");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

在使用過程中,如果需要獲取本地視頻的第一幀圖片的話,可以直接將main方法中的網絡地址換成本地地址即可

  

 "D:\\images\\test2.jpg":圖片存儲路徑

ffmpeg獲取網絡視頻第一幀圖片