<address id="zhpbl"></address>
<noframes id="zhpbl">
<address id="zhpbl"><form id="zhpbl"><th id="zhpbl"></th></form></address>

    <em id="zhpbl"></em>

      <address id="zhpbl"><th id="zhpbl"><progress id="zhpbl"></progress></th></address>
      更多精彩內容,歡迎關注:

      視頻號
      視頻號

      抖音
      抖音

      快手
      快手

      微博
      微博

      java獲取視頻時長

      文檔

      java獲取視頻時長

      java可以通過視頻的url地址或者是上傳視頻文件有類型為(MultipartFile)的來獲取視頻的時長,兩個方法最后讀取的數據單位都是s(秒)。
      推薦度:
      導讀java可以通過視頻的url地址或者是上傳視頻文件有類型為(MultipartFile)的來獲取視頻的時長,兩個方法最后讀取的數據單位都是s(秒)。

      很多朋友都想知道java怎么獲取視頻時長?下面就一起來了解一下吧~

      可以通過視頻的url地址或者是上傳視頻 文件有類型為(MultipartFile)的來獲取視頻的時長。

      通過MultipartFile?的文件類型獲取視頻時長:

      這個方式的獲取比較好辦;直接上碼:類名=》VideoUtil.java

      /**
      ?????*?通過MultipartFile?的文件類型獲取視頻時長
      ?????*?@param?file
      ?????*?@return
      ?????*/
      ????public?Integer?ReadVideoTimeMs(MultipartFile?file)?{
      ????????Encoder?encoder?=?new?Encoder();
      ????????long?ms?=?0;
      ????????try?{
      ????????????//?獲取文件類型
      ????????????String?fileName?=?file.getContentType();
      ????????????//?獲取文件后綴
      ????????????String?pref?=?fileName.indexOf("/")?!=?-1???fileName.substring(fileName.lastIndexOf("/")?+?1,?fileName.length())?:?null;
      ????????????String?prefix?=?"."?+?pref;
      ????????????//?用uuid作為文件名,防止生成的臨時文件重復
      ????????????final?File?excelFile?=?File.createTempFile(UUID.randomUUID().toString().replace("-",?""),?prefix);
      ????????????//?MultipartFile?to?File
      ????????????file.transferTo(excelFile);
      ????????????MultimediaInfo?m?=?encoder.getInfo(excelFile);
      ????????????ms?=?m.getDuration();
      ????????????//程序結束時,刪除臨時文件
      ????????????VideoUtil.deleteFile(excelFile);
      ????????}?catch?(Exception?e)?{
      ????????????e.printStackTrace();
      ????????}
      ????????int?ss?=?1000;
      ????????int?mi?=?ss?*?60;
      ????????int?hh?=?mi?*?60;
      ????????int?dd?=?hh?*?24;
      ????????long?day?=?ms?/?dd;
      ????????long?hour?=?(ms?-?day?*?dd)?/?hh;
      ????????long?minute?=?(ms?-?day?*?dd?-?hour?*?hh)?/?mi;
      ????????long?second?=?(ms?-?day?*?dd?-?hour?*?hh?-?minute?*?mi)?/?ss;
      ????????Integer?timeMS?=?Math.toIntExact(hour?*?3600?+?minute?*?60?+?second);
      ????????return?timeMS;
      ????}
      ????/**
      ?????*?通過url地址獲取視頻時長
      ?????*?@param?fileName
      ?????*?@return
      ?????*/
      ????public?Integer?FileVideoTimeMs(String?fileName){
      ????????Encoder?encoder?=?new?Encoder();
      ????????long?ms?=?0;
      ????????try?{
      ????????????File?file?=?getFileByUrl(fileName);
      ????????????MultimediaInfo?m?=?encoder.getInfo(file);
      ????????????ms?=?m.getDuration();
      ????????????VideoUtil.deleteFile(file);
      ????????}catch?(Exception?e){
      ????????????e.printStackTrace();
      ????????}
      ????????int?ss?=?1000;
      ????????int?mi?=?ss?*?60;
      ????????int?hh?=?mi?*?60;
      ????????int?dd?=?hh?*?24;
      ????????long?day?=?ms?/?dd;
      ????????long?hour?=?(ms?-?day?*?dd)?/?hh;
      ????????long?minute?=?(ms?-?day?*?dd?-?hour?*?hh)?/?mi;
      ????????long?second?=?(ms?-?day?*?dd?-?hour?*?hh?-?minute?*?mi)?/?ss;
      ????????Integer?timeMS?=?Math.toIntExact(hour?*?3600?+?minute?*?60?+?second);
      ????????return?timeMS;
      ????}

      通過url地址獲取視頻時長

      url地址來獲取的時長需要做一定的轉換,要先獲取文件

      主要的方法代碼:類名 =》VideoUtil.java

      /**
      ?????*?通過url地址獲取視頻時長
      ?????*?@param?fileName
      ?????*?@return
      ?????*/
      ????public?Integer?FileVideoTimeMs(String?fileName){
      ????????Encoder?encoder?=?new?Encoder();
      ????????long?ms?=?0;
      ????????try?{
      ????????????File?file?=?getFileByUrl(fileName);
      ????????????MultimediaInfo?m?=?encoder.getInfo(file);
      ????????????ms?=?m.getDuration();
      ????????????VideoUtil.deleteFile(file);
      ????????}catch?(Exception?e){
      ????????????e.printStackTrace();
      ????????}
      ????????int?ss?=?1000;
      ????????int?mi?=?ss?*?60;
      ????????int?hh?=?mi?*?60;
      ????????int?dd?=?hh?*?24;
      ????????long?day?=?ms?/?dd;
      ????????long?hour?=?(ms?-?day?*?dd)?/?hh;
      ????????long?minute?=?(ms?-?day?*?dd?-?hour?*?hh)?/?mi;
      ????????long?second?=?(ms?-?day?*?dd?-?hour?*?hh?-?minute?*?mi)?/?ss;
      ????????Integer?timeMS?=?Math.toIntExact(hour?*?3600?+?minute?*?60?+?second);
      ????????return?timeMS;
      ????}

      getFileByUrl()方法 :類名 =》VideoUtil.java

      public?static?File?getFileByUrl(String?url)?throws??IOException?{
      ????????File?tmpFile?=?File.createTempFile("temp",?".tmp");//創建臨時文件
      ????????VideoUrl.toBDFile(url,?tmpFile.getCanonicalPath());
      ????????return?tmpFile;
      ????}

      toBDFile() 方法:類名 =》VideoUrl.java

      public?static?void?toBDFile(String?urlStr,?String?bdUrl)?throws?IOException,?UnknownHostException?{
      ????????URL?url?=?new?URL(urlStr);
      ????????HttpURLConnection?conn?=?(HttpURLConnection)?url.openConnection();
      ????????DataInputStream?in?=?new?DataInputStream(conn.getInputStream());
      ????????byte[]?data?=?toByteArray(in);
      ????????in.close();
      ????????FileOutputStream?out?=?new?FileOutputStream(bdUrl);
      ????????out.write(data);
      ????????out.close();
      ????}
      toByteArray()方法:類名?=》VideoUrl.java
      public?static?byte[]?toByteArray(InputStream?in)?throws?IOException?{
      ????????ByteArrayOutputStream?out?=?new?ByteArrayOutputStream();
      ????????byte[]?buffer?=?new?byte[1024?*?4];
      ????????int?n?=?0;
      ????????while?((n?=?in.read(buffer))?!=?-1)?{
      ????????????out.write(buffer,?0,?n);
      ????????}
      ????????return?out.toByteArray();
      ????}

      不要忘記刪除自己生成的臨時文件:類名 =》VideoUtil.java

      /**
      ?????*?刪除臨時文件
      ?????*?@param?files
      ?????*/
      ????private?static?void?deleteFile(File...?files)?{
      ????????for?(File?file?:?files)?{
      ????????????if?(file.exists())?{
      ????????????????file.delete();
      ????????????}
      ????????}
      ????}

      總結

      兩個方法的獲取視頻的時長,最后讀取的數據單位都是s(秒)。

      以上就是小編今天的分享,希望能夠幫到大家。

      文檔

      java獲取視頻時長

      java可以通過視頻的url地址或者是上傳視頻文件有類型為(MultipartFile)的來獲取視頻的時長,兩個方法最后讀取的數據單位都是s(秒)。
      推薦度:
      為你推薦
      資訊專欄
      熱門視頻
      相關推薦
      java csrf java獲取請求域名 java獲取路徑 java cssbox java獲取運行時間 java ctp java獲取隨機字符串 java daemon java獲取隨機數 java dashboard java獲得當前路徑 java表單校驗 java dataframe java規則引擎 java dataset java讀取json java讀取本地excel java decaf java調python java decode java crontab java獲取網絡時間 java crash java獲取秒級時間戳 java core java獲取泛型class java corba java cookie java獲取本地ip java controller java獲取本周日期 java context japanese java 微信怎么群發消息 java獲取時間差 java獲取文件名后綴 微博賬號怎么注銷 excel如何換行 java獲取當月最后一天日期 excel去重
      Top 少妇高潮太爽了在线视频