데이터베이스에 저장된 음성 메시지를 재생할 페이지에 mp3 플레이어를 포함시키는 작업이 있습니다. 일부 메시지는 WAV 형식으로 저장되므로 mp3로 변환해야합니다. 변환은 "on fly"로 수행되어야합니다. 모든 메시지를 변환해야하는 것은 아니기 때문에 필요할 때 사용할 스트림 필터를 사용하는 것이 좋습니다.PHP 라메 스트림 필터
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-> pipes [1], 8192);" 예상 된 데이터 길이에 대해 실제로 독립적으로 스크립트를 차단합니다.
STDIN을 닫지 않는 프로세스에서 올바른 방법으로 읽을 수 있습니까?
아니요. 버킷을 없애고 if-statement를 사용합니다. :) –