java实现用ffmpeg 获取视频时长

1545年前 (2020-07-17)java技术2986

首先需要引入ffmpeg.exe

调用

 int time = ConvertM3U8.getVideoTime(downloadPath);

方法:

 static String ffmpegpath="D:\\Program Files\\ffmpeg-20200628-4cfcfb3-win64-static\\bin\\ffmpeg.exe"; // ffmpeg.exe的目录
   static String ffmpegpath= Global.getProfile()+"\\ffmpeg.exe";
   
public static int getVideoTime(String video_path) {
    List<String> commands = new java.util.ArrayList<String>();
    commands.add(ffmpegpath);
    commands.add("-i");
    commands.add(video_path);
    try {
        ProcessBuilder builder = new ProcessBuilder();
        builder.command(commands);
        final Process p = builder.start();

        //从输入流中读取视频信息
        BufferedReader br = new BufferedReader(new InputStreamReader(p.getErrorStream()));
        StringBuffer sb = new StringBuffer();
        String line = "";
        while ((line = br.readLine()) != null) {
            sb.append(line);
        }
        br.close();

        //从视频信息中解析时长
        String regexDuration = "Duration: (.*?), start: (.*?), bitrate: (\\d*) kb\\/s";
        Pattern pattern = Pattern.compile(regexDuration);
        Matcher m = pattern.matcher(sb.toString());
        if (m.find()) {
            int time = getTimelen(m.group(1));
            System.out.println(video_path+",视频时长:"+time+", 开始时间:"+m.group(2)+",比特率:"+m.group(3)+"kb/s");
            return time;
        }
    } catch (Exception e) {
        e.printStackTrace();
    }

    return 0;
}
private static int getTimelen(String timelen){
    int min=0;
    String strs[] = timelen.split(":");
    if (strs[0].compareTo("0") > 0) {
        min+=Integer.valueOf(strs[0])*60*60;//秒
    }
    if(strs[1].compareTo("0")>0){
        min+=Integer.valueOf(strs[1])*60;
    }
    if(strs[2].compareTo("0")>0){
        min+=Math.round(Float.valueOf(strs[2]));
    }
    return min;
}


本文原创,转载必追究版权。

分享给朋友:

相关文章

多说评论框怎么用更好

 1.隐藏屏蔽掉多说评论框的版权链接代码?简单css实现:多说隐藏版权链接,在后台自定义css添加:#ds-thread #ds-reset .ds-powered-by { display...

get/post方式调用http接口

get/post方式调用http接口

 1. 项目环境如下:myeclipse8.5 、tomcat5.0/weblogic、xp、JDK:开发1.5,编译1.4为了方便,在原来的web项目UpDown中新建了一个httpcal...

坑爹的ShowModalDialog 后台传值解决方案

 今天遇到需要ShowModalDialog打开页面,通过acceptanceIds 参数值后台过滤出相应结果前台  var url = "loadAccept...

js/jquery 实现点击图片更换头像(图片)实例

    总之一句话, 可以先将 file类型 的input 隐藏 起来,通过 图片(头像)的onclick事件 来触发 file 的onclick事件。1.引入外部js:...

是谁动了我的座位

是谁动了我的座位

女孩一上火车,见自己的座位上坐着一男士。她核对自己的票,客气地说:“先生,您坐错位置了吧?” 男士拿出票嚷嚷着:“看清楚点,这是我的座,你瞎了?” 女孩仔细看了他的票,不再做声,默...

目标管理法——目标分解法

让自己的人生更幸福更有意义关键是:要将梦想转化为具体的目标,然后合理的分解,达到量化,指标化!现将学习到的两种非常有效的目标分解法分享给所有梦想、有激情的朋友:祝愿大家都能梦想成真! 一、俄...

发表评论

访客

◎欢迎参与讨论,请在这里发表您的看法和观点。