protected function shiftMetaSize() { /* * Injecting calculated keyframes */ $keyframes = new stdClass(); $keyframes->filepositions = $this->filepositions; $keyframes->times = $this->times; $this->metadata->keyframes = $keyframes; $shift = parent::shiftMetaSize(); /* * Shifting filepositions */ foreach ($this->filepositions as $key => $value) { $this->filepositions[$key] = $value + $shift; } /* * Updating keyframes with shifted filepositions */ $keyframes->filepositions = $this->filepositions; $this->metadata->keyframes = $keyframes; return $shift; }
/** * 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 }