欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Java之如何截取視頻第一幀

 更新時(shí)間:2023年06月19日 09:07:25   作者:上官天夜  
這篇文章主要介紹了Java之如何截取視頻第一幀問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(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();
?? ? ? ?// 對(duì)截取的幀進(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 + "]對(duì)應(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)文章

  • 淺析Java基于Socket的文件傳輸案例

    淺析Java基于Socket的文件傳輸案例

    這篇文章主要針對(duì)Java基于Socket的文件傳輸案例進(jìn)行詳細(xì)解析,具有一定的參考價(jià)值,感興趣的朋友可以參考一下
    2016-02-02
  • spring+springmvc+mybatis 開發(fā)JAVA單體應(yīng)用

    spring+springmvc+mybatis 開發(fā)JAVA單體應(yīng)用

    這篇文章主要介紹了spring+springmvc+mybatis 開發(fā)JAVA單體應(yīng)用的相關(guān)知識(shí),本文通過圖文實(shí)例代碼的形式給大家介紹的非常詳細(xì) ,需要的朋友可以參考下
    2018-11-11
  • Kafka的安裝及接入SpringBoot的詳細(xì)過程

    Kafka的安裝及接入SpringBoot的詳細(xì)過程

    Kafka 是一種高性能、分布式的消息隊(duì)列系統(tǒng),最初由 LinkedIn 公司開發(fā),并于2011年成為 Apache 頂級(jí)項(xiàng)目,這篇文章主要介紹了Kafka的安裝及接入SpringBoot,需要的朋友可以參考下
    2024-05-05
  • Java synchronized底層的實(shí)現(xiàn)原理

    Java synchronized底層的實(shí)現(xiàn)原理

    這篇文章主要介紹了Java synchronized底層的實(shí)現(xiàn)原理,文章基于Java來介紹 synchronized 是如何運(yùn)行的,內(nèi)容詳細(xì)具有一定的參考價(jià)值,需要的小伙伴可以參考一下
    2022-05-05
  • Java實(shí)現(xiàn)最小高度樹

    Java實(shí)現(xiàn)最小高度樹

    本文主要介紹了Java實(shí)現(xiàn)最小高度樹,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2022-04-04
  • Java 實(shí)現(xiàn)文件批量重命名親測(cè)可用(精簡(jiǎn)版)

    Java 實(shí)現(xiàn)文件批量重命名親測(cè)可用(精簡(jiǎn)版)

    本文給大家分享一段自己寫的java代碼實(shí)現(xiàn)文件批量重命名,親測(cè)試過沒有任何問題,大家可以放心使用
    2016-11-11
  • Java數(shù)組的定義、初始化、及二維數(shù)組用法分析

    Java數(shù)組的定義、初始化、及二維數(shù)組用法分析

    這篇文章主要介紹了Java數(shù)組的定義、初始化、及二維數(shù)組用法,結(jié)合具體實(shí)例形式分析了java數(shù)組概念、功能、數(shù)組定義、靜態(tài)數(shù)組、動(dòng)態(tài)數(shù)組、二維數(shù)組等相關(guān)使用技巧,需要的朋友可以參考下
    2019-01-01
  • Java多線程中線程間的通信實(shí)例詳解

    Java多線程中線程間的通信實(shí)例詳解

    這篇文章主要介紹了Java多線程中線程間的通信實(shí)例詳解的相關(guān)資料,需要的朋友可以參考下
    2017-04-04
  • Redis實(shí)現(xiàn)商品秒殺功能頁(yè)面流程

    Redis實(shí)現(xiàn)商品秒殺功能頁(yè)面流程

    這篇文章主要介紹了Redis實(shí)現(xiàn)商品秒殺功能的方法,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2022-09-09
  • Java synchronized偏向鎖的概念與使用

    Java synchronized偏向鎖的概念與使用

    因?yàn)樵谖覀儗懙某绦虍?dāng)中可能會(huì)經(jīng)常使用到synchronized關(guān)鍵字,因此JVM對(duì)synchronized做出了很多優(yōu)化,而在本篇文章當(dāng)中我們將仔細(xì)介紹JVM對(duì)synchronized的偏向鎖的細(xì)節(jié)
    2023-02-02

最新評(píng)論