首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >PHP Lame流过滤器

PHP Lame流过滤器
EN

Stack Overflow用户
提问于 2010-08-20 22:19:29
回答 1查看 817关注 0票数 3

我有一个任务,使一个mp3播放器嵌入在一个页面上,将播放一些存储在数据库中的语音信息。有些消息是以WAV格式存储的,因此它们必须转换为mp3。转换应该在“运行中”完成。因为不是所有的消息都必须转换,所以我认为使用流过滤器将是一个好主意,它将在需要时使用。

代码语言:javascript
复制
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?

EN

回答 1

Stack Overflow用户

发布于 2010-12-13 11:32:00

作为此解决方案的替代方案,您是否考虑过将BLOB保存到临时文件并使用lame转换临时文件,以便只使用popen()将结果流式传输回来?

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/3531733

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档