Get Thumbnail from video in Java

旧城冷巷雨未停 提交于 2019-12-24 03:07:33

问题


I want to create a thumbnail from a video in a servlet or any other server side Java-Method.

The video-file is uploaded on a Server and after the upload the thumbnail shall be created.

My Problem there while is not to create thumbnails, but to create only one or a certain amount of thumbnails.

My Code so far:

public class Test {
    public static void main(String[] args) throws IOException, InterruptedException, IM4JavaException {
        createThumbnail(new File("00-50-C2-1D-7F-85_005.avi"), 512);
    }

    private static void createThumbnail(File sourceImage, int width) throws IOException,
        InterruptedException, IM4JavaException {
        ConvertCmd cmd = new ConvertCmd();
        String destinationFileName = sourceImage.getName() + "_" + width + "_" + "thumb.png";
        File thumbNailFile = new File(destinationFileName);
        if (!thumbNailFile.exists()) {
            IMOperation op = new IMOperation();
            op.addImage(sourceImage.toString());
            op.thumbnail(width);
            op.addImage(destinationFileName);
            cmd.run(op);
        }
    }
}

The Problem here is: for each frame from the video a thumbnail is created. do in my test video of 21,012s over 1000 thumbnails are getting created.

Is there any way to create a thumbnail from a video every Xseconds or frames in Java?

EDIT1: I also tried to use FFMpegwith the FFMpegWrapper fom com.day.cq.dam.handler.ffmpeg.FFMpegWrapper used as Maven-depenency:

   <dependency>
      <groupId>com.day.cq.dam</groupId>
      <artifactId>cq-dam-video</artifactId>
      <version>5.6.2</version>
    </dependency>

with the class:

public class Test2 {
    public static void main(String[] args) throws IOException {
        String videoName = "Roentgen_A_VisarioG2_005.avi";
        File videoFile = new File(videoName);
        testFFMpeg(videoFile); // FFMpeg

        // lets try VideoImageSource
        VideoImageSource viSource = new VideoImageSource(videoFile);
        viSource.setMediaTime(10);
        BufferedImage thumb = viSource.getImage();
        File output = new File(videoName + "_thumb.png");
        ImageIO.write(thumb, "png", output);
    }

    public static void testFFMpeg(File videoFile) throws IOException {
        FFMpegWrapper wraper = new FFMpegWrapper(videoFile);
        int length = (int) wraper.getInputDuration() / 1000 / 2;
        BufferedImage[] thumbnail = wraper.getThumbnails(10, length);
        if (thumbnail == null) {
            System.out.println("[ERROR] no thumbnail created!");
            return;
        }
        ImageIO.write(thumbnail[0], "png", new File(videoFile.getAbsolutePath() + "_thumb.png"));
    }
}

also ViedeoImageSource taken from:https://code.google.com/p/vitalopensource/source/browse/trunk/src/com/vtls/opensource/image/VideoImageSource.java doesn't work. With FFMpegWrapper (http://docs.adobe.com/docs/en/cq/5-6/javadoc/com/day/cq/dam/handler/ffmpeg/FFMpegWrapper.html) I don't receive any BufferedImage (getThumnbails() returns null) and with VideoImageSource i get the Error: Unable to handle format: H264, 1536x1024, FrameRate=50.0, Length=4722688 0 extra bytes Failed to realize: com.sun.media.PlaybackEngine@5defbbf

Error: Unable to realize com.sun.media.PlaybackEngine@5defbbf Exception in thread "main" java.lang.IllegalArgumentException: image == null! at javax.imageio.ImageTypeSpecifier.createFromRenderedImage(ImageTypeSpecifier.java:925) at javax.imageio.ImageIO.getWriter(ImageIO.java:1591) at javax.imageio.ImageIO.write(ImageIO.java:1520) at testVideos.Test2.main(Test2.java:37)

none of Methods in test2-class does work and class test gives me a thumbnail for each frame... :(

来源:https://stackoverflow.com/questions/30208850/get-thumbnail-from-video-in-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!