Save frames / images of a video

3

Hello,

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

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

1st Example to take 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 I can not do this with either one.

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();
        }


    }
}

error:

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: link

I'm not using Maven:

Bookstores added to the project: javacpp javacv

issues:

  • Does anyone know which libraries are needed to por does javaCV work?
  • Is it necessary to have already installed on the OpenCV PC or has nothing to do?
  • Some more practical code for what I want to do?
asked by anonymous 30.11.2017 / 15:54

1 answer

1

Example using Xuggle to generate images of a video and resize them by 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: link

    
08.12.2017 / 16:50