3
我有一个任务,使MP3播放器嵌入在一个页面,将播放一些语音邮件存储在数据库中。一些消息以WAV格式存储,所以它们必须转换成MP3。转换应该“实时”完成。由于并非所有消息都必须转换,我希望使用一个将在需要时使用的流过滤器是一个好主意。PHP Lame流过滤器
class LameFilter extends php_user_filter
{
protected $process;
protected $pipes = array();
public function onCreate() {
$descriptorspec = array(
0 => array("pipe", "r"),
1 => array("pipe", "w"),
//2 => array("pipe", "w"),
);
$this->process = proc_open('lame --cbr -b 128 - -', $descriptorspec, $this->pipes);
}
public function filter($in, $out, &$consumed, $closing) {
while ($bucket = stream_bucket_make_writeable($in)) {
fwrite($this->pipes[0], $bucket->data);
$data = '';
while (true) {
$line = fread($this->pipes[1], 8192);
if (strlen($line) == 0) {
/* EOF */
break;
}
$data .= $line;
}
$bucket->data = $data;
$consumed += $bucket->datalen;
stream_bucket_append($out, $bucket);
}
return PSFS_PASS_ON;
}
public function onClose() {
//$error = stream_get_contents($this->pipes[2]);
fclose($this->pipes[0]);
fclose($this->pipes[1]);
//fclose($this->pipes[2]);
proc_close($this->process);
}
}
/* Register our filter with PHP */
stream_filter_register("lame", "LameFilter")
or die("Failed to register filter");
$mp3 = fopen("result.mp3", "wb");
/* Attach the registered filter to the stream just opened */
stream_filter_append($mp3, "lame");
$wav = fopen('ir_end.wav', 'rb');
while (!feof($wav)) {
fwrite($mp3, fread($wav, 8192));
}
fclose($wav);
fclose($mp3);
在示例中,我使用从一个文件读取并写入另一个文件。但实际上数据是从OCI-lob读取的,必须写入STDOUT。
的问题是,线 “$线=的fread($这 - >管[1],8192);”在预期的数据长度上实际上独立地阻止脚本。
有从过程中不要关闭其STDIN读取任何正确的方式?
没有,只是摆脱水桶和使用if语句:) – 2010-12-13 12:14:10