Exemple #1
0
 /**
  * @param string $filename
  * @return Yamdi_InputStream
  */
 protected function read($filename)
 {
     $this->sourceFilename = $filename;
     /*
      * File to read 
      */
     $stream = new Yamdi_InputStream($this->sourceFilename);
     /*
      * Constant header
      */
     $this->header->read($stream);
     if (!$this->header->isValid()) {
         throw new Exception('File ' . $this->sourceFilename . ' is not of flv format');
     }
     /*
      * Constant size of zero tag
      */
     $this->zeroTagSize->read($stream);
     // $this->zeroTagSize->size should be zero
     /*
      * First tag (meta)
      */
     $this->metadataTag->read($stream);
     if (!$this->metadataTag->isMeta()) {
         throw new Exception('No metadata tag found if file ' . $this->sourceFilename);
     }
     $tagBodyString = $this->metadataTag->readTagBody($stream);
     $this->metadataTagBodyLenghth = strlen($tagBodyString);
     $this->metadata->read($tagBodyString);
     /*
      * Previous tag size
      */
     $this->metadataTagSize->read($stream);
     $this->sourceMediaTagsStartPosition = $stream->getPosition();
     return $stream;
 }
 /**
  * Decomposes a file into flv tags while reading. Searches for keyframes
  * 
  * @param string $filename
  * @return Yamdi_InputStream
  */
 protected function read($filename)
 {
     $stream = parent::read($filename);
     $this->filepositions = array();
     $this->times = array();
     $tag = new Yamdi_FlvTag();
     while (!$stream->isEnd()) {
         $tag_position = $stream->getPosition();
         /*
          * Tag
          */
         if (!$tag->read($stream)) {
             break;
         }
         if (!$tag->isValid()) {
             throw new Exception('Invalid tag found');
         }
         if ($tag->isVideo()) {
             if ($tag->checkIfKeyFrame($stream)) {
                 $this->filepositions[] = $tag_position;
                 $this->times[] = $tag->getTimestamp() / 1000.0;
             }
         }
         $tag->skipTagBody($stream);
         // wind forward to next tag
         /*
          * Previous tag size
          */
         $tagSize = new Yamdi_FlvTagSize();
         if (!$tagSize->read($stream)) {
             break;
         }
     }
     return null;
     //nothing else to read
 }
 /**
  * Decomposes a file into flv tags while reading. Searches for given keyframe
  * 
  * @param string $filename
  * @return Yamdi_InputStream
  */
 protected function read($filename)
 {
     $stream = parent::read($filename);
     /*
      * Looking for needed keyframe video tag
      */
     if ($this->metadata->keyframes !== null) {
         /*
          * Keyframes present in meta, getting position of needed tag
          */
         $filepositions = $this->metadata->keyframes->filepositions;
         if (isset($filepositions[$this->desiredKeyframeNumber])) {
             $this->videoTagPosition = $filepositions[$this->desiredKeyframeNumber];
         } else {
             $this->videoTagPosition = $filepositions[0];
         }
         /*
          * Reading tag to fetch block size
          */
         $stream->seek($this->videoTagPosition, SEEK_SET);
         $tag = new Yamdi_FlvTag();
         if (!$tag->read($stream)) {
             throw new Exception('Invalid tag fileposition');
         }
         if (!$tag->isValid()) {
             throw new Exception('Invalid tag found');
         }
         if (!$tag->isVideo()) {
             throw new Exception('Specified tag is not video');
         }
         if (!$tag->checkIfKeyFrame($stream)) {
             throw new Exception('Specified tag is not a keyframe');
         }
         $this->videoBlockSize = $tag->getDataSize() + 4 + 11;
     } else {
         /*
          * Keyframes absent in meta, reading tags until needed keyframe is found
          */
         $tag = new Yamdi_FlvTag();
         $keyframe_number = 0;
         while (!$stream->isEnd()) {
             $tag_position = $stream->getPosition();
             /*
              * Tag
              */
             if (!$tag->read($stream)) {
                 break;
             }
             if (!$tag->isValid()) {
                 throw new Exception('Invalid tag found');
             }
             if ($tag->isVideo()) {
                 if ($tag->checkIfKeyFrame($stream)) {
                     if ($this->videoTagPosition === null) {
                         // initializing with first tag position and size
                         $this->videoTagPosition = $tag_position;
                         $this->videoBlockSize = $tag->getDataSize() + 4 + 11;
                     }
                     if (++$keyframe_number == $this->desiredKeyframeNumber) {
                         $this->videoTagPosition = $tag_position;
                         //found keyframe
                         $this->videoBlockSize = $tag->getDataSize() + 4 + 11;
                         break;
                     }
                 }
             }
             $tag->skipTagBody($stream);
             // wind forward to next tag
             /*
              * Previous tag size
              */
             $tagSize = new Yamdi_FlvTagSize();
             if (!$tagSize->read($stream)) {
                 break;
             }
         }
     }
     return null;
     //nothing else to read
 }
 /**
  * @deprecated 
  */
 public function testMetadataWriteback()
 {
     /*
      * File to read 
      */
     $stream = new Yamdi_InputStream(dirname(__FILE__) . '/../trailer_2.flv');
     /*
      * Constant header
      */
     $header = new Yamdi_FlvFileHeader();
     $header->read($stream);
     $this->assertEqual(true, $header->isValid());
     /*
      * Constant size of zero tag
      */
     $zeroTagSize = new Yamdi_FlvTagSize();
     $zeroTagSize->read($stream);
     $this->assertEqual(0, $zeroTagSize->size);
     // allways 0
     /*
      * First tag (should be meta)
      */
     $tag = new Yamdi_FlvTag();
     $tag->read($stream);
     $this->assertTrue($tag->isMeta());
     $metadata = new Yamdi_FlvMetadataBody();
     $tagBody = $tag->readTagBody($stream);
     $metadata->read($tagBody);
     $this->assertEqual(strlen($tagBody), strlen($metadata->write()));
     /*
      * Cleanup
      */
     $stream->close();
 }