根据视频URL 通过 Java 统计视频时长

导入 jar 包:可下载: Google 云盘地址

pox.xml

<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>
        <!-- Mac ffmpeg -->
        <dependency>
            <groupId>ws.schild</groupId>
            <artifactId>jave-native-osx64</artifactId>
            <version>2.4.6</version>
        </dependency>

Java 代码:

    public Long timeSum() throws EncoderException {
        // Endpoint以华东1(杭州)为例,其它Region请按实际情况填写。
        String endpoint = "oss-cn-hangzhou.aliyuncs.com";
        // 阿里云账号AccessKey拥有所有API的访问权限,风险很高。强烈建议您创建并使用RAM用户进行API访问或日常运维,请登录RAM控制台创建RAM用户。
        String accessKeyId = "LTAI4GEkaccessKeyId4GEk";
        String accessKeySecret = "FeNwLwaccessKeySecretZQkEt";
        // 填写Bucket名称,例如examplebucket。
        String bucketName = "xihu-new";
        // 创建OSSClient实例。
        OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
//        String[] years = new String[]{"2021", "2022"};
        String[] years = new String[]{"2020", "2021", "2022"};
        String[] days = new String[]{"01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24", "25", "26", "27", "28", "29", "30", "31"};
        String[] months = new String[]{
                "01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11", "12"};

        long totalSeconds = 0;
        long i = 0;

        for (String year : years) {
            for (String month : months) {
                for (String day : days) {
               		keyPrefix = "video/" + year + "/" + month + "/" + day + "/";
                    ObjectListing objectListing = ossClient.listObjects(new ListObjectsRequest(bucketName).withPrefix(keyPrefix));
                    List<OSSObjectSummary> sums = objectListing.getObjectSummaries();
                    for (OSSObjectSummary s : sums) {
                        try {
                            String fileName = s.getKey();
                            // 视频地址 URL
                            String url = "https://xihu-new.oss-cn-hangzhou.aliyuncs.com/" + fileName;
                            File mediaFile = new File(url);
                            FfmpegFileInfo ffmpegFileInfo = new FfmpegFileInfo(mediaFile);
                            MultimediaInfo multimediaInfo = ffmpegFileInfo.getInfo(url);
                            long playTime = multimediaInfo.getDuration();
                            long seconds = playTime / 1000;
                            totalSeconds = seconds + totalSeconds;
                            i++;
                            System.out.println(keyPrefix + "......第 " + i + " 个视频 总视频总时长数:" + totalSeconds + " 秒");
                        } catch (Exception ignored) {
                        }
                    }
                }
            }
        }
        return totalSeconds 
    }
   

运行结果:在这里插入图片描述

参考链接:
https://www.cnblogs.com/yoyoyu/p/14255264.html

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值