我有一个任务,使一个mp3播放器嵌入在一个页面上,将播放一些存储在数据库中的语音信息。有些消息是以WAV格式存储的,因此它们必须转换为mp3。转换应该在“运行中”完成。因为不是所有的消息都必须转换,所以我认为使用流过滤器将是一个好主意,它将在需要时使用。
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。
问题是"$line = fread($this->pipes1,8192);“这一行实际上独立于预期数据长度阻塞了脚本。
有没有正确的方法来读取进程而不是关闭它的STDIN?
发布于 2010-12-13 11:32:00
作为此解决方案的替代方案,您是否考虑过将BLOB保存到临时文件并使用lame转换临时文件,以便只使用popen()将结果流式传输回来?
https://stackoverflow.com/questions/3531733
复制相似问题