Java獲取視頻的大小、時長


前端上傳視頻之后,根據上傳的視頻文件獲取視頻的大小和時長

1、獲取視頻時長

private String ReadVideoTime(File source) {
Encoder encoder = new Encoder();
String length = "";
try {
MultimediaInfo m = encoder.getInfo(source);
long ls = m.getDuration()/1000;
int hour = (int) (ls/3600);
int minute = (int) (ls%3600)/60;
int second = (int) (ls-hour*3600-minute*60);
length = hour+"'"+minute+"''"+second+"'''";
} catch (Exception e) {
e.printStackTrace();
}
return length;
}

 

2、獲取視頻大小

/**
* 獲取視頻大小
* @param source
* @return
*/
private String ReadVideoSize(File source) {
FileChannel fc= null;
String size = "";
try {
@SuppressWarnings("resource")
FileInputStream fis = new FileInputStream(source);
fc= fis.getChannel();
BigDecimal fileSize = new BigDecimal(fc.size());
size = fileSize.divide(new BigDecimal(1048576), 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;
}

***獲取視頻大小的時候,由於用到了流,使用完之后一定要及時的關閉流,避免無法刪除視頻文件***


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM