如何在 Java 中获取 mp4 电影的单个图像
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11808815/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get the single images of an mp4-Movie in Java
提问by andy
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.
我想对以 .mp4 格式存储的视频进行一些图像分析。因此,我需要一种方法来用 Java 获取这部电影的图像。我搜索了很多,找到了一些库,比如 jcodec 和 jaad。但是我无法让这些库运行起来。正如我发现的那样,有一些示例(至少我没有发现)展示了我的用例。
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?
你能帮助我吗?你知道任何可以做我需要的并且至少在 Win7 64 位上运行的库吗?或者您知道如何使用 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.
在我写的时候,我用 jcodec 试了一下。我发现了如何获取帧的数据,但没有发现如何将其放入 BufferedImage 之类的东西中。我希望这些数据不是简单的 RGB 格式,而是任何压缩格式。(我这样做对吗?)我不知道解码这些数据。
You can get the data of a frame with jcodec as follows (at least as far as I understand this):
您可以使用 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();
}
I also found the following: http://code.google.com/p/jcodec/source/browse/trunk/src/test/java/org/jcodec/containers/mp4/DitherTest.java?r=70But this isn't working (has compile errors) with the downloadable jar-package.
我还发现了以下内容:http: //code.google.com/p/jcodec/source/browse/trunk/src/test/java/org/jcodec/containers/mp4/DitherTest.java?r=70 但这不是与可下载的 jar 包一起工作(有编译错误)。
回答by Jovi Dsilva
you could use jcodec(http://jcodec.org/) in the followinf program i am extracting frames from a video.
您可以在我正在从视频中提取帧的 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");
}
}
回答by kousik Mridha
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();
}
}
}