欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

java获取视频第一帧

程序员文章站 2022-07-02 09:24:21
...

web展示视频,如果通过直接展示,视频是黑色照片显示,此方法可以通过后台截取获取到的视频第一帧,获取到视频的照片,代码如下,写的是main方法,方便大家进行测试与修改。如果对大家有用,请留言支持!

pom文件

<!-- javacv start -->
<dependency>
    <groupId>org.bytedeco</groupId>
    <artifactId>javacpp</artifactId>
    <version>1.4.1</version>
</dependency>
<dependency>
    <groupId>org.bytedeco</groupId>
    <artifactId>javacv</artifactId>
    <version>1.4.1</version>
</dependency>
<dependency>
    <groupId>org.bytedeco.javacpp-presets</groupId>
    <artifactId>opencv-platform</artifactId>
    <version>3.4.1-1.4.1</version>
</dependency>
<dependency>
    <groupId>org.bytedeco.javacpp-presets</groupId>
    <artifactId>ffmpeg-platform</artifactId>
    <version>3.4.2-1.4.1</version>
</dependency>
<!-- javacv end -->

<dependency>
    <groupId>org.bytedeco.javacpp-presets</groupId>
    <artifactId>opencv-platform</artifactId>
    <version>3.4.1-1.4.1</version>
</dependency>

java代码

package com.zifeng.check.utils;


import org.bytedeco.javacv.FFmpegFrameGrabber;
import org.bytedeco.javacv.Frame;
import org.bytedeco.javacv.Java2DFrameConverter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

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

public class videoText {

    public static final Logger log = LoggerFactory.getLogger(videoText.class);

    public static String getTempPath(String tempPath, String filePath) throws Exception {
//        String tempPath="    ";//保存的目标路径
        File targetFile = new File(tempPath);
        if (!targetFile.getParentFile().exists()) {
            targetFile.getParentFile().mkdirs();
        }
            File file2 = new File(filePath);
            if (file2.exists()) {
                log.info("文件存在,路径正确!");
                FFmpegFrameGrabber ff = new FFmpegFrameGrabber(file2);
                ff.start();
                int ftp = ff.getLengthInFrames();
                int flag=0;
                Frame frame = null;
                while (flag <= ftp) {
                    //获取帧
                    frame = ff.grabImage();
                    //过滤前3帧,避免出现全黑图片
                    if ((flag>3)&&(frame != null)) {
                        break;
                    }
                    flag++;
                }
                ImageIO.write(FrameToBufferedImage(frame), "jpg", targetFile);
                ff.close();
                ff.stop();
            }
        return null;
    }
    private static RenderedImage FrameToBufferedImage(Frame frame) {
        //创建BufferedImage对象
        Java2DFrameConverter converter = new Java2DFrameConverter();
        BufferedImage bufferedImage = converter.getBufferedImage(frame);
        return bufferedImage;
    }

    public static void main(String[] args) {
        try {
//            getTempPath("http://jyt.fty.pw:8080/videos/guoyifang/a2966518-03dd-4a31-b05b-5c52db6baf73.mp4", "D:\\test.jpg");
            getTempPath("D:\\test.jpg", "D:/videoText/1.mp4");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}