Save frames/ images of a video

Asked

Viewed 225 times

3

Hello,

I need to save pictures/frames from a video. The idea is to create a preview of the film before it starts.

The problem is I’m not being able to implement the examples I found.

First example take a screenshot of the component with Javafx, it works but the video has to 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 Librarias seems to be possible but neither with one nor with the other.

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: https://stackoverflow.com/a/22107132/3792998

I’m not wearing Maven:

Libraries added to the project: javacpp javacv

issues:

  • Does anyone know which libraries are needed to put the it works the javaCV?
  • It is necessary to have already installed on Opencv PC or has nothing to do?
  • Some more practical code for what I intend to do?
  • By mistake java.lang.NoClassDefFoundError: com/googlecode/javacpp/Pointer is missing a class/package, compiled via command line? If yes try to use so java -cp nome-do-pacote.jar

  • If not using Maven which one are you using? Gradle, Sbt or command line?

  • no, I haven’t compiled via cmd yet... I’m using Netbeans and entering the libraries directly. But in this case I’m not getting it.

  • But does the project in Netbeans use Maven or Gradle? Why are these libs usually added to the project, or did you not even add?

  • I directly added the libs to the project (without using Maven or Gradle). https://github.com/bytedeco/javacv#manual-installation - I’ve added the ones that say here

  • Check there if added all jars, in case it is the javacpp.jar. Another thing, by the comments of the post you linked, the way to get the BufferedImage is different in more recent versions. Also see if when running the same jars are in classpath.

  • @Dudaskank yes I have the lib javacpp.jar inserted, already updated it by another but the problem continues, the error results when I try to create the FFmpegFrameGrabber

Show 2 more comments

1 answer

1


Example using Xuggle to generate video images and resize them by passing their 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

Browser other questions tagged

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