如何在java中获取缩略图像(格式MJPEG)

时间:2015-03-19 10:50:32

标签: java video ffmpeg

我将视频存储在硬盘中并将其链接存储在数据库中,因为我发现这是在数据库中存储视频的解决方案。 现在我需要检索每个视频的缩略图。如何在java中使用ffmpeg获取视频的缩略图(MJPEG格式)..

我不知道也许找不到合适的关键字。

PS:我正在使用Eclipse IDE并在Java Swing中实现所需的接口

2 个答案:

答案 0 :(得分:0)

您可以使用易于使用的xuggle,并提供样本。

但是,它已被弃用。你可能想要查看不起眼的视频。

我使用过xuggle,它支持大量的视频格式。

它适用于主要的操作系统而无需安装任何东西。

在谷歌搜索它。

答案 1 :(得分:0)

您可以在此处找到库和示例代码。但是,这取决于您想要拍摄缩略图的帧。

使用此代码查找您的需求:

/**
 *
 * @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);
        }
    }
}

来自Xuggle的更多样本,

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