Package com.xuggle.xuggler

Examples of com.xuggle.xuggler.IContainer


      throw new RuntimeException("you must install the GPL version" +
          " of Xuggler (with IVideoResampler support) for " +
          "this demo to work");

    // Create a Xuggler container object
    IContainer container = IContainer.make();

    // Open up the container
    if (container.open(filename, IContainer.Type.READ, null) < 0)
      throw new IllegalArgumentException("could not open file: " + filename);

    // query how many streams the call to open found
    int numStreams = container.getNumStreams();

    // and iterate through the streams to find the first video stream
    int videoStreamId = -1;
    IStreamCoder videoCoder = null;
    for(int i = 0; i < numStreams; i++)
    {
      // Find the stream object
      IStream stream = container.getStream(i);
      // Get the pre-configured decoder that can decode this stream;
      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: "
          +filename);

    /*
     * Now we have found the video stream in this file.  Let's open up our decoder so it can
     * do work.
     */
    if (videoCoder.open() < 0)
      throw new RuntimeException("could not open video decoder for container: "
          +filename);

    IVideoResampler resampler = null;
    if (videoCoder.getPixelType() != IPixelFormat.Type.BGR24)
    {
      // if this stream is not in BGR24, we're going to need to
      // convert it.  The VideoResampler does that for us.
      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: " + filename);
    }
    /*
     * And once we have that, we draw a window on screen
     */
    openJavaWindow();

    /*
     * Now, we start walking through the container looking at each packet.
     */
    IPacket packet = IPacket.make();
    long firstTimestampInStream = Global.NO_PTS;
    long systemClockStartTime = 0;
    while(container.readNextPacket(packet) >= 0)
    {
      /*
       * Now we have a packet, let's see if it belongs to our video stream
       */
      if (packet.getStreamIndex() == videoStreamId)
      {
        /*
         * We allocate a new picture to get the data out of Xuggler
         */
        IVideoPicture picture = IVideoPicture.make(videoCoder.getPixelType(),
            videoCoder.getWidth(), videoCoder.getHeight());

        int offset = 0;
        while(offset < packet.getSize())
        {
          /*
           * Now, we decode the video, checking for any errors.
           *
           */
          int bytesDecoded = videoCoder.decodeVideo(picture, packet, offset);
          if (bytesDecoded < 0)
            throw new RuntimeException("got error decoding video in: "
                + filename);
          offset += bytesDecoded;

          /*
           * Some decoders will consume data in a packet, but will not be able to construct
           * a full video picture yet.  Therefore you should always check if you
           * got a complete picture from the decoder
           */
          if (picture.isComplete())
          {
            IVideoPicture newPic = picture;
            /*
             * If the resampler is not null, that means we didn't get the
             * video in BGR24 format and
             * need to convert it into BGR24 format.
             */
            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: "
                    + filename);
            }
            if (newPic.getPixelType() != IPixelFormat.Type.BGR24)
              throw new RuntimeException("could not decode video" +
                  " as BGR 24 bit data in: " + filename);

            /**
             * We could just display the images as quickly as we decode them,
             * but it turns out we can decode a lot faster than you think.
             *
             * So instead, the following code does a poor-man's version of
             * trying to match up the frame-rate requested for each
             * IVideoPicture with the system clock time on your computer.
             *
             * Remember that all Xuggler IAudioSamples and IVideoPicture objects
             * always give timestamps in Microseconds, relative to the first
             * decoded item. If instead you used the packet timestamps, they can
             * be in different units depending on your IContainer, and IStream
             * and things can get hairy quickly.
             */
            if (firstTimestampInStream == Global.NO_PTS)
            {
              // This is our first time through
              firstTimestampInStream = picture.getTimeStamp();
              // get the starting clock time so we can hold up frames
              // until the right time.
              systemClockStartTime = System.currentTimeMillis();
            } else {
              long systemClockCurrentTime = System.currentTimeMillis();
              long millisecondsClockTimeSinceStartofVideo =
                systemClockCurrentTime - systemClockStartTime;
              // compute how long for this frame since the first frame in the
              // stream.
              // remember that IVideoPicture and IAudioSamples timestamps are
              // always in MICROSECONDS,
              // so we divide by 1000 to get milliseconds.
              long millisecondsStreamTimeSinceStartOfVideo =
                (picture.getTimeStamp() - firstTimestampInStream)/1000;
              final long millisecondsTolerance = 50; // and we give ourselfs 50 ms of tolerance
              final long millisecondsToSleep =
                (millisecondsStreamTimeSinceStartOfVideo -
                  (millisecondsClockTimeSinceStartofVideo +
                      millisecondsTolerance));
              if (millisecondsToSleep > 0)
              {
                try
                {
                  Thread.sleep(millisecondsToSleep);
                }
                catch (InterruptedException e)
                {
                  // we might get this when the user closes the dialog box, so
                  // just return from the method.
                  return;
                }
              }
            }

            // And finally, convert the BGR24 to an Java buffered image
            BufferedImage javaImage = Utils.videoPictureToImage(newPic);

            // and display it on the Java Swing window
            updateJavaWindow(javaImage);
          }
        }
      }
      else
      {
        /*
         * This packet isn't part of our video stream, so we just
         * silently drop it.
         */
        do {} while(false);
      }

    }
    /*
     * Technically since we're exiting anyway, these will be cleaned up by
     * the garbage collector... but because we're nice people and want
     * to be invited places for Christmas, we're going to show how to clean up.
     */
    if (videoCoder != null)
    {
      videoCoder.close();
      videoCoder = null;
    }
    if (container !=null)
    {
      container.close();
      container = null;
    }
    closeJavaWindow();

  }
