<?
  /* Main include info */
  ini_set('include_path',"/usr/web/sites/ubixonline.com/include/");
  include("site.inc");
  include_once 'FLV/FLV.php';
  

  $query = "SELECT vid,file,length,width,height,thumb,thumbFrame FROM videos ORDER BY vid";
  $result = mysql_query($query);



  for ($i=0;$i<mysql_numrows($result);$i++) {
    $file = mysql_result($result,$i,'file');
    $len = mysql_result($result,$i,'length');
    $width = mysql_result($result,$i,'width');
    $height = mysql_result($result,$i,'height');
    $thumb  = mysql_result($result,$i,'thumb');
    $thumbFrame = mysql_result($result,$i,'thumbFrame');
    $vid = mysql_result($result,$i,'vid');

    if (($len <= 1) || ($width <= 1) || ($height <= 1)) {
  $flv = new FLV();
    print "File: $file\n";
  $meta = array();
$meta['metadatacreator'] = 'FLV Tools for PHP v0.1 by DrSlump';
$meta['metadatadate'] = gmdate('Y-m-d\TH:i:s') . '.000Z';
$meta['keyframes'] = array();
$meta['keyframes']['filepositions'] = array();
$meta['keyframes']['times'] = array();
$skipTagTypes = array();

    $flv->open("/usr/web/sites/ubixonline.com/docroot/streams/" . $file);

	while ($tag = $flv->getTag( $skipTagTypes ))
	{
	    $ts = number_format($tag->timestamp/1000, 3,'.','');
	    
	    if ($tag->timestamp > 0)
		    $meta['lasttimestamp'] = $ts;
	    
	    switch ($tag->type)
	    {
	        case FLV_Tag::TYPE_VIDEO :
	        	        	
	           	//Optimization, extract the frametype without analyzing the tag body
	           	if ((ord($tag->body[0]) >> 4) == FLV_Tag_Video::FRAME_KEYFRAME)
	           	{
					$meta['keyframes']['filepositions'][] = $flv->getTagOffset();
					$meta['keyframes']['times'][] = $ts;
	           	}
	           	
	            if ( !in_array(FLV_TAG::TYPE_VIDEO, $skipTagTypes) )
	            {
	                $meta['width'] = $tag->width;
	                $meta['height'] = $tag->height;
	                $meta['videocodecid'] = $tag->codec;                
	            	array_push( $skipTagTypes, FLV_Tag::TYPE_VIDEO );
	            }
	            
	        	break;
	        	
	        case FLV_Tag::TYPE_AUDIO :
	        	
	        	if ($ts - $oldTs > AUDIO_FRAME_INTERVAL)
	        	{
		        	$meta['audioframes']['filepositions'][] = $flv->getTagOffset();
		        	$meta['audioframes']['times'][] = $ts;
		        	$oldTs = $ts;
	        	}
	        	
	            if ( !in_array( FLV_Tag::TYPE_AUDIO, $skipTagTypes) )  
	            {
		            $meta['audiocodecid'] = $tag->codec;
		            $meta['audiofreqid'] = $tag->frequency;
		            $meta['audiodepthid'] = $tag->depth;
		            $meta['audiomodeid'] = $tag->mode;
		            
	            	array_push( $skipTagTypes, FLV_Tag::TYPE_AUDIO );
	            }
	        break;
	        case FLV_Tag::TYPE_DATA :
	            if ($tag->name == 'onMetaData')
	            {
	            	$fileMetaPos = $pos;
	            	$fileMetaSize = $tag->size;
	            	$fileMeta = $tag->value;
	            }
	        break;
	    }
	    
	    //Does it actually help with memory allocation?
	    unset($tag);
	}
    $len = $meta['lasttimestamp'];
    $width  = $meta['width'];
    $height = $meta['height'];
    print "Len: $len\nWidth: $width\nHeight: $height\n\n";

    $qry = "UPDATE videos SET length = $len,width = $width,height = $height WHERE vid = $vid";
    mysql_query($qry);
    unset($meta);
    unset($skipTagTypes);
    unset($flv);
      }
    if (($thumb == "") || ($thumbFrame == "")) {
      if ($thumbFrame == "")
        $thumbFrame = 1;
      print "File: $file\nThumb: $file.jpg\nthumbFrame: $thumbFrame\n\n";
      $cmd = "/usr/local/bin/ffmpeg -i \"/usr/web/sites/ubixonline.com/docroot/streams/$file\" -t 0.001 -ss $thumbFrame -vframes 1 -f mjpeg /tmp/1.jpg";
      system($cmd);
      $cmd = "/bin/mv /tmp/1.jpg \"/usr/web/sites/ubixonline.com/docroot/images/$file.jpg\"";
      system($cmd);
      $thumb = "$file.jpg";
      $query = "UPDATE videos SET thumb = \"$thumb\",thumbFrame = $thumbFrame WHERE vid = $vid";
      mysql_query($query);
      }
    }

?>
