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

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

// Load options
if (count($argv) != 5) {
  echo "Usage: encaudio <file> <kbps> <Hz> <basename>\n";
  exit;
}
$name = array_shift($argv);
$file = array_shift($argv);
$kbps = array_shift($argv);
$hz = array_shift($argv);
$base = array_shift($argv);

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

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

$cmd = sprintf('mencoder - -o %s.mp2 -of rawaudio -srate %u -ovc copy -oac lavc -lavcopts acodec=mp2:abitrate=%u',
        $base, $hz, $kbps);
$ds = Array(
  0 => Array("pipe", "r"),
  1 => Array("file", sprintf("%s.mp2.log", $base), "a"),
  2 => Array("file", sprintf("%s.mp2.log", $base), "a"),
);
$p = proc_open($cmd, $ds, $pipes);
if (!$p) {
  printf("Command execution failed: %s\n", $cmd);
  return 1;
}

$blksz = 4096;
$count = 0;
$running = true;
while ($running) {
  $data = fread($src, $blksz);
  $count+= strlen($data);
  $out = fwrite($pipes[0], $data, strlen($data));
  if (!$out) break;
  if (0 == ($count % 65536)) {
    printf("\r  %3.3f%% [%09u/%09u]  ", (100 * ($count / $len)), $count, $len);
  }
  $running = !feof($src);
}

printf("\r  %3.3f%% [%09u/%09u]  \n", (100 * ($count / $len)), $count, $len);

fclose($src);
fclose($pipes[0]);
$ret = proc_close($p);

if (0 != $ret) {
  printf("Command returned with status %u\n", $ret);
  printf("Check command log (%s.mp2.log) for details\n", $base);
  return 1;
}

?>