博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Java通过调用FFMPEG获取视频时长(已测试)
阅读量:4041 次
发布时间:2019-05-24

本文共 3395 字,大约阅读时间需要 11 分钟。

转载地址:

FFmpeg是一套可以用来记录、转换数字音频、视频,并能将其转化为流的开源计算机程序。采用LGPL或GPL许可证。它提供了录制、转换以及流化音视频的完整解决方案。它包含了非常先进的音频/视频编解码库libavcodec,为了保证高可移植性和编解码质量,libavcodec里很多codec都是从头开发的。

      主要利用的/usr/bin/ffmpeg   -i    698.mp4  这个命令  

       由此看来FFmpeg很强大,很多主流的音频、视频处理软件都使用了FFmpeg。

       FFmpeg下载下来解压,cmd进入到FFmpeg.exe目录中,即可在命令行下进行各种操作,查看视频信息命令:ffmpeg 视频 -i,如下图:

       D:\ffmpeg\Libs>ffmpeg -i D:\MonitorRecord\monitor_20091222_050948_1.avi

[sql]   
 
  1. FFmpeg version SVN-r10087, Copyright (c) 2000-2007 Fabrice Bellard, et al.  
  2.   configuration: --prefix=f:/svn_build_bins --enable-memalign-hack --enable-shared --disable-static --enable-w32threads --enable-liba52 --enable-avisynth --enable-libamr-nb --enable-libamr-wb --enable-libfaac --enable-libfaad --enable-libgsm --enable-libmp3lame --enable-libogg --enable-libtheora --enable-libvorbis --enable-libx264 --enable-gpl --extra-cflags=-I/usr/local/include --extra-ldflags=-L/usr/local/lib  
  3.   libavutil version: 49.5.0  
  4.   libavcodec version: 51.40.4  
  5.   libavformat version: 51.12.2  
  6.   built on Aug 12 2007 11:38:35, gcc: 4.2.1  
  7.   
  8.   Compiled by msn: dev # fastreaming.com, 2007/08/12  
  9.   Enjoy it  
  10.   
  11. Input #0, avi, from 'D:\MonitorRecord\monitor_20091222_050948_1.avi':  
  12.   Duration: 00:00:25.0, start: 0.000000, bitrate: 619 kb/s  
  13.   Stream #0.0: Video: mpeg4, yuv420p, 1620x1100,  1.14 fps(r)  
  14. Must supply at least one output file  

 

    在中执行此操作,解析返回结果,可以得到视频时长等信息。

[java]   
 
  1. /** 
  2.      * 获取视频总时间 
  3.      * @param viedo_path    视频路径 
  4.      * @param ffmpeg_path   ffmpeg路径 
  5.      * @return 
  6.      */  
  7.     public static int getVideoTime(String video_path, String ffmpeg_path) {  
  8.         List<String> commands = new java.util.ArrayList<String>();  
  9.         commands.add(ffmpeg_path);  
  10.         commands.add("-i");  
  11.         commands.add(video_path);  
  12.         try {  
  13.             ProcessBuilder builder = new ProcessBuilder();  
  14.             builder.command(commands);  
  15.             final Process p = builder.start();  
  16.               
  17.             //从输入流中读取视频信息  
  18.             BufferedReader br = new BufferedReader(new InputStreamReader(p.getErrorStream()));  
  19.             StringBuffer sb = new StringBuffer();  
  20.             String line = "";  
  21.             while ((line = br.readLine()) != null) {  
  22.                 sb.append(line);  
  23.             }  
  24.             br.close();  
  25.               
  26.             //从视频信息中解析时长  
  27.             String regexDuration = "Duration: (.*?), start: (.*?), bitrate: (\\d*) kb\\/s";  
  28.             Pattern pattern = Pattern.compile(regexDuration);  
  29.             Matcher m = pattern.matcher(sb.toString());  
  30.             if (m.find()) {  
  31.                 int time = getTimelen(m.group(1));  
  32.                 log.info(video_path+",视频时长:"+time+", 开始时间:"+m.group(2)+",比特率:"+m.group(3)+"kb/s");  
  33.                 return time;  
  34.             }  
  35.         } catch (Exception e) {  
  36.             e.printStackTrace();  
  37.         }  
  38.           
  39.         return 0;  
  40.     }  
  41.       
  42.     //格式:"00:00:10.68"  
  43.     private static int getTimelen(String timelen){  
  44.         int min=0;  
  45.         String strs[] = timelen.split(":");  
  46.         if (strs[0].compareTo("0") > 0) {  
  47.             min+=Integer.valueOf(strs[0])*60*60;//秒  
  48.         }  
  49.         if(strs[1].compareTo("0")>0){  
  50.             min+=Integer.valueOf(strs[1])*60;  
  51.         }  
  52.         if(strs[2].compareTo("0")>0){  
  53.             min+=Math.round(Float.valueOf(strs[2]));  
  54.         }  
  55.         return min;  
  56.     }  
以下是调用方法,经测试上述请求可以获得正确时长
public void updateVideoDuration(String liveId,int videoId){		OSSConfig ossConfig = new OSSConfig();		String videolocalPath = config.getConfigValue("videoLocalPath")+liveId+ossConfig.getSuffix();		LOGGER.info("updateVideoDuration ffmpegpath:" + videolocalPath+"   ,liveId:"+liveId);		try{			int duration = FfmpegUtil.getVideoTime(videolocalPath, "/user/bin/ffmpeg");			Video video = new Video();			video.setId(videoId);			video.setDuration(duration);			videoService.updateVideoDuration(video);			LOGGER.info("updateVideoDuration  success,duration:"+duration);  		}catch(Exception e){			e.printStackTrace();			LOGGER.error("updateVideoDuration error:"+e);		}			}
    在
中执行此操作,解析返回结果,可以得到视频时长等信息。
你可能感兴趣的文章
Android中电池信息(Battery information)的取得
查看>>
SVN客户端命令详解
查看>>
Android/Linux 内存监视
查看>>
Linux系统信息查看
查看>>
用find命令查找最近修改过的文件
查看>>
Android2.1消息应用(Messaging)源码学习笔记
查看>>
Android之TelephonyManager类的方法详解
查看>>
android raw读取超过1M文件的方法
查看>>
ubuntu下SVN服务器安装配置
查看>>
MPMoviePlayerViewController和MPMoviePlayerController的使用
查看>>
CocoaPods实践之制作篇
查看>>
[Mac]Mac 操作系统 常见技巧
查看>>
苹果Swift编程语言入门教程【中文版】
查看>>
捕鱼忍者(ninja fishing)之游戏指南+游戏攻略+游戏体验
查看>>
iphone开发基础之objective-c学习
查看>>
iphone开发之SDK研究(待续)
查看>>
计算机网络复习要点
查看>>
Variable property attributes or Modifiers in iOS
查看>>
NSNotificationCenter 用法总结
查看>>
C primer plus 基础总结(一)
查看>>