PHP: Linux convert wav to mp3 using lame

PHOTO EMBED

Sun Aug 28 2022 01:42:26 GMT+0000 (Coordinated Universal Time)

Saved by @marcopinero #php

<?php


$wavfile=$_REQUEST['wav'];
$wav=file_get_contents($wavfile);

$descriptorspec = array(
    0 => array( "pipe", "r" ),
    1 => array( "pipe", "w" ),
    2 => array( "file", "/dev/null", "w" )
);

$process = proc_open( "/usr/bin/lame - -", $descriptorspec, $pipes );

fwrite( $pipes[0], $wav );
fclose( $pipes[0] );

$mp3 = stream_get_contents( $pipes[1] );
fclose( $pipes[1] );

proc_close( $process );

header('Content-Type: audio/mpeg');
echo $mp3;
content_copyCOPY

Single php script to convert one wav file to mp3. This is based on Linux using lame utility. Permissions to www-data user must be set for executing lame and writing output file at destination path.