java – Save frames/images from a video

Question:

Hi,

I need to save images/frames of a video. The idea is to create a preview of the movie before it starts.

The problem is that I'm not able to implement the examples I found.

1st example taking a screenshot of the component with JavaFX, it works but the video must be playing.

Code:

...
    // 
    private void saveAsPng(Node n) {
        WritableImage image = n.snapshot(new SnapshotParameters(), null);
        // TODO: probably use a file chooser here
        File file = new File("D:\\movie12.png");

        try {
            ImageIO.write(SwingFXUtils.fromFXImage(image, null), "png", file);
        } catch (IOException e) {
            System.out.println("Não foi possível capturar imagem do video");
        }
    }
...

2º Making use of the JavaCV or Xuggler Libraries seems to be possible but neither with one nor the other I am able to.

JavaCV

import com.googlecode.javacv.FFmpegFrameGrabber;
import com.googlecode.javacv.cpp.opencv_core.IplImage;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;

public class ZzzzzzzSaveImgFromVideo{
    public static void main(String []args) throws IOException, Exception
    {
        FFmpegFrameGrabber frameGrabber = new FFmpegFrameGrabber("D:\\Movie..................mp4");
        frameGrabber.start();
        IplImage i;
        try {

            i = frameGrabber.grab();
            BufferedImage  bi = i.getBufferedImage();
            ImageIO.write(bi,"png", new File("D:/Img.png"));
            frameGrabber.stop();
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }


    }
}

mistake:

run:
Exception in thread "main" java.lang.NoClassDefFoundError: com/googlecode/javacpp/Pointer
    at java.lang.ClassLoader.defineClass1(Native Method)
    at java.lang.ClassLoader.defineClass(ClassLoader.java:763)
    at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
    at java.net.URLClassLoader.defineClass(URLClassLoader.java:467)
    at java.net.URLClassLoader.access$100(URLClassLoader.java:73)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:368)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:362)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:361)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:335)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
    at zzzzzzzsaveimgfromvideo.ZzzzzzzSaveImgFromVideo.main(ZzzzzzzSaveImgFromVideo.java:18)
Caused by: java.lang.ClassNotFoundException: com.googlecode.javacpp.Pointer
    at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:335)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
    ... 13 more

link: https://stackoverflow.com/a/22107132/3792998

I'm not using Maven:

Bookstores added to the project: javacpp javacv

questions:

  • Does anyone know what libraries are needed to make javaCV work?
  • Is it necessary to have already installed OpenCV on PC or it has nothing to do with it?
  • Any more practical code for what I intend to do?

Answer:

Example using Xuggle to generate images of a video and resize them passing its path and the interval of seconds between frames:

public static final double SECONDS_BETWEEN_FRAMES = 1;
private static final int IMG_WIDTH = 300;
private static final int IMG_HEIGHT = 225;

String fileName = "video_path";

IMediaReader mediaReader = null;
mediaReader = ToolFactory.makeReader(fileName);
mediaReader.setBufferedImageTypeToGenerate(BufferedImage.TYPE_3BYTE_BGR);
mediaReader.addListener(new ImageSnapListener());
while (mediaReader.readPacket() == null)
   do {} while(false);



/**
 * Classe auxiliar para gerar imagem do arquivo vídeo
 * de upload
 * @author allan-braga
 *
 */
private  class ImageSnapListener extends MediaListenerAdapter{
    @Override
    public void onVideoPicture(IVideoPictureEvent event) {
            String outputFilename = dumpImageToFile(event.getImage());
        }
    }
    private String dumpImageToFile(BufferedImage image) {
        try {


            String outputFilename =  "PATH_AND_NAME" + ".png";
            ImageIO.write(image, "png", new File(outputFilename));

            //Redimensiona a imagem 
            BufferedImage originalImage = ImageIO.read(new File(outputFilename));
            int type = originalImage.getType() == 0? BufferedImage.TYPE_INT_ARGB : originalImage.getType();

            BufferedImage resizeImagePng = resizeImage(originalImage, type);
            ImageIO.write(resizeImagePng, "png", new File(outputFilename));

            return outputFilename;
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }
}

private static BufferedImage resizeImage(BufferedImage originalImage, int type){
    BufferedImage resizedImage = new BufferedImage(IMG_WIDTH, IMG_HEIGHT, type);
    Graphics2D g = resizedImage.createGraphics();
    g.drawImage(originalImage, 0, 0, IMG_WIDTH, IMG_HEIGHT, null);
    g.dispose();
    return resizedImage;
 }

Download:
http://www.xuggle.com/xuggler

Scroll to Top