Note: You are viewing an old version of this page. View the current version.

#!/usr/bin/env php
<?php

declare(ticks = 1);

// Load options
if (count($argv) != 5) {
#  echo "Usage: encvideo <file> <quality> <kbps> <fps> <basename>\n";
#  echo " <quality> can be 'low', 'med', 'high', 'ultra'\n";
  echo "Usage: encvideo <file> <kbps> <fps> <basename>\n";
  return 1;
}
$name = array_shift($argv);
$file = array_shift($argv);
$kbps = array_shift($argv);
$fps = array_shift($argv);
$base = array_shift($argv);

if (!file_exists($file)) {
  printf("encvideo: %s: No such file or directory\n", $file);
  return 1;
}

$fifo = sprintf("%s.vid", $base);
$passes = 2;

#if (!posix_mkfifo($fifo, 0600)) {
#  printf("mkfifo: %s: Failed\n", $fifo);
#  return 1;
#}

function cleanup() {
#  unlink(sprintf("%s.vid", $GLOBALS["base"]));
  foreach($GLOBALS['pids'] as $pid) {
    posix_kill($pid, SIGKILL);
  }
}
register_shutdown_function('cleanup');
pcntl_signal(SIGHUP, 'cleanup');
pcntl_signal(SIGINT, 'cleanup');
pcntl_signal(SIGTERM, 'cleanup');


$len = filesize($file);
$src = fopen($file, "r");

