第一步:先引入Maven依赖

<dependencies>
   <!-- jave核心包 -->
    <dependency>
        <groupId>ws.schild</groupId>
        <artifactId>jave-core</artifactId>
        <version>2.4.6</version>
    </dependency>
    <!-- window64位 ffmpeg -->
    <dependency>
        <groupId>ws.schild</groupId>
        <artifactId>jave-native-win64</artifactId>
        <version>2.4.6</version>
    </dependency>
    <!-- linux64位 ffmpeg -->
    <dependency>
        <groupId>ws.schild</groupId>
        <artifactId>jave-native-linux64</artifactId>
        <version>2.4.6</version>
    </dependency>

</dependencies>

第二步:在业务层main.java下新建包ws.schild.jave(目的是和jave的jar包路径一致,这样就可以使用jar包中非public类,方便改造。)

第三步:复制jave中的MultimediaObject类在新建的包中重名名为FfmpegFileInfo,需要修改三个地方

1.注释掉88行变量Macher m;

java 获取视频码率 java获取视频文件时长_ci

2.注释掉126行的String token;

java 获取视频码率 java获取视频文件时长_视频时长_02

 

 3.修改getInfo()方法为

public MultimediaInfo getInfo(String url) throws InputFormatException, EncoderException {
        FFMPEGExecutor ffmpeg = this.locator.createExecutor();
        ffmpeg.addArgument("-i");
        ffmpeg.addArgument(url);
        try {
            ffmpeg.execute();
        } catch (IOException var8) {
            throw new EncoderException(var8);
        }

        MultimediaInfo var3;
        try {
            RBufferedReader reader = new RBufferedReader(new InputStreamReader(ffmpeg.getErrorStream()));
            var3 = this.parseMultimediaInfo(this.inputFile, reader);
        } finally {
            ffmpeg.destroy();
        }
        return var3;

}

第四步:使用

/**
     * 获取URL地址获取视频文件长度
     *
     * @param filePath 文件路径
     * @return 视频长度(单位:秒)
     */
    private long getURLVideoSecond(String filePath) {
        File file = new File(filePath);
        try {
            FfmpegFileInfo ffmpegFileInfo = new FfmpegFileInfo(file);
            ws.schild.jave.MultimediaInfo info = ffmpegFileInfo.getInfo(filePath);
            return info.getDuration() / 1000;
        } catch (Exception e) {
            log.error("获取URL视频文件时长失败", e);
            return 0;
        }
    }