使用PHP的LAME将WAV转换为MP3 [英] Convert WAV to MP3 using LAME from PHP
问题描述
我有WAV数据,我想使用PHP脚本将其实时转换为MP3. WAV文件起源于脚本,因此它不是作为文件开头的.
I have WAV data that I'd like to convert to MP3 on the fly with a PHP script. The WAV file originates with the script, so it does not start out as a file.
我可以运行以下命令:
exec( "lame --cbr -b 32k in.wav out.mp3" );
但这需要我先将in.wav写入磁盘,从磁盘中读出.mp3,然后在完成后进行清理.我不想这样做.相反,我将wav文件存储在$ wav中,我想通过LAME运行它,以便将输出的数据存储在$ mp3中.
But this will require that I first write in.wav to disk, read out.mp3 from disk, and then clean up when I'm finished. I'd prefer not to do that. Instead, I have the wav file stored in $wav, and I'd like to run this through LAME such that the outputted data is then stored in $mp3.
我已经看到了对FFMPEG PHP库的引用,但是如果可能的话,我宁愿避免为该任务安装任何其他库.
I've seen references to an FFMPEG PHP library, but I'd prefer to avoid having to install any additional libraries for this task if possible.
推荐答案
似乎proc_open()是我想要的.这是我编写和测试的代码片段,完全符合我的期望:
It appears that proc_open() is what I was looking for. Here's the snippet of code I wrote and tested that does exactly what I was looking for:
位置:
- $ wav是要转换的原始WAV数据.
- $ mp3保存转换后的MP3数据,
- $wav is the original WAV data to be converted.
- $mp3 holds the converted MP3 data,
$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 );
最终输出的数据与运行/usr/bin/lame --cbr -b 32k in.wav out.mp3
的情况相同.
The final outputted data is identical to if I had run /usr/bin/lame --cbr -b 32k in.wav out.mp3
.
这篇关于使用PHP的LAME将WAV转换为MP3的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!