JAVA通过URL链接获取视频时长转化成时分秒格式(eg: 2:9′20″)(无需下载文件)

JAVA通过URL链接获取视频时长(无需下载文件)

转载的原文章(https://www.cnblogs.com/qq545505061/p/11224871.html)

1、下载,引入jar包

原作者修改后的jar包提取:
链接:https://pan.baidu.com/s/1gqsfl_2Tq2swbMY-mQUQeg
提取码:zpdh

2、工具类

public class VideoUtil {

    /**
     * 根据视频路径获取视频时长
     */
    public static void getVideoTime(String path) throws EncoderException {
        // 编码器
        Encoder encoder = new Encoder();
        File file = new File(path);
        // 多媒体信息
        MultimediaInfo info = encoder.getInfo(file);
        // 时长信息
        double duration = info.getDuration();
        String vidoTime = formatSecond(duration/1000);
        System.out.println("视频时长为:" + vidoTime);
        // 音频信息
        AudioInfo audio = info.getAudio();
        int bitRate = audio.getBitRate();  // 比特率
        int channels = audio.getChannels();  // 声道
        String decoder = audio.getDecoder();  // 解码器
        int sRate = audio.getSamplingRate();  // 采样率
        System.out.println("解码器:" + decoder + ",声道:" + channels + ",比特率:" + bitRate + ",采样率:" + sRate);
        // 视频信息
        VideoInfo video = info.getVideo();
        int bitRate2 = video.getBitRate();
        Float fRate = video.getFrameRate();  // 帧率
        VideoSize videoSize = video.getSize();
        int height = videoSize.getHeight();  // 视频高度
        int width = videoSize.getWidth();  // 视频宽度
        System.out.println("视频帧率:" + fRate + ",比特率:" + bitRate2 + ",视频高度:" + height + ",视频宽度:" + width);
    }
    
    /**
     * 将秒转换成几时几分几秒(eg: 2:9′20″)
     */
   public static String formatSecond(Object second){
        String  time="0秒";
        if(second!=null){
            Double s=(Double) second;
            String format;
            Object[] array;
            Integer hours =(int) (s/(60*60));
            Integer minutes = (int) (s/60-hours*60);
            Integer seconds = (int) (s-minutes*60-hours*60*60);
            if(hours>0){
                format="%1$,d:%2$,d′%3$,d″";
                array=new Object[]{hours,minutes,seconds};
            }else if(minutes>0){
                format="%1$,d′%2$,d″";
                array=new Object[]{minutes,seconds};
            }else{
                format="%1$,d″";
                array=new Object[]{seconds};
            }
            time= String.format(format, array);
        }
        return time;

    }

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值