Grab thumbnail of videos (Windows)

Asked

Viewed 200 times

5

I Googled several terms of how to get the thumbnail of the videos but only found how to "generate" the thumbnail of a frame of the video using the Xuggler, but I’m making mistakes.

The mistake I’m having is: Imagem com o erro

Follow my class down:

package util;

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.List;

import javax.imageio.ImageIO;

import com.xuggle.mediatool.IMediaReader;
import com.xuggle.mediatool.MediaListenerAdapter;
import com.xuggle.mediatool.ToolFactory;
import com.xuggle.mediatool.event.IVideoPictureEvent;
import com.xuggle.xuggler.Global;

import model.Video;

public class BuildThumbnailUtil {

    public static final double SECONDS_BETWEEN_FRAMES = 1;
    private static final String outputFilePrefix = "C:/Documents and Settings/Panda/Desktop/MovieList/";
    private static int mVideoStreamIndex = -1;
    private static long mLastPtsWrite = Global.NO_PTS;
    public static final long MICRO_SECONDS_BETWEEN_FRAMES = (long) (Global.DEFAULT_PTS_PER_SECOND * SECONDS_BETWEEN_FRAMES);

    public static void buildThumbnail(List<Video> videos) {
        for (Video v : videos) {
            IMediaReader mediaReader = ToolFactory.makeReader(v.getPath());
            try {
                mediaReader.setBufferedImageTypeToGenerate(BufferedImage.TYPE_3BYTE_BGR);
                ImageSnapListener isListener = new ImageSnapListener();
                mediaReader.addListener(isListener);

                while (!isListener.isImageGrabbed()) {
                    mediaReader.readPacket();
                }
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }
    }

    private static class ImageSnapListener extends MediaListenerAdapter {

        public boolean imageGrabbed = false;

        public void onVideoPicture(IVideoPictureEvent event) {
            if (event.getStreamIndex() != mVideoStreamIndex) {
                if (mVideoStreamIndex == -1)
                    mVideoStreamIndex = event.getStreamIndex();
                else
                    return;
            }
            if (mLastPtsWrite == Global.NO_PTS)
                mLastPtsWrite = event.getTimeStamp() - MICRO_SECONDS_BETWEEN_FRAMES;
            if (event.getTimeStamp() - mLastPtsWrite >= MICRO_SECONDS_BETWEEN_FRAMES * 10) {
                String outputFilename = dumpImageToFile(event.getImage());
                this.imageGrabbed = true;
                double seconds = ((double) event.getTimeStamp()) / Global.DEFAULT_PTS_PER_SECOND;
                System.out.printf("at elapsed time of %6.3f seconds wrote: %s\n", seconds, outputFilename);
                mLastPtsWrite += MICRO_SECONDS_BETWEEN_FRAMES;
            }
        }

        private String dumpImageToFile(BufferedImage image) {
            try {
                String outputFilename = outputFilePrefix + System.currentTimeMillis() + ".jpg";
                System.out.println("Thumbnail image name is going to be : =====>" + outputFilename);
                ImageIO.write(image, "jpg", new File(outputFilename));
                return outputFilename;
            } catch (IOException e) {
                e.printStackTrace();
                return null;
            }
        }

        public boolean isImageGrabbed() {
            return imageGrabbed;
        }
    }
}

Does anyone know if they have a way to get the same thumbnail as a video, just like Windows displays?

  • Pandemonio, you could paste the error message in textual format (images are difficult to see and make life difficult for those who will respond).

2 answers

1

As I answered in your other topic, follow another class that also works perfectly here!

package xug;


import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;

import com.xuggle.mediatool.IMediaReader;
import com.xuggle.mediatool.MediaListenerAdapter;
import com.xuggle.mediatool.ToolFactory;
import com.xuggle.mediatool.event.IVideoPictureEvent;
import com.xuggle.xuggler.Global;

public class Main {

    public static final double SECONDS_BETWEEN_FRAMES = 10;
    private static final String inputFilename = "D:\\pasta de videos\\Live for free.flv";
    private static final String outputFilePrefix = "D:\\imaagens\\";

    private static int mVideoStreamIndex = -1;
    private static long mLastPtsWrite = Global.NO_PTS;
    public static final long MICRO_SECONDS_BETWEEN_FRAMES =
            (long) (Global.DEFAULT_PTS_PER_SECOND * SECONDS_BETWEEN_FRAMES);

    public static void main(String[] args) {

        IMediaReader mediaReader = ToolFactory.makeReader(inputFilename);
        mediaReader.setBufferedImageTypeToGenerate(BufferedImage.TYPE_3BYTE_BGR);
        mediaReader.addListener(new ImageSnapListener());
        while (mediaReader.readPacket() == null);

    }

    private static class ImageSnapListener extends MediaListenerAdapter {
         public void onVideoPicture(IVideoPictureEvent event) {

            if (event.getStreamIndex() != mVideoStreamIndex) {

                if (mVideoStreamIndex == -1) {
                    mVideoStreamIndex = event.getStreamIndex();
                }
                else {
                    return;
                }
            }


            if (mLastPtsWrite == Global.NO_PTS) {
                mLastPtsWrite = event.getTimeStamp() - MICRO_SECONDS_BETWEEN_FRAMES;
            }

            if (event.getTimeStamp() - mLastPtsWrite
                    >= MICRO_SECONDS_BETWEEN_FRAMES) {

                String outputFilename = dumpImageToFile(event.getImage());

                double seconds = ((double) event.getTimeStamp())
                        / Global.DEFAULT_PTS_PER_SECOND;

                System.out.printf(
                        "at elapsed time of %6.3f seconds wrote: %s\n",
                        seconds, outputFilename);

                mLastPtsWrite += MICRO_SECONDS_BETWEEN_FRAMES;

            }
        }

        private String dumpImageToFile(BufferedImage image) {

            try {

                String outputFilename = outputFilePrefix
                        + System.currentTimeMillis() + ".png";

                ImageIO.write(image, "png", new File(outputFilename));

                return outputFilename;

            } catch (IOException e) {

                e.printStackTrace();

                return null;

            }
        }
    }
}

It is very similar to yours, the funniest thing is that picking up your class I started having this mistake of yours... I’m gonna scrub in and see if I can find the biggest difference!

0

Browser other questions tagged

You are not signed in. Login or sign up in order to post.