最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Java之如何截取視頻第一幀

 更新時(shí)間:2023年06月19日 09:07:25   作者:上官天夜  
這篇文章主要介紹了Java之如何截取視頻第一幀問題,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教

Java截取視頻第一幀

方法一:使用第三方j(luò)ar包截取

1、導(dǎo)入依賴

<dependency>
? ? ? ? <groupId>org.bytedeco</groupId>
? ? ? ? <artifactId>javacv</artifactId>
? ? ? ? <version>0.8</version>
? ? </dependency>

2、示例

package com.zemel.video;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.File;
import javax.imageio.ImageIO;
import org.bytedeco.javacpp.opencv_core.IplImage;
import org.bytedeco.javacv.FFmpegFrameGrabber;
import org.bytedeco.javacv.Frame;
public class Test {
?? ?/**
?? ? * 獲取指定視頻的幀并保存為圖片至指定目錄
?? ? * @param videofile ?源視頻文件路徑
?? ? * @param framefile ?截取幀的圖片存放路徑
?? ? * @throws Exception
?? ? */
?? ?public static void fetchFrame(String videofile, String framefile)
?? ? ? ? ? ?throws Exception {
?? ? ? ?long start = System.currentTimeMillis();
?? ? ? ?File targetFile = new File(framefile);
?? ? ? ?FFmpegFrameGrabber ff = new FFmpegFrameGrabber(videofile);?
?? ? ? ?ff.start();
?? ? ? ?int lenght = ff.getLengthInFrames();
?? ? ? ?int i = 0;
?? ? ? ?Frame f = null;
?? ? ? ?while (i < lenght) {
?? ? ? ? ? ?// 過濾前5幀,避免出現(xiàn)全黑的圖片,依自己情況而定
?? ? ? ? ? ?f = ff.grabFrame();
?? ? ? ? ? ?if ((i > 5) && (f.image != null)) {
?? ? ? ? ? ? ? ?break;
?? ? ? ? ? ?}
?? ? ? ? ? ?i++;
?? ? ? ?}
?? ? ? ?IplImage img = f.image;
?? ? ? ?int owidth = img.width();
?? ? ? ?int oheight = img.height();
?? ? ? ?// 對截取的幀進(jìn)行等比例縮放
?? ? ? ?int width = 800;
?? ? ? ?int height = (int) (((double) width / owidth) * oheight);
?? ? ? ?BufferedImage bi = new BufferedImage(width, height, BufferedImage.TYPE_3BYTE_BGR);
?? ? ? ?bi.getGraphics().drawImage(f.image.getBufferedImage().getScaledInstance(width, height, Image.SCALE_SMOOTH),
?? ? ? ? ? ? ? ?0, 0, null);
?? ? ? ?ImageIO.write(bi, "jpg", targetFile);
?? ? ? ?//ff.flush();
?? ? ? ?ff.stop();
?? ? ? ?System.out.println(System.currentTimeMillis() - start);
?? ?}
?? ?public static void main(String[] args) {
?? ? ? ?try {
?? ? ? ? ? ?Test.fetchFrame("D:\\biudata\\vedio\\1523598768844GFE2GWDDM8.mp4", "D:\\biudata\\vedio\\test5.jpg");
?? ? ? ?} catch (Exception e) {
?? ? ? ? ? ?e.printStackTrace();
?? ? ? ?}
?? ?}
}

方法二:使用ffmpeg

