Java獲取視頻時(shí)長(zhǎng)、大小的示例
項(xiàng)目中有這樣一個(gè)需求,網(wǎng)頁(yè)上上傳了一個(gè)視頻,需要獲取此視頻的時(shí)長(zhǎng)、大小,把這兩個(gè)數(shù)據(jù)返回給前臺(tái)在頁(yè)面顯示。后臺(tái)使用的是springboot框架,項(xiàng)目部署在linux上面。下面是核心代碼:
1、pom文件中需要導(dǎo)入的jar包依賴(lài)
(分為兩部分:核心包、ffmpeg包兩部分,ffmpeg包又分為Windows環(huán)境以及Linux環(huán)境,同時(shí)又區(qū)分32位系統(tǒng)以及64位系統(tǒng)。針對(duì)于不同的運(yùn)行環(huán)境要導(dǎo)入不同的包,這一點(diǎn)對(duì)于開(kāi)發(fā)、測(cè)試環(huán)境為Window而生產(chǎn)環(huán)境為L(zhǎng)inux的情況,尤其要注意需要導(dǎo)入所有的包,使其在兩種環(huán)境下都能夠使用)
<dependency> <groupId>ws.schild</groupId> <artifactId>jave-all-deps</artifactId> <version>2.6.0</version> </dependency> <dependency> <groupId>ws.schild</groupId> <artifactId>jave-core</artifactId> <version>2.4.5</version> </dependency> <!-- window32位 ffmpeg --> <dependency> <groupId>ws.schild</groupId> <artifactId>jave-native-win32</artifactId> <version>2.4.5</version> </dependency> <!-- window64位 ffmpeg --> <dependency> <groupId>ws.schild</groupId> <artifactId>jave-native-win64</artifactId> <version>2.4.5</version> </dependency> <!-- linux32位 ffmpeg --> <dependency> <groupId>ws.schild</groupId> <artifactId>jave-native-linux32</artifactId> <version>2.4.6</version> </dependency> <!-- linux64位 ffmpeg --> <dependency> <groupId>ws.schild</groupId> <artifactId>jave-native-linux64</artifactId> <version>2.4.6</version> </dependency>
2、controller層代碼
package com.aaa.bbb.controller; import io.swagger.annotations.Api; import lombok.extern.slf4j.Slf4j; import org.apache.commons.io.FileUtils; import org.springframework.util.ResourceUtils; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; import ws.schild.jave.MultimediaInfo; import ws.schild.jave.MultimediaObject; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.math.BigDecimal; import java.math.RoundingMode; import java.nio.channels.FileChannel; @Slf4j @RestController @RequestMapping("/readVideo") @Api(tags = "獲取視頻時(shí)長(zhǎng)、大小相關(guān)信息的接口") public class ReadVideoController { /** * 獲取視頻時(shí)長(zhǎng) * * @param fileUrl * @return */ @PostMapping("/videoLengthAndSize") public static String getLengthAndSize(@RequestParam String fileUrl) throws FileNotFoundException { ReadVideoController r = new ReadVideoController(); String path = ResourceUtils.getURL("classpath:").getPath() + "static"; System.out.println("666666666666666666666666666666【" + path + "】666666666666666666666666666666】"); fileUrl = path + fileUrl; String videoLength = r.ReadVideoTime(fileUrl);//視頻時(shí)長(zhǎng) System.out.println("===========================視頻時(shí)長(zhǎng):" + videoLength + "==========================="); return videoLength; } /** * 視頻時(shí)長(zhǎng) * * @param FileUrl * @return */ public static String ReadVideoTime(String FileUrl) { File source = new File(FileUrl); String length = ""; try { MultimediaObject instance = new MultimediaObject(source); MultimediaInfo result = instance.getInfo(); long ls = result.getDuration() / 1000; Integer hour = (int) (ls / 3600); Integer minute = (int) (ls % 3600) / 60; Integer second = (int) (ls - hour * 3600 - minute * 60); String hr = hour.toString(); String mi = minute.toString(); String se = second.toString(); if (hr.length() < 2) { hr = "0" + hr; } if (mi.length() < 2) { mi = "0" + mi; } if (se.length() < 2) { se = "0" + se; } length = hr + ":" + mi + ":" + se; } catch (Exception e) { e.printStackTrace(); } return length; } /** * 視頻大小 * * @param source * @return */ @SuppressWarnings({"resource"}) public static String ReadVideoSize(File source) { FileChannel fc = null; String size = ""; try { FileInputStream fis = new FileInputStream(source); fc = fis.getChannel(); BigDecimal fileSize = new BigDecimal(fc.size()); size = fileSize.divide(new BigDecimal(1024 * 1024), 2, RoundingMode.HALF_UP) + "MB"; } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { if (null != fc) { try { fc.close(); } catch (IOException e) { e.printStackTrace(); } } } return size; } }
以上就是Java獲取視頻時(shí)長(zhǎng)、大小的示例的詳細(xì)內(nèi)容,更多關(guān)于Java 視頻時(shí)長(zhǎng)、大小的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
springboot @Value實(shí)現(xiàn)獲取計(jì)算機(jī)中絕對(duì)路徑文件的內(nèi)容
這篇文章主要介紹了springboot @Value實(shí)現(xiàn)獲取計(jì)算機(jī)中絕對(duì)路徑文件的內(nèi)容,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。2021-09-09java程序運(yùn)行時(shí)內(nèi)存分配詳解
這篇文章主要介紹了java程序運(yùn)行時(shí)內(nèi)存分配詳解 ,需要的朋友可以參考下2016-07-07java學(xué)習(xí)之一維數(shù)組中重復(fù)元素的去除
關(guān)于一維數(shù)組中有重復(fù)的元素該怎么剔除,作為java初學(xué)者的我整理出不調(diào)用任何特殊庫(kù)的基礎(chǔ)方法,這種思想在其他語(yǔ)言也適用,有需要的朋友可以借鑒參考下2021-09-09Java實(shí)現(xiàn)的圖片上傳工具類(lèi)完整實(shí)例
這篇文章主要介紹了Java實(shí)現(xiàn)的圖片上傳工具類(lèi),涉及java針對(duì)圖片文件的檢查、上傳、清除等相關(guān)操作技巧,需要的朋友可以參考下2017-10-10