很多朋友都想知道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(秒)。
以上就是小编今天的分享,希望能够帮到大家。