Java之如何截取視頻第一幀
更新時間:2023年06月19日 09:07:25 作者:上官天夜
這篇文章主要介紹了Java之如何截取視頻第一幀問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
Java截取視頻第一幀
方法一:使用第三方j(luò)ar包截取
1、導入依賴
<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();
?? ? ? ?// 對截取的幀進行等比例縮放
?? ? ? ?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");// 這個參數(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ā)工具\\ffmpeg.exe");
}Java截取視頻第一幀返回InputStream,用于視頻上傳后作為封面
引入maven依賴
<dependency> ?? ?<groupId>org.bytedeco</groupId> ?? ?<artifactId>javacv-platform</artifactId> ?? ?<version>1.4.3</version> </dependency>
因為上面的依賴包含的jar包太多,所以我們需要排除一些東西
?? ?<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幀,因為前5幀可能是全黑的
? ? ? ? ? ? ? ? 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é)
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
sentinel?整合spring?cloud限流的過程解析
這篇文章主要介紹了sentinel?整合spring?cloud限流,本文通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-03-03
深度解析Java中volatile的內(nèi)存語義實現(xiàn)以及運用場景
這篇文章主要介紹了Java中volatile的內(nèi)存語義實現(xiàn)以及運用場景,通過JVM的機制來分析volatile關(guān)鍵字在線程編程中的作用,需要的朋友可以參考下2015-12-12
SpringCloud?eureka(server)微服務(wù)集群搭建過程
這篇文章主要介紹了微服務(wù)SpringCloud-eureka(server)集群搭建,?項目搭建的主要步驟和配置就是創(chuàng)建項目和引入pom依賴,本文通過圖文示例代碼相結(jié)合給大家介紹的非常詳細,需要的朋友可以參考下2022-07-07
詳解mybatis-plus的 mapper.xml 路徑配置的坑
這篇文章主要介紹了詳解mybatis-plus的 mapper.xml 路徑配置的坑,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2020-08-08

