jaffree 封装ffmpeg 转换视频格式,获取大小,时间,封面

下载

参考网址

【收藏级教程】FFmpeg音视频处理宝典:从入门到精通的50个实用技巧_ffmpeg教程-CSDN博客

配置环境变量

验证

重启idea开发工具

springboot maven集成

复制代码
<dependency>
   <groupId>com.github.kokorin.jaffree</groupId>
   <artifactId>jaffree</artifactId>
   <version>2023.09.10</version>
</dependency>

视频转换同时获取视频大小和时长

java 复制代码
package org.jeecg.common.util;

import com.github.kokorin.jaffree.StreamType;
import com.github.kokorin.jaffree.ffmpeg.*;


import javax.imageio.ImageIO;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;
import java.util.concurrent.atomic.AtomicLong;

public class JaffreeVideoConverter {
   static final AtomicLong durationMillis = new AtomicLong();
   static final AtomicLong durationMillis2 = new AtomicLong();

    public static void main(String[] args) {
        // 输入和输出文件路径
        Path input = Paths.get("D:/input.wmv");
        Path outputss = Paths.get("D:/231311.mp4");
        // 调用FFmpeg进行视频格式转换
        FFmpegResult result = FFmpeg.atPath()
                .addInput(UrlInput.fromPath(input))
                .addOutput(UrlOutput.toPath(outputss))
                .setProgressListener(new ProgressListener() {
                    @Override
                    public void onProgress(FFmpegProgress progress) {
                        durationMillis.set(progress.getTimeMillis());
                        durationMillis2.set(progress.getSize());
                    }
                })
                .execute();
        System.out.println("Exact duration: " + durationMillis.get() + " milliseconds");//视频时长
        System.out.println("Exact duration22: " + durationMillis2.get() + " milliseconds");//视频大小
        System.out.println("视频格式转换完成!");
    }
}

获取视频封面

java 复制代码
package org.jeecg.common.util;

import com.github.kokorin.jaffree.StreamType;
import com.github.kokorin.jaffree.ffmpeg.*;


import javax.imageio.ImageIO;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;
import java.util.concurrent.atomic.AtomicLong;

public class JaffreeVideoConverter {

    public static void main(String[] args) {
        // 输入和输出文件路径
        Path input = Paths.get("D:/input.wmv");
        Path outputs = Paths.get("D:/");

        FFmpegResult result = FFmpeg.atPath()
                .addInput(UrlInput.fromPath(input))
                .addOutput(
                        FrameOutput
                                .withConsumer(
                                        new FrameConsumer() {
                                            private long num = 1;

                                            @Override
                                            public void consumeStreams(List<Stream> streams) {
                                                // All stream type except video are disabled. just ignore
                                            }

                                            @Override
                                            public void consume(Frame frame) {
                                                // End of Stream
                                                if (frame == null) {
                                                    return;
                                                }

                                                try {
                                                    String filename = "frame_" + num++ + ".png";
                                                    Path output = outputs.resolve(filename);
                                                    ImageIO.write(frame.getImage(), "png", output.toFile());
                                                } catch (Exception e) {
                                                    e.printStackTrace();
                                                }
                                            }
                                        }
                                )
                                  //控制截多少张图,这里我只需要截取一张
                                .setFrameCount(StreamType.VIDEO, 1l)
                                //每十秒截取一张 从0开始
                                .setFrameRate(0.1)
                                .disableStream(StreamType.AUDIO)
                                .disableStream(StreamType.SUBTITLE)
                                .disableStream(StreamType.DATA)
                )
                .execute();
       
    }
}

除此之外,还可以合成视频,截取视频等功能

Jaffree项目地址

https://gitcode.com/gh_mirrors/ja/Jaffree

相关推荐
DogDaoDao17 小时前
HEVC/H.265 码流分析工具 HEVCESBrowser 使用教程
ffmpeg·音视频·h.265·hevc·码流分析工具·elecard·hevcsbrowser
hjjdebug2 天前
ffplay6 播放器关键技术点分析 1/2
c++·ffmpeg·音视频
泰勒朗斯3 天前
ffmpeg 中config 文件一些理解
windows·microsoft·ffmpeg
泰勒朗斯3 天前
ffmpeg下编译tsan
ffmpeg
xmode5 天前
centos7.9安装ffmpeg6.1和NASM、Yasm、x264、x265、fdk-aac、lame、opus解码器
ffmpeg·centos
王江奎5 天前
FFmpeg 升级指北
ffmpeg
雨夜和阳晨5 天前
FFmpeg录制屏幕及声音
ffmpeg
吴声子夜歌5 天前
FFmpeg——基础知识及FFmpeg框架
ffmpeg
aqi006 天前
FFmpeg开发笔记(七十二)Linux给FFmpeg集成MPEG-5视频编解码器EVC
android·ffmpeg·音视频·流媒体
不太会编程的IT男7 天前
在 Jetson Orin 开发套件上使用 Hardware Encoder / Decoder 构建 FFmpeg
ffmpeg·视频编解码·h.264