Today, the editor will share with you the relevant knowledge points on how to convert amr to mp3 in php. , I hope you have gained something after reading this article, let's take a look at it together.
How to convert amr to mp3 in php: 1. Install ffmpeg on the server; 2. Use ffmpeg -i command to convert amr to mp3 format; 3. In The web page uses the HTML5 audio tag to play the mp3 file.
PHP converts amr audio files to mp3 format
Let’s talk about the overall idea
1. Install ffmpeg on the server
2. Use the ffmpeg -i command to convert amr to mp3 format (this will be written in the PHP code and executed with the exec function )
3. Use the HTML5 audio tag on the web page to play mp3 files
The following are the operation details:
1. Install ffmpeg on the server to Cenos as an example
It needs special attention that in the following method, the installation of amrnb and amrwb to make will request a URL of 3gp, which is generally not available , you can use crtl+c to cancel his process, and these two formats can be converted if you don’t need them.
When you receive the demand, you need to convert amr to mp3 under Linux, and directly under windows You can use the exe method encapsulated by the third-party jar package, but it does not support Linux. After crawling the Internet, it is said that it can be realized with ffmpeg and amr plug-in. I tried it according to the tutorial:
1. First install the system Compilation environment
yum install -y automake autoconf libtool gcc gcc-c++ #CentOS
2. Compile the required source package
#yasm: assembler, the new version of ffmpeg adds assembly code
wget http://www.tortall.net/projects/yasm/releases/yasm-1.3.0.tar.gz
tar -xzvf yasm-1.3.0.tar.gz
cd yasm-1.3.0
./configure
make
make install
#lame: Mp3 audio decoding
wget http://jaist.dl.sourceforge.net/project/lame/lame/3.99/lame-3.99.5.tar.gz
tar -xzvf lame-3.99.5.tar.gz
cd lame-3.99.5
./configure
make
make install
#amr support
wget http://downloads.sourceforge.net/project/opencore-amr/opencore-amr/opencore-amr-0.1.3.tar.gz
tar -xzvf opencore-amr-0.1.3.tar.gz
cd opencore-amr-0.1.3
./configure
make
make install
#amrnb support
wget http://www.penguin.cz/~utx/ftp/amr/amrnb-11.0.0.0.tar.bz2
tar -xjvf amrnb-11.0.0.0.tar.bz2
cd amrnb-11.0.0.0
./configure
make
make install
#amrwb support
wget http://www.penguin.cz/~utx/ftp/amr/amrwb-11.0.0.0.tar.bz2
tar -xjvf amrwb-11.0.0.0.tar.bz2
cd amrwb-11.0.0.0
./configure
make
make install
#ffmpeg
wget http://ffmpeg.org/releases/ffmpeg-2.5.3.tar.bz2
tar -xjvf ffmpeg-2.5.3.tar.bz2
cd ffmpeg-2.5.3
./configure --enable-libmp3lame --enable-libopencore-amrnb --enable-libopencore-amrwb --enable-version3 --enable-shared
make
make install
#load configuration
#Finally, after writing the config, run the ffmpeg command on the terminal, if success and installed extensions appear, the operation is successful.
ldconfig
3. How to use
ffmpeg -i 1.mp3 -ac 1 -ar 8000 1.amr #MP3 conversion AMR
ffmpeg -i 1.amr 1.mp3 #AMR conversion MP3
Appendix:
Appendix 1. The default installation directory of ffmpeg is /usr/local/ lib, the software directory in some 64-bit systems is /usr/lib64,
ffmpeg: error while loading shared libraries: libmp3lame.so.0: cannot open shared object file: No such file or may appear during compilation directory and other similar errors, the solution is to create a soft link:
# ln -s /usr/local/lib/libmp3lame.so.0.0.0 /usr/lib64/libmp3lame.so.0
Appendix 2. If the following prompt appears: ffmpeg: error while loading shared libraries: libavdevice.so.54: cannot open shared object file: No such file or directory
You can check the dynamic link library of ffmpeg in the following way Not found:
ldd `which ffmpeg`
libavdevice.so.54 => not found
libavfilter.so.3 => not found
libavformat.so.54 => not found
libavcodec.so.54 => not found
libswresample.so.0 => not found
libswscale.so.2 => not found
libavutil.so.51 =>not found
libm.so.6 => /lib64/libm.so.6 (0x00002ab7c0eb6000)
libpthread.so.0 => /lib64/libpthread.so.0 (0x00002ab7c100b000)
libc.so.6 => /lib64/libc.so.6 (0x00002ab7c1125000)
/lib64/ld-linux-x86-64.so.2 (0x00002ab7c0d9a000)
#If it is similar to the above output content, search for the above class library, you will find that all are under /usr/local/lib/
find /usr/local/lib/ | grep -E "libavdevice.so.54|libavfilter.so.3|libavcodec.so.54"
/usr/local/lib/libavfilter.so.3.17.100
/usr/local/lib/libavcodec.so.54.59.100
/usr/local/lib/libavdevice.so.54
/usr/local/lib/libavcodec.so.54
/usr/local/lib/libavfilter.so.3
/usr/local/lib/libavdevice.so.54.2.101
#View link library configuration file
more /etc/ld.so.conf | grep /usr/local/lib
#If it is not included, you need to edit this article to add:
vi /etc/ld.so.conf
/usr/local/lib
/usr/local/lib64
#run configure command
ldconfig
Introduction to ffmpeg:
FFmpeg is an open source free cross-platform video and audio streaming solution, which belongs to free software and adopts LGPL or GPL license certificate (depending on the components you choose). It provides a complete solution for recording, converting and streaming audio and video. It contains a very advanced audio/video codec library libavcodec. In order to ensure high portability and codec quality, many codecs in libavcodec are developed from scratch.
Second, use the ffmpeg command
After finishing the firstAfter the step, you can use ffmpeg --help to see if it is installed correctly, if not, please check if you forgot to make install
The command to use conversion is ffmpeg -i 1.amr 2.mp3
It will convert 1.amr to 2.mp3
Third, use php to execute the linux command ffmpeg
to convert the file Of course, we can’t keep going to the server to run linux commands, so we use php to execute linux commands to process amr files
Use the exec function to execute
$amr = './'.$vo['voice'];
$mp3 = $amr.'.mp3';
if(file_exists($mp3) == true){
// exit('No need to convert');
}else{
$command = "/usr/local/bin/ffmpeg -i $amr $mp3";
exec($command,$error);
}
Look at the code carefully, I use /usr/local/bin/ffmpeg to execute it here, because I can’t run ffmpeg instructions directly with php, if Your command is not in this directory, you can use locate or find to find the directory where ffmpeg is located.
Copyright Description:No reproduction without permission。
Knowledge sharing community for developers。
Let more developers benefit from it。
Help developers share knowledge through the Internet。
Follow us
2023-03-13
2023-03-13