博主
258
258
258
258
专辑

使用Java从MP4文件中截取视频指定帧的图片

亮子 2023-01-17 14:25:12 6285 0 0 0

1、添加依赖

        <dependency>
            <groupId>org.bytedeco</groupId>
            <artifactId>javacv-platform</artifactId>
            <version>1.4.4</version>
        </dependency>

2、截图代码

    /**
     * 截取视频获得指定帧的图片
     *
     * @param video  源视频文件
     * @param picPath 截图存放路径
     */
    public static void getVideoPic(File video, String picPath) {
        FFmpegFrameGrabber ff = new FFmpegFrameGrabber(video);

        try {
            ff.start();

            String title = ff.getMetadata("title");
            String comment = ff.getMetadata("comment");
            System.out.println("title:"+title);
            System.out.println("comment:"+comment);

            Map<String, String> metadata = ff.getMetadata();
            System.out.println(metadata.toString());

            // 截取中间帧图片(具体依实际情况而定)
            int i = 0;
            int length = ff.getLengthInFrames();
            int middleFrame = length / 2;
            Frame frame = null;
            while (i < length) {
                frame = ff.grabFrame();
                if ((i > middleFrame) && (frame.image != null)) {
                    break;
                }
                i++;
            }
            // 截取的帧图片
            Java2DFrameConverter converter = new Java2DFrameConverter();
            BufferedImage srcImage = converter.getBufferedImage(frame);
            int srcImageWidth = srcImage.getWidth();
            int srcImageHeight = srcImage.getHeight();
            // 对截图进行等比例缩放(缩略图)
//            int width = 480;
//            int height = (int) (((double) width / srcImageWidth) * srcImageHeight);
            int width = srcImageWidth;
            int height = 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);
            ImageIO.write(thumbnailImage, "jpg", picFile);
            ff.stop();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 使用示例
     */
    @Test
    public void doSnapShot() {
        String video = "G:\\zhang-class\\2020-10-19\\2020-10-19讲解流程图.mp4";
        String picPath = "G:\\zhang-class\\2020-10-19\\2020-10-19讲解流程图.jpg";
        File file = new File(video);
        getVideoPic(file, picPath);
    }

3、获取视频信息

    public static void getMp4Info() {
        File video = new File("G:\\zhang-class\\2020-10-19\\2020-10-19讲解流程图.mp4");

        FFmpegFrameGrabber ff = new FFmpegFrameGrabber(video);

        try {
            ff.start();

            String title = ff.getMetadata("title");
            String comment = ff.getMetadata("comment");
            System.out.println("title:" + title);
            System.out.println("comment:" + comment);

            ff.stop();

        } catch (FrameGrabber.Exception e) {
            e.printStackTrace();
        }
    }