从xuggler的视频中提取帧

时间:2016-05-03 11:20:51

标签: java xuggler

我希望xuggler 5.4中的图像来自视频,但是我在java.I做了IOStream的帮助但是我的代码卡在了container.open()方法我的IDE(Net Beans 7.2)显示代码运行但是等了很久之后同样的事情它表明。 Plz验证我的代码并纠正我。

public class DecodeAndCaptureFrames
{
  public static final double SECONDS_BETWEEN_FRAMES = 5;
  public static final long NANO_SECONDS_BETWEEN_FRAMES = 
    (long)(Global.DEFAULT_PTS_PER_SECOND * SECONDS_BETWEEN_FRAMES);

  private static long mLastPtsWrite = Global.NO_PTS;

  private static void processFrame(IVideoPicture picture, BufferedImage image)
  {
    try
    {

      if (mLastPtsWrite == Global.NO_PTS)
        mLastPtsWrite = picture.getPts() - NANO_SECONDS_BETWEEN_FRAMES;
      if (picture.getPts() - mLastPtsWrite >= NANO_SECONDS_BETWEEN_FRAMES)
      {

        File file = File.createTempFile("frame", ".png");

        ImageIO.write(image, "png", file);
        double seconds = ((double)picture.getPts()) / Global.DEFAULT_PTS_PER_SECOND;
        System.out.printf("at elapsed time of %6.3f seconds wrote: %s\n",
          seconds, file);        
        mLastPtsWrite += NANO_SECONDS_BETWEEN_FRAMES;
      }
    }
    catch (Exception e)
    {
      e.printStackTrace();
    }
  }

  @SuppressWarnings("deprecation")
  public static void main(String[] args) throws FileNotFoundException
  {
      File f = new File("/root/Downloads/15465.mp4");
       InputStream input = new FileInputStream(f);
        System.out.println(input);
    if (!IVideoResampler.isSupported(
        IVideoResampler.Feature.FEATURE_COLORSPACECONVERSION))
      throw new RuntimeException(
        "you must install the GPL version of Xuggler (with IVideoResampler" + 
        " support) for this demo to work");
    IContainer container = IContainer.make();
    if (container.open(input, null) < 0)
      throw new IllegalArgumentException("could not open file: ");
    int numStreams = container.getNumStreams();    
      System.out.println(numStreams);

    int videoStreamId = -1;
    IStreamCoder videoCoder = null;
    for(int i = 0; i < numStreams; i++)
    {
      IStream stream = container.getStream(i);

      IStreamCoder coder = stream.getStreamCoder();

      if (coder.getCodecType() == ICodec.Type.CODEC_TYPE_VIDEO)
      {
        videoStreamId = i;
        videoCoder = coder;
        break;
      }
    }

    if (videoStreamId == -1)
      throw new RuntimeException("could not find video stream in container: ");

    if (videoCoder.open() < 0)
      throw new RuntimeException(
        "could not open video decoder for container: " );

    IVideoResampler resampler = null;
    if (videoCoder.getPixelType() != IPixelFormat.Type.BGR24)
    {
      resampler = IVideoResampler.make(
        videoCoder.getWidth(), videoCoder.getHeight(), IPixelFormat.Type.BGR24,
        videoCoder.getWidth(), videoCoder.getHeight(), videoCoder.getPixelType());
      if (resampler == null)
        throw new RuntimeException(
          "could not create color space resampler for: " );
    }


    IPacket packet = IPacket.make();
    while(container.readNextPacket(packet) >= 0)
    {

      if (packet.getStreamIndex() == videoStreamId)
      {
        IVideoPicture picture = IVideoPicture.make(videoCoder.getPixelType(),
            videoCoder.getWidth(), videoCoder.getHeight());

        int offset = 0;
        while(offset < packet.getSize())
        {
        int bytesDecoded = videoCoder.decodeVideo(picture, packet, offset);
          if (bytesDecoded < 0)
            throw new RuntimeException("got error decoding video in: " );
          offset += bytesDecoded;

          if (picture.isComplete())
          {
            IVideoPicture newPic = picture;

            if (resampler != null)
            {
              // we must resample
              newPic = IVideoPicture.make(
                resampler.getOutputPixelFormat(), picture.getWidth(), 
                picture.getHeight());
              if (resampler.resample(newPic, picture) < 0)
                throw new RuntimeException(
                  "could not resample video from: " );
            }

            if (newPic.getPixelType() != IPixelFormat.Type.BGR24)
              throw new RuntimeException(
                "could not decode video as BGR 24 bit data in: " );
            BufferedImage javaImage = Utils.videoPictureToImage(newPic);
            processFrame(newPic, javaImage);
          }
        }
      }
      else
      {
        // This packet isn't part of our video stream, so we just
        // silently drop it.
        do {} while(false);
      }
    }

    if (videoCoder != null)
    {
      videoCoder.close();
      videoCoder = null;
    }
    if (container !=null)
    {
      container.close();
      container = null;
    }
  }
}

0 个答案:

没有答案