javacv抽指定帧

pom.xml:

        <dependency>
            <groupId>org.bytedecogroupId>
            <artifactId>javacvartifactId>
            <version>1.4.4version>
            <exclusions>
                <exclusion>
                    <groupId>org.bytedecogroupId>
                    <artifactId>javacppartifactId>
                exclusion>
                <exclusion>
                    <groupId>org.bytedeco.javacpp-presetsgroupId>
                    <artifactId>flycaptureartifactId>
                exclusion>
                <exclusion>
                    <groupId>org.bytedeco.javacpp-presetsgroupId>
                    <artifactId>libdc1394artifactId>
                exclusion>
                <exclusion>
                    <groupId>org.bytedeco.javacpp-presetsgroupId>
                    <artifactId>libfreenectartifactId>
                exclusion>
                <exclusion>
                    <groupId>org.bytedeco.javacpp-presetsgroupId>
                    <artifactId>libfreenect2artifactId>
                exclusion>
                <exclusion>
                    <groupId>org.bytedeco.javacpp-presetsgroupId>
                    <artifactId>librealsenseartifactId>
                exclusion>
                <exclusion>
                    <groupId>org.bytedeco.javacpp-presetsgroupId>
                    <artifactId>videoinputartifactId>
                exclusion>
                <exclusion>
                    <groupId>org.bytedeco.javacpp-presetsgroupId>
                    <artifactId>opencvartifactId>
                exclusion>
                <exclusion>
                    <groupId>org.bytedeco.javacpp-presetsgroupId>
                    <artifactId>tesseractartifactId>
                exclusion>
                <exclusion>
                    <groupId>org.bytedeco.javacpp-presetsgroupId>
                    <artifactId>leptonicaartifactId>
                exclusion>
                <exclusion>
                    <groupId>org.bytedeco.javacpp-presetsgroupId>
                    <artifactId>flandmarkartifactId>
                exclusion>
                <exclusion>
                    <groupId>org.bytedeco.javacpp-presetsgroupId>
                    <artifactId>artoolkitplusartifactId>
                exclusion>
            exclusions>
        dependency>
        <dependency>
            <groupId>org.bytedecogroupId>
            <artifactId>javacv-platformartifactId>
            <version>1.4.4version>
            <exclusions>
                <exclusion>
                    <groupId>org.bytedecogroupId>
                    <artifactId>javacvartifactId>
                exclusion>
                <exclusion>
                    <groupId>org.bytedeco.javacpp-presetsgroupId>
                    <artifactId>flycapture-platformartifactId>
                exclusion>
                <exclusion>
                    <groupId>org.bytedeco.javacpp-presetsgroupId>
                    <artifactId>libdc1394-platformartifactId>
                exclusion>
                <exclusion>
                    <groupId>org.bytedeco.javacpp-presetsgroupId>
                    <artifactId>libfreenect-platformartifactId>
                exclusion>
                <exclusion>
                    <groupId>org.bytedeco.javacpp-presetsgroupId>
                    <artifactId>libfreenect2-platformartifactId>
                exclusion>
                <exclusion>
                    <groupId>org.bytedeco.javacpp-presetsgroupId>
                    <artifactId>librealsense-platformartifactId>
                exclusion>
                <exclusion>
                    <groupId>org.bytedeco.javacpp-presetsgroupId>
                    <artifactId>videoinput-platformartifactId>
                exclusion>
                <exclusion>
                    <groupId>org.bytedeco.javacpp-presetsgroupId>
                    <artifactId>opencv-platformartifactId>
                exclusion>
                <exclusion>
                    <groupId>org.bytedeco.javacpp-presetsgroupId>
                    <artifactId>tesseract-platformartifactId>
                exclusion>
                <exclusion>
                    <groupId>org.bytedeco.javacpp-presetsgroupId>
                    <artifactId>leptonica-platformartifactId>
                exclusion>
                <exclusion>
                    <groupId>org.bytedeco.javacpp-presetsgroupId>
                    <artifactId>flandmark-platformartifactId>
                exclusion>
                <exclusion>
                    <groupId>org.bytedeco.javacpp-presetsgroupId>
                    <artifactId>artoolkitplus-platformartifactId>
                exclusion>
            exclusions>
        dependency>

java:


import org.bytedeco.javacv.FFmpegFrameGrabber;
import org.bytedeco.javacv.Frame;
import org.bytedeco.javacv.Java2DFrameConverter;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;

public class VideoFirstImage {
    /**
     * 获取指定视频的帧并保存为图片至指定目录
     *
     * @param videofile 源视频文件路径
     * @param picPath 截取帧的图片存放路径
     * @throws Exception
     */
    public static File fetchFrame(String videofile, String picPath) throws Exception {
        System.out.println(videofile);
        FFmpegFrameGrabber ff = new FFmpegFrameGrabber(videofile);
        ff.start();
        int lenght = ff.getLengthInFrames();
        System.out.println("视频长度:"+lenght);
        int i = 0;
        Frame f = ff.grabFrame();
        for (int j = 0; j < 50; j++) {
            f = ff.grabImage();//循环一次就是一帧
        }
        Java2DFrameConverter converter = new Java2DFrameConverter();
        BufferedImage srcImage = converter.getBufferedImage(f);
        int srcImageWidth = srcImage.getWidth();
        int srcImageHeight = srcImage.getHeight();

        // 对截图进行等比例缩放(缩略图)
        int width = 480;
        int height = (int) (((double) width / srcImageWidth) * srcImageHeight);
        BufferedImage thumbnailImage = new BufferedImage(width, height, BufferedImage.TYPE_3BYTE_BGR);
        thumbnailImage.getGraphics().drawImage(srcImage.getScaledInstance(width, height, Image.SCALE_SMOOTH), 0, 0, null);

        File picFile = new File(picPath+videofile.substring(videofile.lastIndexOf('\\'),videofile.lastIndexOf('.'))+".jpg");
        ImageIO.write(thumbnailImage, "jpg", picFile);

        ff.stop();
        return picFile;
    }

    public static void main(String[] args) throws Exception {
        File file = fetchFrame("E:\\abc.mp4", "E:\\post\\");
//        FileInputStream is = new FileInputStream(file);
//        FileOutputStream fileOutputStream = new FileOutputStream();
    }
}

你可能感兴趣的:(javascript,java,spring)