我想对以.mp4格式存储的视频进行一些图像分析。因此,我需要一种方法来用Java获取该电影的图像。我走了很多路,发现了一些库,例如jcodec和jaad。但是我无法让这些库运行这些东西。而且我发现,有一些示例(至少没有找到)显示了我的用例。
您能帮我吗?您是否知道可以满足我的需要并且至少在Win7 64位上运行的任何库。还是您知道如何使用jcodec做到这一点?
编辑:
正如我写的,我用jcodec尝试过。我发现了如何获取帧的数据,但没有找到如何将其获取到诸如BufferedImage之类的东西中。我希望这些数据不是简单的RGB格式,而是任何压缩格式。 (我对吗?)我不知道要解码该数据。
您可以使用jcodec如下获取帧的数据(至少据我了解):
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();
}
我还发现了以下内容:http://code.google.com/p/jcodec/source/browse/trunk/src/test/java/org/jcodec/containers/mp4/DitherTest.java?r=70但这对于可下载的jar包不起作用(有编译错误)。
您可以在followinf程序中使用jcodec(http://jcodec.org/),我正在从视频中提取帧。
/*
* 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");
}
}
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();
}
}
}