gpt4 book ai didi

java - 如何获取上传视频文件的缩略图?

转载 作者:行者123 更新时间:2023-12-01 16:52:31 26 4
gpt4 key购买 nike

几天来我一直在尝试为上传的文件创建缩略图,但没有成功。我尝试过 Xuggler、FFMpegFrameGrabber 和其他一些随机代码,但似乎都不起作用。从视频中捕获单个帧应该是一项微不足道的任务。有人知道我该怎么做吗?

最佳答案

package com.tape.controller;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import javax.imageio.ImageIO;
import org.bytedeco.javacv.FFmpegFrameGrabber;
import org.bytedeco.javacv.OpenCVFrameGrabber;

public class VideoThumbTaker {

protected String ffmpegApp;

public VideoThumbTaker(String ffmpegApp)
{
this.ffmpegApp = ffmpegApp;
}

public void getThumb(String videoFilename, String thumbFilename, int width, int height,int hour, int min, float sec)
throws IOException, InterruptedException
{
ProcessBuilder processBuilder = new ProcessBuilder(ffmpegApp, "-y", "-i", videoFilename, "-vframes", "1",
"-ss", hour + ":" + min + ":" + sec, "-f", "mjpeg", "-s", width + "*" + height, "-an", thumbFilename);
Process process = processBuilder.start();
InputStream stderr = process.getErrorStream();
InputStreamReader isr = new InputStreamReader(stderr);
BufferedReader br = new BufferedReader(isr);
String line;
while ((line = br.readLine()) != null);
process.waitFor();
}

public static void main(String[] args) throws Exception, IOException
{
FFmpegFrameGrabber g = new FFmpegFrameGrabber("C:\\JavaEE\\New Project\\tape\\src\\main\\webapp\\web-resources\\videos\\vid.mp4");
g.setFormat("mp4");
g.start();

for (int i = 0 ; i < 50 ; i++) {
ImageIO.write(g.grab().getBufferedImage(), "png", new File("C:\\JavaEE\\New Project\\tape\\src\\main\\webapp\\web-resources\\thumbnails\\video-frame-" + System.currentTimeMillis() + ".png"));
}

g.stop();
}
}

我使用 FFMpegFrameGrabber 的 Java 代码来创建视频的缩略图。尝试一下。

Maven 依赖:

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

关于java - 如何获取上传视频文件的缩略图?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37163978/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com