本文共 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
- FFmpeg version SVN-r10087, Copyright (c) 2000-2007 Fabrice Bellard, et al.
- configuration:
- libavutil version: 49.5.0
- libavcodec version: 51.40.4
- libavformat version: 51.12.2
- built on Aug 12 2007 11:38:35, gcc: 4.2.1
-
- Compiled by msn: dev # fastreaming.com, 2007/08/12
- Enjoy it
-
- Input #0, avi, from 'D:\MonitorRecord\monitor_20091222_050948_1.avi':
- Duration: 00:00:25.0, start: 0.000000, bitrate: 619 kb/s
- Stream #0.0: Video: mpeg4, yuv420p, 1620x1100, 1.14 fps(r)
- Must supply at least one output file
在中执行此操作,解析返回结果,可以得到视频时长等信息。
-
-
-
-
-
-
- public static int getVideoTime(String video_path, String ffmpeg_path) {
- List<String> commands = new java.util.ArrayList<String>();
- commands.add(ffmpeg_path);
- 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));
- log.info(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;
- }
以下是调用方法,经测试上述请求可以获得正确时长 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); } }
在 中执行此操作,解析返回结果,可以得到视频时长等信息。