0

I stored videos in hard disk and stored their link in database, as I find this as a solution to stored video in database. Now I need retrieve thumbnail image of each video.so how can I get thumbnail image of video (MJPEG format) using ffmpeg in java..

I don't know maybe I cannot find the right keywords.

PS: I am using Eclipse IDE and implement required interface in Java Swing

Rüdiger Herrmann
  • 20,512
  • 11
  • 62
  • 79
Shaista
  • 147
  • 1
  • 5
  • 15

2 Answers2

0

You can use xuggle which it is easy to use and has samples to do so.

However, it is deprecated. You may like to check humble video instead.

I have used xuggle and it supports a huge list of video formats.

It works on major operation systems without installing anything.

Search google for it.

Soley
  • 1,716
  • 1
  • 19
  • 33
0

Here you can find the library and sample codes. However, it depends on you on which frame you want to take the thumbnail.

Play with this code to find your needs:

/**
 *
 * @author Pasban
 */
import com.xuggle.xuggler.ICodec;
import com.xuggle.xuggler.IContainer;
import com.xuggle.xuggler.IPacket;
import com.xuggle.xuggler.IStream;
import com.xuggle.xuggler.IStreamCoder;
import com.xuggle.xuggler.IVideoPicture;
import com.xuggle.xuggler.video.ConverterFactory;
import com.xuggle.xuggler.video.IConverter;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;

public class VideoThumbnail extends JDialog {

    private Image image;

    public static void main(String[] args) {
        final VideoThumbnail merge = new VideoThumbnail();
        Thread thread = new Thread() {

            @Override
            public void run() {
                merge.perform("y:/a.mp4", "y:/thumb.a.png");
                merge.setVisible(false);
                System.exit(0);
            }
        };
        thread.run();
    }

    public void perform(String path_video, String filename) {

        IContainer container = IContainer.make();

        // check if file is readable
        if (container.open(path_video, IContainer.Type.READ, null) < 0) {
            throw new IllegalArgumentException("Can't find " + path_video);
        }

        // read video file and create stream



        double fRate = 0;
        IStreamCoder coderVideo = null;

        for (int i = 0; i < container.getNumStreams(); i++) {
            IStream stream = container.getStream(i);
            IStreamCoder coder = stream.getStreamCoder();
            if (coder.getCodecType() == ICodec.Type.CODEC_TYPE_VIDEO) {
                coderVideo = coder;
                fRate = coderVideo.getFrameRate().getDouble();
                break;
            }
        }


        if (coderVideo == null || coderVideo.open(null, null) < 0) {
            throw new RuntimeException("Can't open video coder");
        }

        int width = coderVideo.getWidth();
        int height = coderVideo.getHeight();

        this.setSize(width, height);
        this.setLocationRelativeTo(null);
        this.setVisible(true);

        IPacket packet = IPacket.make();

        int after_this_frame = 60; // or  60 * fRate to have your time in Seconds (here it will be 60*fRate = 1mins)
        while (container.readNextPacket(packet) >= 0) {

            // video packet
            IVideoPicture picture = IVideoPicture.make(coderVideo.getPixelType(), width, height);
            coderVideo.decodeVideo(picture, packet, 0);
            if (picture.isComplete()) {
                IConverter converter = ConverterFactory.createConverter(ConverterFactory.XUGGLER_BGR_24, picture);
                BufferedImage bi = converter.toImage(picture);
                try {
                    after_this_frame--;
                    if (after_this_frame == 0) {
                        ImageIO.write(bi, "png", new File(filename));
                        break;
                    }
                } catch (IOException ex) {
                    ex.printStackTrace();
                }

                this.setImage(bi);
                // break from this loop if you are happy with the first frame.
                //break;
            }

        }

        coderVideo.close();
        container.close();

    }

    public VideoThumbnail() {
        this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    }

    public void setImage(final Image image) {
        SwingUtilities.invokeLater(new Runnable() {

            public void run() {
                VideoThumbnail.this.image = image;
                repaint();
            }
        });
    }

    @Override
    public synchronized void paint(Graphics g) {
        if (image != null) {
            g.drawImage(image, 0, 0, null);
        }
    }
}

For more samples from Xuggle,

https://github.com/artclarke/xuggle-xuggler/tree/master/src/com/xuggle/mediatool/demos

Soley
  • 1,716
  • 1
  • 19
  • 33
  • xuggle download is not working. Can you suggest any link to download? – Arunkumar S Aug 31 '16 at 04:18
  • 2
    I uploaded my old jar file into this GoogleDrive public folder. ** https://drive.google.com/folderview?id=0BzPg_acW3VmsYTZWbGcxdjdSdmc&usp=sharing ** if I broke any copyright law by this, please let me know to remove it so I will be safe from being sued :P – Soley Sep 01 '16 at 15:53