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

时间:2016-05-11 13:20:58

标签: java video thumbnails

我一直试图为上传的文件创建缩略图几天,但无济于事。我尝试过Xuggler,FFMpegFrameGrabber和其他一些随机代码,似乎没有任何工作。从视频中捕获单个帧应该是一项微不足道的任务。有谁知道我怎么能这样做?

6 个答案:

答案 0 :(得分:4)

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代码来创建视频的thumnails。尝试一下。

Maven依赖:

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

答案 1 :(得分:1)

我写了一个使用io.humble创建视频图片的简单示例。但它是一个scala类。你可以检查它并根据需要将其修改为java版本。

看看: https://github.com/LeoWolfLai/io.humble-example/blob/master/io.humble-example.scala

答案 2 :(得分:1)

按照this link进行依赖

SELECT   payees.supplier_site_id csupplier_site_id,
  payees.org_id c_org_id,
  payees.payee_party_id ,
  cbbv.bank_name,
   max(''''||to_char(ieb.bank_account_num)) as  bank_account_num   
FROM apps.iby_pmt_instr_uses_all instrument,
  apps.iby_account_owners owners,
  apps.iby_external_payees_all payees,
  apps.iby_ext_bank_accounts ieb,
   apps.ap_supplier_sites_all asa,
   apps.ap_suppliers asp,
  apps.ce_bank_branches_v cbbv
WHERE owners.primary_flag      = 'Y'
AND owners.ext_bank_account_id = ieb.ext_bank_account_id
AND owners.ext_bank_account_id = instrument.instrument_id
AND payees.ext_payee_id        = instrument.ext_pmt_party_id
AND payees.payee_party_id      = owners.account_owner_party_id
AND payees.supplier_site_id    = asa.vendor_site_id
AND asa.vendor_id              = asp.vendor_id  
and bank_party_id(+) = ieb.bank_id  
--AND cbbv.branch_party_id(+)    = ieb.branch_id
AND (
(INSTRUMENT.END_DATE IS NULL)
OR
( INSTRUMENT.END_DATE >= TO_DATE(SYSDATE) )  
) 
AND (
(IEB.END_DATE IS NULL)
OR
(IEB.END_DATE >= TO_DATE(SYSDATE))
)
 group by  payees.supplier_site_id  ,
  payees.org_id ,
  payees.payee_party_id ,
  cbbv.bank_name

答案 3 :(得分:0)

使用 ffmpegthumbnailer 快速轻量级视频缩略图。

答案 4 :(得分:0)

您可以使用humble-videoXuggle-xuggler sucesor)

与maven一起使用:

<project>
  ...
  <dependencies>
    ...
    <dependency>
      <groupId>io.humble</groupId>
      <artifactId>humble-video-all</artifactId>
      <version>0.2.1</version>
    </dependency>
  </dependencies>
</project>

没有简单视频的例子,但我想它与xuggler类似,请看DecodeAndCaptureFrames.java

答案 5 :(得分:0)

使用humble-video

import io.humble.video.*;
import io.humble.video.awt.MediaPictureConverter;
import io.humble.video.awt.MediaPictureConverterFactory;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.*;
import java.nio.file.Path;

    Demuxer demuxer = Demuxer.make();
    demuxer.open(videoFile.toString(), null, false, true, null, null);
    final int numStreams = demuxer.getNumStreams();
    int streamIndex = -1;
    Decoder videoDecoder = null;
    for (int i = 0; i < numStreams; ++i) {
        DemuxerStream demuxerStream = demuxer.getStream(i);
        Decoder decoder = demuxerStream.getDecoder();
        if (decoder != null || decoder.getCodecType() == MediaDescriptor.Type.MEDIA_VIDEO) {
            videoDecoder = decoder;
            streamIndex = i;
            break;
        }
    }
    if (videoDecoder == null) {
        throw new IOException("Not a valid video file");
    }
    videoDecoder.open(null, null);

    final MediaPicture picture = MediaPicture.make(
        videoDecoder.getWidth(),
        videoDecoder.getHeight(),
        videoDecoder.getPixelFormat());

    final MediaPictureConverter converter =
        MediaPictureConverterFactory.createConverter(
            MediaPictureConverterFactory.HUMBLE_BGR_24,
            picture);

    final MediaPacket packet = MediaPacket.make();
    BufferedImage image = null;
    MUX:
    while (demuxer.read(packet) >= 0) {
        if (packet.getStreamIndex() != streamIndex) {
            continue;
        }
        int offset = 0;
        int bytesRead = 0;
        videoDecoder.decodeVideo(picture, packet, offset);
        do {
            bytesRead += videoDecoder.decode(picture, packet, offset);
            if (picture.isComplete()) {
                image = converter.toImage(null, picture);
                break MUX;
            }
            offset += bytesRead;

        } while (offset < packet.getSize());
    }
    ImageIO.write(image, "jpeg", new File("foo.jpg"));