我有WAV数据,我想用PHP脚本动态转换为MP3.WAV文件源自脚本,因此它不是以文件开头的.
我可以运行这样的东西:
exec( "lame --cbr -b 32k in.wav out.mp3" );
Run Code Online (Sandbox Code Playgroud)
但这需要我首先将in.wav写入磁盘,从磁盘读出.mp3,然后在我完成时清理.我宁愿不这样做.相反,我将wav文件存储在$ wav中,我想通过LAME运行它,以便输出的数据存储在$ mp3中.
我已经看到了对FFMPEG PHP库的引用,但是如果可能的话,我宁愿避免为此任务安装任何其他库.
看来proc_open()就是我想要的.这是我编写和测试的代码片段,它完全符合我的要求:
哪里:
- $ wav是要转换的原始WAV数据.
- $ mp3保存转换后的MP3数据,
$descriptorspec = array(
0 => array( "pipe", "r" ),
1 => array( "pipe", "w" ),
2 => array( "file", "/dev/null", "w" )
);
$process = proc_open( "/usr/bin/lame --cbr -b 32k - -", $descriptorspec, $pipes );
fwrite( $pipes[0], $wav );
fclose( $pipes[0] );
$mp3 = stream_get_contents( $pipes[1] );
fclose( $pipes[1] );
proc_close( $process );
Run Code Online (Sandbox Code Playgroud)
如果我跑了,最终输出的数据是相同的/usr/bin/lame --cbr -b 32k in.wav out.mp3.