如何使用java从视频字节[]中获取缩略图?

问题描述

我的要求是,我想在fancybox显示视频缩略图。但不幸的是它没有显示缩略图缩略图所需的单独图像。

fancybox 代码示例

<a data-fancybox data-type="iframe" data-width="640" data-height="360" href="http://download.blender.org/peach/bigbuckbunny_movies/BigBuckBunny_320x180.mp4">
    <img class="card-img-top img-fluid" src="https://peach.blender.org/wp-content/uploads/bird1.jpg" />
</a>

所以我决定维护两个url

一个用于视频,另一个用于视频缩略图

  1. http://localhost:8080/resources?resourceId=126 -> 会返回视频
  2. http://localhost:8080/resources?resourceId=126&thumbnail=true -> 返回缩略图

我对此一无所知,但我曾尝试使用 https://github.com/bytedeco/javacv 获取视频缩略图。但是我遇到了“OutofMemory”异常

pom.xml

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

我的缩略图的java servlet代码如下

Java2DFrameConverter converter = new Java2DFrameConverter();
BufferedImage current;
BufferedImage next;
byte[] videoBytes = //video byte array fetched from db
ByteArrayOutputStream baos = new ByteArrayOutputStream();

FFmpegFrameGrabber frameGrabber = new FFmpegFrameGrabber(new ByteArrayInputStream(videoBytes));
frameGrabber.start();
try {
    Frame frame = frameGrabber.grab();
    next = converter.convert(frame);

    current = new BufferedImage(next.getWidth(),next.getHeight(),next.getType());
    ImageIO.write(current,"png",baos);
    byte[] bytes = baos.toByteArray();
    frameGrabber.stop();


    response.setContentType("image/png");
    try (OutputStream o = response.getoutputStream()) {
        o.write(bytes);
        o.flush();
    }

编辑

视频部分工作正常。即可以从这个网址观看视频 http://localhost:8080/resources?resourceId=126

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)