for ($pass=1; $pass <= $passes; $pass++) {
  fseek($src, 0);
  $pids = Array();
  // Source command
  $cmd = sprintf('mencoder /dev/stdin -nosound -o /dev/fd/3 -ofps %3$u -vf scale=256:192:0:0:1.00:0.00 -of lavf -lavfopts format=asf -ovc raw -oac copy 3>&1 > %1$s.m1v.src.log 2>&1 | ( sleep 1; mencoder /dev/stdin -o %1$s.m1v -passlogfile %1$s.m1v.2pass -fps 60 -of rawvideo -ovc lavc -oac copy -lavcopts vcodec=mpeg1video:keyint=%3$u:vbitrate=%4$u:vrc_buf_size=327:vrc_maxrate=%5$u:vpass=%2$u:mbd=2:trell:cbp:mv0:vmax_b_frames=2:cmp=6:subcmp=6:precmp=6:dia=4:predia=4:vb_strategy=2:bidir_refine=4:mv0_threshold=0:last_pred=3:preme=2:intra_matrix=8,9,12,22,26,27,29,34,9,10,14,26,27,29,34,37,12,14,18,27,29,34,37,38,22,26,27,31,36,37,38,40,26,27,29,36,39,38,40,48,27,29,34,37,38,40,48,58,29,34,37,38,40,48,58,69,34,37,38,40,48,58,69,79:inter_matrix=16,18,20,22,24,26,28,30,18,20,22,24,26,28,30,32,20,22,24,26,28,30,32,34,22,24,26,30,32,32,34,36,24,26,28,32,34,34,36,38,26,28,30,32,34,36,38,40,28,30,32,34,36,38,42,42,30,32,34,36,38,40,42,44%6$s )', $base, $pass, $fps, $kbps, ((int) $kbps * 1.5), (($pass != $passes)? ":turbo": ""));
  $ds = Array(
    0 => Array("pipe", "r"),
    1 => Array("file", sprintf("%s.m1v.enc.log", $base), "w"),
    2 => Array("file", sprintf("%s.m1v.enc.log", $base), "a")
  );
  $p = proc_open($cmd, $ds, $pipes);
  if (!$p) {
    printf("Command execution failed: %s\n", $cmd);
    return 1;
  }
  $child = proc_get_status($p);
  $pids[] = $child['pid'];

  $blksz = 4096;
  $count = 0;
  $running = true;
  while ($running) {
    $data = fread($src, $blksz);
    $count+= strlen($data);
    $out = fwrite($pipes[0], $data, strlen($data));
    if (!$out) {
      echo "Wrote 0 bytes to source!\n";
      break;
    }
    if ( 0 == ($count % 65536)) {
      printf("\r  %3.3f%% [S:%09u/%09u][P:%u/%u]  ", (100 * ($count / $len)), $count, $len, $pass, $passes);
    }
    $running = !feof($src);
  }
  printf("\r  %3.3f%% [S:%09u/%09u][P:%u/%u]  ", (100 * ($count / $len)), $count, $len, $pass, $passes);
  fclose($pipes[0]);
  $res = proc_close($p);
  if (0 != $res) {
    printf("Pipeline exited with status %u: %s\n", $res, $cmd);
    printf("Check command logs: %1$s.m1v.src.log, %1$s.m1v.enc.log\n", $base);
    return 1;
  }
/* -- Old Stuff --
  $src_cmd = sprintf('mencoder - -nosound -o %s -ofps %u -vf scale=256:192:0:0:1.00:0.00 -of lavf -lavfopts format=asf -ovc raw -oac copy', $fifo, $fps);
  $enc_cmd = sprintf('sleep 1; mencoder %s -o %s.m1v -passlogfile %2$s.m1v.2pass -fps 60 -of rawvideo -ovc lavc -oac copy -lavcopts vcodec=mpeg1video:keyint=%u:vbitrate=%u:vrc_buf_size=327:vrc_maxrate=%u:vpass=%u:mbd=2:trell:cbp:mv0:vmax_b_frames=2:cmp=6:subcmp=6:precmp=6:dia=4:predia=4:vb_strategy=2:bidir_refine=4:mv0_threshold=0:last_pred=3:preme=2:intra_matrix=8,9,12,22,26,27,29,34,9,10,14,26,27,29,34,37,12,14,18,27,29,34,37,38,22,26,27,31,36,37,38,40,26,27,29,36,39,38,40,48,27,29,34,37,38,40,48,58,29,34,37,38,40,48,58,69,34,37,38,40,48,58,69,79:inter_matrix=16,18,20,22,24,26,28,30,18,20,22,24,26,28,30,32,20,22,24,26,28,30,32,34,22,24,26,30,32,32,34,36,24,26,28,32,34,34,36,38,26,28,30,32,34,36,38,40,28,30,32,34,36,38,42,42,30,32,34,36,38,40,42,44', $fifo, $base, $fps, $kbps, ((int) $kbps * 1.5), $pass);
  $src_ds = Array(
    0 => Array("pipe", "r"),
    1 => Array("file", sprintf("%s.m1v.src.log", $base), "w"),
    2 => Array("file", sprintf("%s.m1v.src.log", $base), "a"),
  );
  $src_p = proc_open($src_cmd, $src_ds, $src_pipes);
  if (!$src_p) {
    printf("Command execution failed: %s\n", $src_cmd);
    return 1;
  }
  $enc_ds = Array(
    0 => Array("file", "/dev/null", "r"),
    1 => Array("file", sprintf("%s.m1v.enc.log", $base), "w"),
    2 => Array("file", sprintf("%s.m1v.enc.log", $base), "a"),
  );
  $enc_p = proc_open($enc_cmd, $enc_ds, $enc_pipes);
  if (!$enc_p) {
    printf("Command execution failed: %s\n", $enc_cmd);
    return 1;
  }
  // Everything's open! Start flinging data!
  $blksz = 4096;
  $count = 0;
  $out_count = 0;
  $running = true;
  while ($running) {
    $data = fread($src, $blksz);
    $count+= strlen($data);
    $out = fwrite($src_pipes[0], $data, strlen($data));
    if (!$out) {
      echo "Wrote 0 bytes to source!\n";
      break;
    }
    if (0 == ($count % 65536)) {
      printf("\r  %3.3f%% [S:%09u/%09u][P:%u/%u]  ", (100 * ($count / $len)), $count, $len, $pass, $passes);
    }
    $running = !feof($src);
  }
  printf("\r  %3.3f%% [S:%09u/%09u][P:%u/%u]  ", (100 * ($count / $len)), $count, $len, $pass, $passes);
  fclose($src_pipes[0]);
  $src_res = proc_close($src_p);
  $enc_res = proc_close($enc_p);
  if (0 != $src_res) {
    printf("Source command exited with status %u: %s\n", $src_res, $src_cmd);
    printf("Check command log: %s.m1v.src.log\n", $base);
    return 1;
  }
  if (0 != $enc_res) {
    printf("Encoder command exited with status %u: %s\n", $enc_res, $enc_cmd);
    printf("Check command log: %s.m1v.enc.log\n", $base);
    return 1;
  }
-- Old Stuff -- */
}
fclose($src);

return 0;
?>