Java獲取視頻時(shí)長(zhǎng)及截取幀截圖詳解
前言
只是最近碰到有這方面的項(xiàng)目需求,所以簡(jiǎn)單 Mark 下本文。下面的示例是參考過他人分享的文章,之后本人再自行實(shí)踐、調(diào)整和測(cè)試過的,希望對(duì)有這方面需求的人有所幫助。
示例
添加依賴
<dependency>
<groupId>org.bytedeco</groupId>
<artifactId>javacv-platform</artifactId>
<version>1.4.4</version>
</dependency>
上述這段 maven 依賴包含了完整的 javacv 功能 (非常多,依賴Jar就占大概有500MB),由于這里只使用到了其中 ffmpeg 這塊的特性,因此也可以像下面這樣排除掉無關(guān)的部分
<dependency>
<groupId>org.bytedeco</groupId>
<artifactId>javacv</artifactId>
<version>1.4.4</version>
<exclusions>
<exclusion>
<groupId>org.bytedeco</groupId>
<artifactId>javacpp</artifactId>
</exclusion>
<exclusion>
<groupId>org.bytedeco.javacpp-presets</groupId>
<artifactId>flycapture</artifactId>
</exclusion>
<exclusion>
<groupId>org.bytedeco.javacpp-presets</groupId>
<artifactId>libdc1394</artifactId>
</exclusion>
<exclusion>
<groupId>org.bytedeco.javacpp-presets</groupId>
<artifactId>libfreenect</artifactId>
</exclusion>
<exclusion>
<groupId>org.bytedeco.javacpp-presets</groupId>
<artifactId>libfreenect2</artifactId>
</exclusion>
<exclusion>
<groupId>org.bytedeco.javacpp-presets</groupId>
<artifactId>librealsense</artifactId>
</exclusion>
<exclusion>
<groupId>org.bytedeco.javacpp-presets</groupId>
<artifactId>videoinput</artifactId>
</exclusion>
<exclusion>
<groupId>org.bytedeco.javacpp-presets</groupId>
<artifactId>opencv</artifactId>
</exclusion>
<exclusion>
<groupId>org.bytedeco.javacpp-presets</groupId>
<artifactId>tesseract</artifactId>
</exclusion>
<exclusion>
<groupId>org.bytedeco.javacpp-presets</groupId>
<artifactId>leptonica</artifactId>
</exclusion>
<exclusion>
<groupId>org.bytedeco.javacpp-presets</groupId>
<artifactId>flandmark</artifactId>
</exclusion>
<exclusion>
<groupId>org.bytedeco.javacpp-presets</groupId>
<artifactId>artoolkitplus</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.bytedeco</groupId>
<artifactId>javacv-platform</artifactId>
<version>1.4.4</version>
<exclusions>
<exclusion>
<groupId>org.bytedeco</groupId>
<artifactId>javacv</artifactId>
</exclusion>
<exclusion>
<groupId>org.bytedeco.javacpp-presets</groupId>
<artifactId>flycapture-platform</artifactId>
</exclusion>
<exclusion>
<groupId>org.bytedeco.javacpp-presets</groupId>
<artifactId>libdc1394-platform</artifactId>
</exclusion>
<exclusion>
<groupId>org.bytedeco.javacpp-presets</groupId>
<artifactId>libfreenect-platform</artifactId>
</exclusion>
<exclusion>
<groupId>org.bytedeco.javacpp-presets</groupId>
<artifactId>libfreenect2-platform</artifactId>
</exclusion>
<exclusion>
<groupId>org.bytedeco.javacpp-presets</groupId>
<artifactId>librealsense-platform</artifactId>
</exclusion>
<exclusion>
<groupId>org.bytedeco.javacpp-presets</groupId>
<artifactId>videoinput-platform</artifactId>
</exclusion>
<exclusion>
<groupId>org.bytedeco.javacpp-presets</groupId>
<artifactId>opencv-platform</artifactId>
</exclusion>
<exclusion>
<groupId>org.bytedeco.javacpp-presets</groupId>
<artifactId>tesseract-platform</artifactId>
</exclusion>
<exclusion>
<groupId>org.bytedeco.javacpp-presets</groupId>
<artifactId>leptonica-platform</artifactId>
</exclusion>
<exclusion>
<groupId>org.bytedeco.javacpp-presets</groupId>
<artifactId>flandmark-platform</artifactId>
</exclusion>
<exclusion>
<groupId>org.bytedeco.javacpp-presets</groupId>
<artifactId>artoolkitplus-platform</artifactId>
</exclusion>
</exclusions>
</dependency>
核心代碼
獲取視頻時(shí)長(zhǎng)
/**
* 獲取視頻時(shí)長(zhǎng),單位為秒
*
* @param video 源視頻文件
* @return 時(shí)長(zhǎng)(s)
*/
public static long getVideoDuration(File video) {
long duration = 0L;
FFmpegFrameGrabber ff = new FFmpegFrameGrabber(video);
try {
ff.start();
duration = ff.getLengthInTime() / (1000 * 1000);
ff.stop();
} catch (FrameGrabber.Exception e) {
e.printStackTrace();
}
return duration;
}
截取視頻指定幀為圖片
/**
* 截取視頻獲得指定幀的圖片
*
* @param video 源視頻文件
* @param picPath 截圖存放路徑
*/
public static void getVideoPic(File video, String picPath) {
FFmpegFrameGrabber ff = new FFmpegFrameGrabber(video);
try {
ff.start();
// 截取中間幀圖片(具體依實(shí)際情況而定)
int i = 0;
int length = ff.getLengthInFrames();
int middleFrame = length / 2;
Frame frame = null;
while (i < length) {
frame = ff.grabFrame();
if ((i > middleFrame) && (frame.image != null)) {
break;
}
i++;
}
// 截取的幀圖片
Java2DFrameConverter converter = new Java2DFrameConverter();
BufferedImage srcImage = converter.getBufferedImage(frame);
int srcImageWidth = srcImage.getWidth();
int srcImageHeight = srcImage.getHeight();
// 對(duì)截圖進(jìn)行等比例縮放(縮略圖)
int width = 480;
int height = (int) (((double) width / srcImageWidth) * srcImageHeight);
BufferedImage thumbnailImage = new BufferedImage(width, height, BufferedImage.TYPE_3BYTE_BGR);
thumbnailImage.getGraphics().drawImage(srcImage.getScaledInstance(width, height, Image.SCALE_SMOOTH), 0, 0, null);
File picFile = new File(picPath);
ImageIO.write(thumbnailImage, "jpg", picFile);
ff.stop();
} catch (IOException e) {
e.printStackTrace();
}
}
測(cè)試用例
public static void main(String[] args) {
String videoPath = ResourceUtils.CLASSPATH_URL_PREFIX + "video.mp4";
File video = null;
try {
video = ResourceUtils.getFile(videoPath);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
String picPath = "video.jpg";
getVideoPic(video, picPath);
long duration = getVideoDuration(video);
System.out.println("videoDuration = " + duration);
}
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Idea實(shí)現(xiàn)接口的方法上無法添加@Override注解的解決方案
文章介紹了在IDEA中實(shí)現(xiàn)接口方法時(shí)無法添加@Override注解的問題及其解決方法,主要步驟包括更改項(xiàng)目結(jié)構(gòu)中的Language level到支持該注解的版本,以及在pom.xml文件中指定maven-compiler-plugin的版本以解決自動(dòng)更新后的問題2025-02-02
關(guān)于Spring啟動(dòng)時(shí)Context加載源碼分析
這篇文章通過源碼分析主要給大家介紹了關(guān)于Spring啟動(dòng)時(shí)Context加載的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧。2018-01-01
Java實(shí)現(xiàn)字符串倒序輸出的常用方法小結(jié)
這篇文章主要介紹了Java實(shí)現(xiàn)字符串倒序輸出的常用方法,通過三個(gè)實(shí)例從不同角度實(shí)現(xiàn)該功能,有不錯(cuò)的借鑒價(jià)值,需要的朋友可以參考下2014-09-09
OPENCV+JAVA實(shí)現(xiàn)人臉識(shí)別
這篇文章主要為大家詳細(xì)介紹了OPENCV+JAVA實(shí)現(xiàn)人臉識(shí)別,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-02-02
java實(shí)現(xiàn)遠(yuǎn)程連接執(zhí)行命令行與上傳下載文件
這篇文章主要介紹了java實(shí)現(xiàn)遠(yuǎn)程連接執(zhí)行命令行與上傳下載文件方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-05-05
淺談Java內(nèi)部類與靜態(tài)內(nèi)部類的區(qū)別
本文主要介紹了淺談Java內(nèi)部類與靜態(tài)內(nèi)部類的區(qū)別,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-06-06
Spring Boot 中的 @ConditionalOnBean 注解場(chǎng)景分析
本文詳細(xì)介紹了Spring Boot中的@ConditionalOnBean注解的使用場(chǎng)景、原理和基本用法,通過多個(gè)示例,展示了如何使用該注解根據(jù)Bean是否存在來動(dòng)態(tài)地注冊(cè)或跳過特定的Bean,感興趣的朋友一起看看吧2025-03-03
Java中volatile關(guān)鍵字的線程的可見性、有序性詳解
這篇文章主要介紹了Java中volatile關(guān)鍵字的線程的可見性、有序性詳解,在juc多線程并發(fā)編程中,常常需要關(guān)注線程的"可見性"與"有序性",本文將詳細(xì)介紹這兩部分內(nèi)容,以及volatile關(guān)鍵字的使用,需要的朋友可以參考下2024-01-01
spring使用@Async注解導(dǎo)致循環(huán)依賴問題異常的排查記錄
這篇文章主要介紹了spring使用@Async注解導(dǎo)致循環(huán)依賴問題異常的排查記錄,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-08-08