View Full Code Here


    if (args.length <= 0)
      throw new IllegalArgumentException("must pass in a filename as the first argument");
   
    String filename = args[0];
    // Create a Xuggler container object
    IContainer container = IContainer.make();
   
    // Open up the container
    if (container.open(filename, IContainer.Type.READ, null) < 0)
      throw new IllegalArgumentException("could not open file: " + filename);
   
    // query how many streams the call to open found
    int numStreams = container.getNumStreams();
    System.out.printf("file \"%s\": %d stream%s; ",
        filename,
        numStreams,
        numStreams == 1 ? "" : "s");
    System.out.printf("duration (ms): %s; ", container.getDuration() == Global.NO_PTS ? "unknown" : "" + container.getDuration()/1000);
    System.out.printf("start time (ms): %s; ", container.getStartTime() == Global.NO_PTS ? "unknown" : "" + container.getStartTime()/1000);
    System.out.printf("file size (bytes): %d; ", container.getFileSize());
    System.out.printf("bit rate: %d; ", container.getBitRate());
    System.out.printf("\n");

    // and iterate through the streams to print their meta data
    for(int i = 0; i < numStreams; i++)
    {
      // Find the stream object
      IStream stream = container.getStream(i);
      // Get the pre-configured decoder that can decode this stream;
      IStreamCoder coder = stream.getStreamCoder();
     
      // and now print out the meta data.
      System.out.printf("stream %d: ",    i);
      System.out.printf("type: %s; ",     coder.getCodecType());
      System.out.printf("codec: %s; ",    coder.getCodecID());
      System.out.printf("duration: %s; ", stream.getDuration() == Global.NO_PTS ? "unknown" : "" + stream.getDuration());
      System.out.printf("start time: %s; ", container.getStartTime() == Global.NO_PTS ? "unknown" : "" + stream.getStartTime());
      System.out.printf("language: %s; ", stream.getLanguage() == null ? "unknown" : stream.getLanguage());
      System.out.printf("timebase: %d/%d; ", stream.getTimeBase().getNumerator(), stream.getTimeBase().getDenominator());
      System.out.printf("coder tb: %d/%d; ", coder.getTimeBase().getNumerator(), coder.getTimeBase().getDenominator());
     
      if (coder.getCodecType() == ICodec.Type.CODEC_TYPE_AUDIO)
View Full Code Here

  public DisplayWebcamVideo(String driverName, String deviceName)
  {
    // create a Xuggler container object

    IContainer container = IContainer.make();

    // tell Xuggler about the device format

    IContainerFormat format = IContainerFormat.make();
    if (format.setInputFormat(driverName) < 0)
      throw new IllegalArgumentException(
        "couldn't open webcam device: " + driverName);
   
    // devices, unlike most files, need to have parameters set in order
    // for Xuggler to know how to configure them, for a webcam, these
    // parameters make sense

    IMetaData params = IMetaData.make();
   
    params.setValue("framerate", "30/1");
    params.setValue("video_size", "320x240");
   
    // open the container

    int retval = container.open(deviceName, IContainer.Type.READ, format,
        false, true, params, null);
    if (retval < 0)
    {
      // this little trick converts the non friendly integer return
      // value into a slightly more friendly object to get a
View Full Code Here

TOP

Related Classes of com.xuggle.xuggler.IContainer

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.