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

▼魔方 西西 提交于 2019-12-09 11:17:28

问题


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?

Thanks a lot + best regards, andy

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 follwing: 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.

Thanks for your help!


回答1:


you could use jcodec(http://jcodec.org/) 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");
    }
}



回答2:


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


    }
}



回答3:


This link might give you some basic idea about how to do. Try this..

http://krishnabhargav.blogspot.in/2008/02/processing-videos-in-java.html



来源:https://stackoverflow.com/questions/11808815/how-to-get-the-the-single-images-of-an-mp4-movie-in-java

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!