如何从视频中获取帧样本 (jpeg) (mov)

2022-09-01 15:01:09

我想用java从视频文件(mov)中获取帧样本(jpeg)。有没有一个简单的方法来做到这一点。当我在谷歌中搜索时,我所能找到的就是从多个jpgs制作mov。我不知道也许我找不到正确的关键字。


答案 1

我知道最初的问题已经解决,但是,我正在发布这个答案,以防其他人像我一样陷入困境。

从昨天开始,我尝试了一切,我的意思是一切来做到这一点。所有可用的Java库要么已经过时,要么不再维护,要么缺乏任何类型的可用文档(认真??!?!)

我尝试了JFM(旧的和无用的),JCodec(没有任何文档),JJMpeg(看起来很有前途,但由于缺乏Java类文档,使用起来非常困难和麻烦),OpenCV自动Java构建和一些我不记得的其他库。

最后,我决定看看JavaCV的(Github链接)类,瞧!它包含带有详细文档的 FFMPEG 绑定。

<dependency>
    <groupId>org.bytedeco</groupId>
    <artifactId>javacv</artifactId>
    <version>1.0</version>
</dependency>

事实证明,有一种非常简单的方法可以将视频帧从视频文件中提取到JPEG文件,并通过扩展名JPEG文件。类 FFmpegFrameGrabber 可以很容易地用于抓取单个帧并将其转换为 .代码示例如下:BufferedImageBufferedImage

FFmpegFrameGrabber g = new FFmpegFrameGrabber("textures/video/anim.mp4");
g.start();

Java2DFrameConverter converter = new Java2DFrameConverter();

for (int i = 0 ; i < 50 ; i++) {
    
    Frame frame = g.grabImage(); // It is important to use grabImage() to get a frame that can be turned into a BufferedImage

    BufferedImage bi = converter.convert(frame);

    ImageIO.write(bi, "png", new File("frame-dump/video-frame-" + System.currentTimeMillis() + ".png"));
}

g.stop();

基本上,此代码转储视频的前 50 帧,并将其另存为 PNG 文件。好消息是内部 seek 函数适用于实际帧而不是关键帧(我在 JCodec 上遇到了这个问题)

您可以参考JavaCV的主页,以了解有关可用于从网络摄像头等捕获帧的其他类的更多信息。希望这个答案有帮助:-)


答案 2

Xuggler完成了这项工作。他们甚至给出了一个示例代码,完全符合我的需求。链接如下

http://xuggle.googlecode.com/svn/trunk/java/xuggle-xuggler/src/com/xuggle/mediatool/demos/DecodeAndCaptureFrames.java

我已经修改了这个链接中的代码,使它只保存视频的第一帧。

import javax.imageio.ImageIO;

import java.io.File;

import java.awt.image.BufferedImage;

import com.xuggle.mediatool.IMediaReader;
import com.xuggle.mediatool.MediaListenerAdapter;
import com.xuggle.mediatool.ToolFactory;
import com.xuggle.mediatool.event.IVideoPictureEvent;
import com.xuggle.xuggler.Global;

/**
 *  * @author aclarke
 *    @author trebor
 */

public class DecodeAndCaptureFrames extends MediaListenerAdapter
{
  private int mVideoStreamIndex = -1;
  private boolean gotFirst = false;
  private String saveFile;
  private Exception e;
  /** Construct a DecodeAndCaptureFrames which reads and captures
   * frames from a video file.
   * 
   * @param filename the name of the media file to read
   */

  public DecodeAndCaptureFrames(String videoFile, String saveFile)throws Exception
  {
    // create a media reader for processing video
    this.saveFile = saveFile;
    this.e = null;
     IMediaReader reader = ToolFactory.makeReader(videoFile);

    // stipulate that we want BufferedImages created in BGR 24bit color space
    reader.setBufferedImageTypeToGenerate(BufferedImage.TYPE_3BYTE_BGR);


    // note that DecodeAndCaptureFrames is derived from
    // MediaReader.ListenerAdapter and thus may be added as a listener
    // to the MediaReader. DecodeAndCaptureFrames implements
    // onVideoPicture().

    reader.addListener(this);

    // read out the contents of the media file, note that nothing else
    // happens here.  action happens in the onVideoPicture() method
    // which is called when complete video pictures are extracted from
    // the media source

      while (reader.readPacket() == null && !gotFirst);

      if (e != null)
          throw e;
  }



  /** 
   * Called after a video frame has been decoded from a media stream.
   * Optionally a BufferedImage version of the frame may be passed
   * if the calling {@link IMediaReader} instance was configured to
   * create BufferedImages.
   * 
   * This method blocks, so return quickly.
   */

  public void onVideoPicture(IVideoPictureEvent event)
  {
    try
    {
      // if the stream index does not match the selected stream index,
      // then have a closer look

      if (event.getStreamIndex() != mVideoStreamIndex)
      {
        // if the selected video stream id is not yet set, go ahead an
        // select this lucky video stream

        if (-1 == mVideoStreamIndex)
          mVideoStreamIndex = event.getStreamIndex();

        // otherwise return, no need to show frames from this video stream

        else
          return;
      }

      ImageIO.write(event.getImage(), "jpg", new File(saveFile));
      gotFirst = true;

    }
    catch (Exception e)
    {
      this.e = e;
    }
  }
}