视频截屏(视频转图片)

复制代码
public class VideoToPicture {
    public static void main(String[] args) throws IOException {
        String filename = "D://tmp//4fbe1db0bd9e4a59ab380f262ea305c8.mp4";
        String id = UUID.randomUUID().toString().replace("-", "");
        String filename2 = "D://tmp//" + id + ".mp4";
        String outputFilename = "D://tmp//" + id + ".png";
        execute("视频地址", outputFilename, "mp4");
    }

    public static void base64ToVideo(String base64String, String outputFilename) {
        String prefix = "data:video/mp4;base64,";
        if (base64String.startsWith(prefix)) {
            base64String = base64String.replace(prefix, "");
        }
        byte[] decodedData = Base64.getDecoder().decode(base64String);
        FileOutputStream fos = null;
        try {
            fos = new FileOutputStream(outputFilename);
            fos.write(decodedData);
            fos.flush();
            fos.close();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (fos != null) {
                try {
                    fos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    /**
     * 视频文件截图转图片存储
     *
     * @param inputFilename  视频文件
     * @param outputFilename 图片输出地址
     * @param format         音频格式
     */
    public static void execute(String inputFilename, String outputFilename, String format) {
        Assert.hasText(inputFilename, "音频文件不能为空");
        Assert.hasText(outputFilename, "图片地址不能为空");
        Assert.hasText(format, "音频格式不能为空");
        IContainer container = IContainer.make();
        IContainerFormat containerFormat = IContainerFormat.make();
        containerFormat.setInputFormat(format);
        boolean notOpen = false;
        try {
            notOpen = container.open(inputFilename, IContainer.Type.READ, containerFormat) < 0;
        } catch (Exception e) {
            e.printStackTrace();
            throw new IllegalArgumentException("Could not open file: " + inputFilename);
        }
        if (notOpen) {
            throw new IllegalArgumentException("Could not open file: " + inputFilename);
        }
        int numStreams = container.getNumStreams();
        int videoStreamId = -1;
        IStreamCoder videoCoder = null;
        for (int i = 0; i < numStreams; i++) {
            IStream stream = container.getStream(i);
            IStreamCoder coder = stream.getStreamCoder();
            if (coder.getCodecType() == ICodec.Type.CODEC_TYPE_VIDEO) {
                videoStreamId = i;
                videoCoder = coder;
                break;
            }
        }
        if (videoStreamId == -1) {
            throw new RuntimeException("No video stream in container: " + inputFilename);
        }
        if (videoCoder.open() < 0) {
            throw new RuntimeException("Could not open video coder for container: " + inputFilename);
        }
        IVideoPicture picture = IVideoPicture.make(videoCoder.getPixelType(), videoCoder.getWidth(), videoCoder.getHeight());
        IPacket packet = IPacket.make();
        boolean isStop = false;
        while (!isStop && container.readNextPacket(packet) >= 0) {
            if (packet.getStreamIndex() == videoStreamId) {
                int offset = 0;
                while (offset < packet.getSize()) {
                    int bytesDecoded = videoCoder.decodeVideo(picture, packet, offset);
                    if (bytesDecoded < 0) {
                        throw new RuntimeException("Error decoding video");
                    }
                    offset += bytesDecoded;
                    if (picture.isComplete()) {
                        if (picture.getTimeStamp() > 1000) {
                            IConverter converter = ConverterFactory.createConverter("XUGGLER-BGR-24", picture);
                            BufferedImage javaImage = converter.toImage(picture);
                            try {
                                ImageIO.write(javaImage, "PNG", new File(outputFilename));
                            } catch (IOException e) {
                                e.printStackTrace();
                            }
                            isStop = true;
                            break;
                        }
                    }
                }
            }
        }
        videoCoder.close();
        container.close();
    }
}
相关推荐
天天扭码17 分钟前
五天SpringCloud计划——DAY2之单体架构和微服务架构的选择和转换原则
java·spring cloud·微服务·架构
程序猿进阶18 分钟前
堆外内存泄露排查经历
java·jvm·后端·面试·性能优化·oom·内存泄露
FIN技术铺22 分钟前
Spring Boot框架Starter组件整理
java·spring boot·后端
小曲程序30 分钟前
vue3 封装request请求
java·前端·typescript·vue
陈王卜1 小时前
django+boostrap实现发布博客权限控制
java·前端·django
小码的头发丝、1 小时前
Spring Boot 注解
java·spring boot
java亮小白19971 小时前
Spring循环依赖如何解决的?
java·后端·spring
飞滕人生TYF1 小时前
java Queue 详解
java·队列
武子康1 小时前
大数据-230 离线数仓 - ODS层的构建 Hive处理 UDF 与 SerDe 处理 与 当前总结
java·大数据·数据仓库·hive·hadoop·sql·hdfs
武子康1 小时前
大数据-231 离线数仓 - DWS 层、ADS 层的创建 Hive 执行脚本
java·大数据·数据仓库·hive·hadoop·mysql