Java获取视频的时长——日期时间处理
来源:互联网 发布:外置式网络打印服务器 编辑:程序博客网 时间:2024/06/07 01:14
Problem:
如何通过Java获取视频的时长?
想通过Java来获取某文件夹(或多个文件夹)下的视频的总时长
经过查询资料,发现可以通过JAVE实现:The JAVE (Java Audio Video Encoder) library is Java wrapper on the ffmpeg project。
JAVE下载地址
Solution:
以单个目录下的视频为例,通过JAVE实现,并采用不同的日期时间处理方式转换为时-分-秒格式,顺便熟悉了一下Java 中的日期处理。
- Note:当然,需要导入下载的JAVE的jar文件。。
eg:
import java.io.File; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; import java.util.TimeZone; import it.sauronsoftware.jave.Encoder; import it.sauronsoftware.jave.EncoderException; import it.sauronsoftware.jave.MultimediaInfo; public class VideoDuration { public void getVideoDuration(String path) { // get all files in specified "path" File[] files = new File(path).listFiles(); Encoder encoder = new Encoder(); MultimediaInfo multimediaInfo; long totalTime = 0L; long duration = 0L; for (int i = 0; i < files.length; i++) { // here, the format of video can be changed, JAVE upports dozens of formats if (!files[i].isDirectory() && files[i].toString().endsWith(".avi")) { try { multimediaInfo = encoder.getInfo(files[i]); duration = multimediaInfo.getDuration(); totalTime += duration; } catch (EncoderException e) { e.printStackTrace(); } } } // long --> hh:mm: calculate the time manually System.out.print(totalTime/(3600*1000) + ":" + totalTime%(3600*1000)/(60*1000) + ":" + totalTime%(3600*1000)%(60*1000)/1000); System.out.println("==>Manually"); // set a default TimeZone before using Date, Calendar and SimpleDateFormat TimeZone.setDefault(TimeZone.getTimeZone("GMT+00:00")); // January 1, 1970, 00:00:00 GMT(can be found in Date.class) // long --> hh:mm:ss by means of java.util.Date Date date = new Date(totalTime); System.out.print(date.getHours() + ":" + date.getMinutes() + ":" + date.getSeconds()); System.out.println("==>By Date"); // long --> hh:mm:ss by means of java.util.Calendar, Date Calendar calendar = Calendar.getInstance(); calendar.setTime(date); System.out.print(calendar.get(Calendar.HOUR_OF_DAY) + ":" + calendar.get(Calendar.MINUTE) + ":" + calendar.get(Calendar.SECOND)); System.out.println("==>By Calendar"); // long --> hh:mm:ss by means of java.text.SimpleDateFormat, java.util.Date SimpleDateFormat simpleDateFormat = new SimpleDateFormat("HH:mm:ss"); System.out.print(simpleDateFormat.format(date)); System.out.println("==>By SimpleDateFormat"); } public static void main(String[] args) { String filePath = "E:\\BaiduYunDownload\\MySQL"; VideoDuration videoDuration = new VideoDuration(); videoDuration.getVideoDuration(filePath); } }
If you have any questions or any bugs are found, please feel free to contact me.
Your comments and suggestions are welcome!
0 0
- Java获取视频的时长——日期时间处理
- java处理视频文件,读取视频的时长
- java调用ffmpeg获取视频时长
- Java获取3gp视频时长
- java 获取音频,视频时长
- java获取视频播放时长
- java调用ffmpeg获取视频时长
- java、js 获取视频时长
- Java——时间和日期处理
- 时间操作(Java版)—获取距离系统时间N天后的日期时间信息
- 获取视频时长
- 获取视频时长
- Java日期处理——字符串转换为日期时间
- 获取HTML5视频时长的方法
- Java 处理视频 、音频文件(读取视频时长等) - JAVE
- 时间操作(Java版)—获取给定日期N天后的日期
- Java处理日期,时间,以及日期时间的类
- java获取时间的处理
- 面试题40:数组中的逆序对
- 计算机网络学习(四)——链路层
- Android开发总结笔记 自定义View的编写 3-8
- eclipse中选中字段,其他相同字段被覆盖的颜色修改
- Android:操作联系人
- Java获取视频的时长——日期时间处理
- iconfont.cn中字体图标的问题
- Android应用开发SharedPreferences存储数据的使用方法
- require.js的用法-阮一峰
- 在服务器上排除问题的头五分钟
- hdoj Box of Bricks 1326 (转换)水
- 你的2016想怎么过?看看成功人士们的年度计划(
- 【Java】Google Java Style 中文版
- 虚拟化技术