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

Differences between version 3 and previous revision of DPG/makemp2.php.

Other diffs: Previous Major Revision, Previous Author

Newer page: version 3 Last edited on Monday, 11 August 2008 13:48:37 by CyberLeo Revert
Older page: version 2 Last edited on Sunday, 10 August 2008 13:57:03 by CyberLeo Revert
@@ -21,9 +21,9 @@
 $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); 
+ $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"), 
@@ -40,9 +40,12 @@
 while ($running) { 
  $data = fread($src, $blksz); 
  $count+= strlen($data); 
  $out = fwrite($pipes[0], $data, strlen($data)); 
- if (!$out) break; 
+ if (!$out) {  
+ echo "...huh. It didn't want any more.\n";  
+ break;  
+ }  
  if (0 == ($count % 65536)) { 
  printf("\r %3.3f%% [%09u/%09u] ", (100 * ($count / $len)), $count, $len); 
 
  $running = !feof($src); 

version 3

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

// Load options
if (count($argv) != 5) {
  echo "Usage: makemp2 <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) {
    echo "...huh. It didn't want any more.\n";
    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;
}

?>