當前位置:生活全書館 >

IT科技

> java獲取視訊時長

java獲取視訊時長

<link rel="stylesheet" href="https://js.how234.com/third-party/SyntaxHighlighter/shCoreDefault.css" type="text/css" /><script type="text/javascript" src="https://js.how234.com/third-party/SyntaxHighlighter/shCore.js"></script><script type="text/javascript"> SyntaxHighlighter.all(); </script>

很多朋友都想知道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;    }

java獲取視訊時長

通過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.javapublic 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
  • 文章版權屬於文章作者所有,轉載請註明 https://shqsg.com/dianzi/qyr0ng.html