Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the single images of an mp4-Movie in Java

Tags:

java

decode

mp4

I want to do some image analysis on a video that's stored in .mp4 format. Therefore I need a way to just get the images of this movie in Java. I goolged a lot and found some libraries like jcodec and jaad. BUT I wasn't able to get the things running with these libraries. And as I found out, there were examples (at least I found none) that showed my usecase.

Can you help me? Do you know any library that can do what I need and is running at least on Win7 64 bit. Or do you know how to accomplish this with jcodec?

edit:

As I wrote, I tried it with jcodec. I found out how to get the data of a frame, but not how I can get it into something like a BufferedImage or so. I expect that these data isn't in a simple RGB format but in any compressed format or so. (Am I right with that?) I don't know to to decode this data.

You can get the data of a frame with jcodec as follows (at least as far as I understand this):

public static void main(String[] args) throws IOException, MP4DemuxerException {
    String path = "videos/video-2011-09-21-20-07-21.mp4";

    MP4Demuxer demuxer1 = new MP4Demuxer(new FileInput(new File(path)));
    DemuxerTrack videoTrack = demuxer1.getVideoTrack();

    Packet firstFrame = videoTrack.getFrames(1);
    byte[] data = firstFrame.getData();
}

I also found the following: http://code.google.com/p/jcodec/source/browse/trunk/src/test/java/org/jcodec/containers/mp4/DitherTest.java?r=70 But this isn't working (has compile errors) with the downloadable jar-package.

like image 218
andy Avatar asked Aug 04 '12 12:08

andy


2 Answers

you could use jcodec(https://github.com/jcodec/jcodec) in the followinf program i am extracting frames from a video.

/*
 * To extract frames from a mp4(avc) video
 * 
 */
package avc_frame;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import org.jcodec.api.FrameGrab;
import org.jcodec.api.JCodecException;

public class Avc_frame {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) throws IOException, JCodecException {
  
        long time = System.currentTimeMillis();
        for (int i = 50; i < 57; i++) { 
            BufferedImage frame = FrameGrab.getFrame(new File("/Users/jovi/Movies/test.mp4"), i);
            ImageIO.write(frame, "bmp", new File("/Users/jovi/Desktop/frames/frame_"+i+".bmp"));
        }
        System.out.println("Time Used:" + (System.currentTimeMillis() - time)+" Milliseconds");
    }
}
like image 165
Jovi Dsilva Avatar answered Sep 26 '22 13:09

Jovi Dsilva


import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import org.bytedeco.javacpp.opencv_core.IplImage;
import org.bytedeco.javacv.FFmpegFrameGrabber;
import org.bytedeco.javacv.FrameGrabber.Exception;

public class Read{
    public static void main(String []args) throws IOException, Exception
    {
        FFmpegFrameGrabber frameGrabber = new FFmpegFrameGrabber("C:/Users/Digilog/Downloads/Test.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();
        }


    }
}
like image 22
kousik Mridha Avatar answered Sep 26 '22 13:09

kousik Mridha