PHP Lame Filter

I have a task to create an mp3 player, built into the page, which will play voice messages stored in the database. Some messages are stored in WAV format, so they need to be converted to mp3. The conversion must be done on the fly. Due to the fact that not all messages need to be converted, I decided that it would be a good idea to use a stream filter, which will be used as needed.

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);

In the example, I used reading from one file and writing to another. But in fact, data is read from OCI-lob and must be written to STDOUT.

The problem is that the line "$ line = fread ($ this-> pipes [1], 8192); blocks the script virtually regardless of the expected data length.

, STDIN?

+3
1

, BLOB lame , popen() ?

0

Source: https://habr.com/ru/post/1760754/


All Articles