1. 程式人生 > >Java獲取視訊的時長——日期時間處理

Java獲取視訊的時長——日期時間處理

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!