1、下載ffmpeg工具(http://ffmpeg.org/)

2、代碼

public static boolean processImg(String veido_path, String ffmpeg_path) {
		File file = new File(veido_path);
		if (!file.exists()) {
			System.err.println("路徑[" + veido_path + "]對應(yīng)的視頻文件不存在!");
			return false;
		}
		List<String> commands = new java.util.ArrayList<String>();
		commands.add(ffmpeg_path);
		commands.add("-i");
		commands.add(veido_path);
		commands.add("-y");
		commands.add("-f");
		commands.add("image2");
		commands.add("-ss");
		commands.add("8");// 這個(gè)參數(shù)是設(shè)置截取視頻多少秒時(shí)的畫面
		// commands.add("-t");
		// commands.add("0.001");
		commands.add("-s");
		commands.add("700x525");
		commands.add(veido_path.substring(0, veido_path.lastIndexOf("."))
				.replaceFirst("vedio", "file") + ".jpg");
		try {
			ProcessBuilder builder = new ProcessBuilder();
			builder.command(commands);
			builder.start();
			System.out.println("截取成功");
			return true;
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
	}
	public static void main(String[] args) {
		processImg("D:\\biudata\\vedio\\15235879054813G0I1783E2.mp4",
				"F:\\開發(fā)工具\(yùn)\ffmpeg.exe");
	}

Java截取視頻第一幀返回InputStream,用于視頻上傳后作為封面

引入maven依賴

<dependency>
?? ?<groupId>org.bytedeco</groupId>
?? ?<artifactId>javacv-platform</artifactId>
?? ?<version>1.4.3</version>
</dependency>

因?yàn)樯厦娴囊蕾嚢膉ar包太多,所以我們需要排除一些東西

?? ?<dependency>
?? ??? ?<groupId>org.bytedeco</groupId>
?? ??? ?<artifactId>javacv</artifactId>
?? ??? ?<version>1.4.3</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.3</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>

獲取視頻幀返回InputStream

public class getImgUtil {
? ? // 獲取要取得的幀數(shù)
? ? private static final int fifthFrame= 5;
? ? /**
? ? ?* @param InputStream ?需要截取幀的視頻的字節(jié)輸入流
? ? ?*
? ? ?* @return?
? ? ?*/
? ? public static InputStream getImg(InputStream is) {
? ? ? ? FFmpegFrameGrabber grabber;
? ? ? ? InputStream img=null ;
? ? ? ? try {
? ? ? ? ? ? grabber = new FFmpegFrameGrabber(is);
? ? ? ? ? ? grabber.start();
? ? ? ? ? ? // 視頻總幀數(shù)
? ? ? ? ? ? int videoLength = grabber.getLengthInFrames();
? ? ? ? ? ? Frame frame = null;
? ? ? ? ? ? int i = 0;
? ? ? ? ? ? while (i < videoLength) {
? ? ? ? ? ? ? ? // 過濾前5幀,因?yàn)榍?幀可能是全黑的
? ? ? ? ? ? ? ? frame = grabber.grabFrame();
? ? ? ? ? ? ? ? if ((i > fifthFrame) && (frame.image != null)) {
? ? ? ? ? ? ? ? ? ? break;
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? i++;
? ? ? ? ? ? }
? ? ? ? ? ? Java2DFrameConverter converter = new Java2DFrameConverter();
? ? ? ? ? ? // 繪制圖片
? ? ? ? ? ? BufferedImage bi = converter.getBufferedImage(frame);
? ? ? ? ? ? img = bufferedImageToInputStream(bi);
? ? ? ? ? ? grabber.stop();
? ? ? ? ? ? grabber.close();
? ? ? ? } catch (IOException e) {
? ? ? ? ? ? e.printStackTrace();
? ? ? ? }
? ? ? ? return img;
? ? }
? ? /**
?? ? * 將BufferedImage轉(zhuǎn)換為InputStream
?? ? * @param image
?? ? * @return
?? ? */
?? ?public static InputStream bufferedImageToInputStream(BufferedImage image){
?? ? ? ?ByteArrayOutputStream os = new ByteArrayOutputStream();
?? ? ? ?try {
?? ? ? ? ? ?ImageIO.write(image, "png", os);
?? ? ? ? ? ?InputStream input = new ByteArrayInputStream(os.toByteArray());
?? ? ? ? ? ?return input;
?? ? ? ?} catch (IOException e) {
?? ? ? ?}
?? ? ? ?return null;
?? ?}

總結(jié)

以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • sentinel?整合spring?cloud限流的過程解析

    sentinel?整合spring?cloud限流的過程解析

    這篇文章主要介紹了sentinel?整合spring?cloud限流,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2022-03-03
  • Java基礎(chǔ)學(xué)習(xí)筆記之?dāng)?shù)組詳解

    Java基礎(chǔ)學(xué)習(xí)筆記之?dāng)?shù)組詳解

    這篇文章主要介紹了Java基礎(chǔ)學(xué)習(xí)筆記之?dāng)?shù)組,結(jié)合實(shí)例形式詳細(xì)分析了java的基本概念、定義、迭代、輸出、反轉(zhuǎn)、排序等常用操作技巧,需要的朋友可以參考下
    2019-08-08
  • mybatis注解與xml常用語句匯總

    mybatis注解與xml常用語句匯總

    最近一直在用mybatis,由于需要使用到了動(dòng)態(tài)sql,遇到了一些問題,現(xiàn)在來總結(jié)一下,經(jīng)驗(yàn)教訓(xùn)。下面這篇文章主要給大家總結(jié)介紹了mybatis注解與xml常用語句的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2018-09-09
  • SpringBoot隨機(jī)端口啟動(dòng)的實(shí)現(xiàn)

    SpringBoot隨機(jī)端口啟動(dòng)的實(shí)現(xiàn)

    本文主要介紹了SpringBoot隨機(jī)端口啟動(dòng)的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2021-07-07
  • Java中List排序的三種實(shí)現(xiàn)方法實(shí)例

    Java中List排序的三種實(shí)現(xiàn)方法實(shí)例

    其實(shí)Java針對數(shù)組和List的排序都有實(shí)現(xiàn),對數(shù)組而言你可以直接使用Arrays.sort,對于List和Vector而言,你可以使用Collections.sort方法,下面這篇文章主要給大家介紹了關(guān)于Java中List排序的三種實(shí)現(xiàn)方法,需要的朋友可以參考下
    2021-12-12
  • springboot聚合工程的部署與深入講解

    springboot聚合工程的部署與深入講解

    最近在寫一個(gè)商城,使用Maven聚合工程來管理,但是其中搭建環(huán)境遇見了各種的坑,下面這篇文章主要給大家介紹了關(guān)于springboot聚合工程部署的相關(guān)資料,需要的朋友可以參考下
    2021-11-11
  • 深度解析Java中volatile的內(nèi)存語義實(shí)現(xiàn)以及運(yùn)用場景

    深度解析Java中volatile的內(nèi)存語義實(shí)現(xiàn)以及運(yùn)用場景

    這篇文章主要介紹了Java中volatile的內(nèi)存語義實(shí)現(xiàn)以及運(yùn)用場景,通過JVM的機(jī)制來分析volatile關(guān)鍵字在線程編程中的作用,需要的朋友可以參考下
    2015-12-12
  • SpringCloud?eureka(server)微服務(wù)集群搭建過程

    SpringCloud?eureka(server)微服務(wù)集群搭建過程

    這篇文章主要介紹了微服務(wù)SpringCloud-eureka(server)集群搭建,?項(xiàng)目搭建的主要步驟和配置就是創(chuàng)建項(xiàng)目和引入pom依賴,本文通過圖文示例代碼相結(jié)合給大家介紹的非常詳細(xì),需要的朋友可以參考下
    2022-07-07
  • 一文徹底搞懂Java項(xiàng)目中DI注入失敗的六大常見原因與解法

    一文徹底搞懂Java項(xiàng)目中DI注入失敗的六大常見原因與解法

    在企業(yè)級 Java 開發(fā)中,Spring 框架的依賴注入幾乎是每個(gè)項(xiàng)目的標(biāo)配,本文將結(jié)合真實(shí)項(xiàng)目經(jīng)驗(yàn),系統(tǒng)梳理依賴注入失敗的六大常見場景,幫助你快速定位問題、理解本質(zhì)、避免踩坑
    2025-11-11
  • 詳解mybatis-plus的 mapper.xml 路徑配置的坑

    詳解mybatis-plus的 mapper.xml 路徑配置的坑

    這篇文章主要介紹了詳解mybatis-plus的 mapper.xml 路徑配置的坑,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-08-08

最新評論

南开区| 盐津县| 林口县| 潮州市| 广昌县| 当涂县| 遂宁市| 利川市| 彩票| 天柱县| 三门县| 夏河县| 福海县| 女性| 阿合奇县| 桦川县| 民和| 绍兴市| 家居| 叙永县| 郯城县| 仪征市| 视频| 灵寿县| 泗水县| 江华| 临高县| 五台县| 遵化市| 闻喜县| 额尔古纳市| 锡林郭勒盟| 阜城县| 伊通| 英德市| 上饶县| 兴国县| 德安县| 白河县| 莫力| 庆元县|