public static function fixFlvTimestamps($srcName, $outName) { $flv_wrapper = new myFlvHandler($srcName); $header = $flv_wrapper->getHeader(); $fh = fopen($outName, "wb"); fwrite($fh, $header); $aTS = 0; $aInsaneTS = null; $vTS = 0; $vInsaneTS = null; while (true) { list($type, $size, $timestamp, $keyframe, $start_pos, $data) = $flv_wrapper->getNextTag(myFlvHandler::GET_NEXT_TAG_ALL); if (!isset($type)) { break; } $fixedTS = null; switch ($type) { case myFlvHandler::TAG_TYPE_AUDIO: if (!isset($aInsaneTS) && $timestamp - $aTS > 200) { $aInsaneTS = $timestamp - $aTS; } if (isset($aInsaneTS)) { $fixedTS = $timestamp - $aInsaneTS; } $aTS = $timestamp; break; case myFlvHandler::TAG_TYPE_VIDEO: if (!isset($vInsaneTS) && $timestamp - $vTS > 200) { $vInsaneTS = $timestamp - $vTS; } if (isset($vInsaneTS)) { $fixedTS = $timestamp - $vInsaneTS; } $vTS = $timestamp; break; case myFlvHandler::TAG_TYPE_METADATA: break; default: continue; } if ($size == self::TAG_WRAPPER_SIZE) { // dont write tag with no actual data continue; } if (isset($fixedTS)) { $data = self::updateFlvTagTimestamp($data, $fixedTS); } fwrite($fh, $data); } fclose($fh); }
public static function fixRed5WebcamFlv($flv_file_name, $new_file) { $flv_wrapper = new myFlvHandler($flv_file_name); $header = $flv_wrapper->getHeader(); // sort timestamps because of a bug in red5 webcam recording $sorted_tags = array(); $index = ""; $tag_count = 0; $last_pos = $flv_wrapper->pos; while ($tag = $flv_wrapper->getNextTag()) { // list($type, $size, $timestamp, $keyframe, $start_pos) = $tag; $index = sprintf("%010d%06d", $tag[self::TAG_FIELD_TIMESTAMP], ++$tag_count); $sorted_tags[$index] = $last_pos; $last_pos = $flv_wrapper->pos; } ksort($sorted_tags, SORT_NUMERIC); $fh = fopen($new_file, "wb"); fwrite($fh, $header); foreach ($sorted_tags as $timestamp => $pos) { $flv_wrapper->seek($pos); list($type, $size, $timestamp, $keyframe, $start_pos, $data) = $flv_wrapper->getNextTag(myFlvHandler::GET_NEXT_TAG_ALL); if ($size != self::TAG_WRAPPER_SIZE) { // dont write tag with no actual data fwrite($fh, $data); } } fclose($fh); }