<?
/*
 * (C) 2008 Christopher Olsen <cwolsen@domainatlantic.com>
 *
 * $Id$
 */

include ("include/config.php");

if (file_exists("/tmp/cron2.php.lock"))
  exit(0);

system("/usr/bin/touch /tmp/cron2.php.lock");

$query = "SELECT * FROM videos_pending v INNER JOIN users u ON v.oid = u.uid WHERE encoded = 0";
$result = $db->query($query);

while ($qData = $result->fetch_assoc()) {

  exec("/usr/local/bin/mediainfo --Inform=\"Video;%Width%,%Height%,%BitRate%,%FrameRate%,%Duration/String3%\" /media/incoming/" . $qData['file'], $mediaInfo);

  $vData = explode(",", $mediaInfo[0]); 

  $mWidth = $vData[0];
  $mHeight = $vData[1];
  $mBitRate = $vData[2];
  $mFrameRate = $vData[3];
  sscanf($vData[4], "%i:%i:%i.%i", $hour, $min, $sec, $ms);

  $mDuration = ($hour * 3600) + ($min * 60) + $sec + ("0." . $ms);

  unset($mediaInfo);

  if (($mWidth == 0) || ($mHeight == 0)) {
    $mType = 2;
    $encode = "/usr/local/bin/ffmpeg -loop_input -i \"/usr/home/reddawg/UbixCube.gif\" -i \"/usr/web/uploads/" . $qData['file'] . "\" -ar 44100 -ac 2 -ab 128k -t $oLen -r 0.5 -f flv -y \"/media/streams/" . $qData['file'] . ".flv\"";
    print("[$encode]");
//    $res = system($encode);
  } else {
    $mType = 1;

    $encode = "/usr/local/bin/ffmpeg -i \"/media/incoming/" . $qData['file'] . "\" -c:v libx264 -vf \"scale=w=800:h=450:force_original_aspect_ratio=1,pad=800:450:(ow-iw)/2:(oh-ih)/2\" -b:v 1M -maxrate 2M -bufsize 4M -preset veryfast -pass 1 -f flv -an -y /dev/null";

    print("[$encode]");

    $res = system($encode, $retVal);

    if ($retVal == 1)
      doExit($encode, $data['vid']);

    $encode = "/usr/local/bin/ffmpeg -i \"/media/incoming/" . $qData['file'] . "\" -c:v libx264 -vf \"scale=w=800:h=450:force_original_aspect_ratio=1,pad=800:450:(ow-iw)/2:(oh-ih)/2\" -b:v 1M -maxrate 2M -bufsize 4M -preset veryfast -c:a mp3 -b:a 128k -pass 2 -f flv -y \"/media/tmp/" . $qData['file'] . ".flv\""; 

    print("[$encode]");

    $res = system($encode, $retVal);

    if ($retVal == 1)
      doExit($encode, $data['vid']);

    print($res);

  system("/bin/mv \"/media/tmp/" . $qData['file'] . ".flv\" \"/red5/webapps/oflaDemo/streams/" . $qData['file'] . ".flv\"");
  system("/usr/sbin/chown tuve:www \"/red5/webapps/oflaDemo/streams/" . $qData['file'] . ".flv\"");

  /* Move The File To The Mirrors */
  /* No more mirrors */
  // system("/usr/bin/scp \"/media/streams/$file.flv\" www@utopia.ubixonline.com:.");
  // system("/usr/bin/scp \"/media/streams/$file.flv\" tuploads@tuve.ubixonline.com:.");

  /* Update The videos_pending Table */
  $query = "UPDATE videos_pending SET encoded = 1 WHERE vid = " . $qData['vid'];
  $db->query($query);

  /* Insert The Video Into The System */
  $uDate = time();
  $query = "INSERT INTO videos (artist,title,genre,file,rating,classification,oid,mType,length,width,height,added) VALUES(\"" . $qData['artist'] . "\",\"" . $qData['title'] . "\",\"" . $qData['genre'] . "\",\"" . $qData['file'] . ".flv\"," . $qData['rating'] . "," . (($qData['classification'] > 0) ? $qData['classification'] : 1) . "," . $qData['oid'] . ",$mType,$mDuration,800,450,$uDate)";
  print "[$query]";
  $res = $db->query($query);
  if (! $res) {
    echo "Encoding Failed!\n";
    $eStatus = "Failed";
  } else
    $eStatus = "Successful";

  $headers = 'MIME-Version: 1.0' . "\r\n";
  $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
  $headers .= 'From: TUve Support <tuve@tuve.tv>' . "\r\n";

  $message = "New Video Encoded: " . $qData['artist'] . " - " . $qData['title'] . "<BR><BR>Your video is now ready for viewing.<BR>Sincerely,<BR>The TUve Staff<BR>\n";
  mail("Christopher Olsen <cwolsen@domainatlantic.com>", "Media Encoding $eStatus", $message, $headers);

  /*
   * $fname = mysql_result($result, 0, 'firstname');
   * $lname = mysql_result($result, 0, 'lastname');
   * $email = mysql_result($result, 0, 'email');
   */
  mail($qData['firstname'] . " " . $qData['lastname'] . " <" . $qData['email'] . ">", "Media Encoded", $message, $headers);

  system("/bin/mv \"/media/incoming/" . $qData['file'] . "\" /media/orig");
  }
}
system("/bin/rm /tmp/cron2.php.lock");

function doExit($errorStr, $vid_id, $error_code = 0)
{
  global $db;

  /* Update The videos_pending Table */
  $query = "UPDATE videos_pending SET encoded = 2, error_code = " . $error_code . ", error_msg = \"" . $errorStr . "\" WHERE vid = $vid_id";
  $db->query($query);

  print("Error: $errorStr\n");


  exit(0);
}
?>
