java实现用ffmpeg 获取视频时长

1546年前 (2020-07-17)java技术3167

首先需要引入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;
}


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

分享给朋友:

相关文章

每次开机checking file system on d

 解决方案:点击左下角(开始)- (运行)输入(CMD)回车 - 进入CMD界面- 输入(d:)回车 - 输入(chkdsk /f)中间有空格。回车,出现提示按Y键,完成后,重启一次会检...

揭秘!如何用一句话找到你的一万微友

 玩微商一年半的时间了,现在的微商如火如荼,很多刚进入微商行业的伙伴都在烦恼一个问题,那就是粉丝、粉丝,我最近调查了身边30多个微友,有25个竟然都是好友问题,有的竟然给我说主动加了50多个...

org.tigris.subversion.javahl.ClientException:Attempted to lock an already-locked dir

 svn更新或提交时候报错:org.tigris.subversion.javahl.ClientException:Attempted to lock an already-locke...

使用Myeclipse 8.5开发基于JAX-WS的Web service实例

 本文为Web service 开发入门篇,主要介绍在Myeclipse 8.5环境下开发Web service的服务程序和客户端程序的基本流程。 在Weblogic 11g...

freeMarker 截取字符串(操作字符串函数 )

<#if c.proSummary!?length gt 25>  <!-- 如果长度 >25 截取25个字-->    ...

freeMarker Jfinal 获取session里的值

问题:freeMaker session取值的常用格式都试过 session["xxx"],session.xxx 直接xxx 都取不出来?????解决:JFinal与Struts...

发表评论

访客

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