這篇文章給大家分享的是有關(guān)Java如何獲取視頻時長及截取幀截圖的內(nèi)容。小編覺得挺實用的,因此分享給大家做個參考,一起跟隨小編過來看看吧。
創(chuàng)新互聯(lián)是一家專注于成都做網(wǎng)站、網(wǎng)站制作與策劃設(shè)計,鐵東網(wǎng)站建設(shè)哪家好?創(chuàng)新互聯(lián)做網(wǎng)站,專注于網(wǎng)站建設(shè)十載,網(wǎng)設(shè)計領(lǐng)域的專業(yè)建站公司;建站業(yè)務(wù)涵蓋:鐵東等地區(qū)。鐵東做網(wǎng)站價格咨詢:18980820575
示例
添加依賴
<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>
核心代碼
獲取視頻時長
/** * 獲取視頻時長,單位為秒 * * @param video 源視頻文件 * @return 時長(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(); // 截取中間幀圖片(具體依實際情況而定) 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(); // 對截圖進(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(); } }
測試用例
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); }
Java是一門面向?qū)ο缶幊陶Z言,可以編寫桌面應(yīng)用程序、Web應(yīng)用程序、分布式系統(tǒng)和嵌入式系統(tǒng)應(yīng)用程序。
感謝各位的閱讀!關(guān)于“Java如何獲取視頻時長及截取幀截圖”這篇文章就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,讓大家可以學(xué)到更多知識,如果覺得文章不錯,可以把它分享出去讓更多的人看到吧!
本文標(biāo)題:Java如何獲取視頻時長及截取幀截圖
網(wǎng)站鏈接:http://jinyejixie.com/article4/peosoe.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供Google、網(wǎng)站營銷、企業(yè)建站、定制網(wǎng)站、ChatGPT、云服務(wù)器
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)