How to convert amr to mp3 in php


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 tar -xzvf yasm-1.3.0.tar.gz cd yasm-1.3.0 ./configure make make install #lame: Mp3 audio decoding wget tar -xzvf lame-3.99.5.tar.gz cd lame-3.99.5 ./configure make make install #amr support wget tar -xzvf opencore-amr-0.1.3.tar.gz cd opencore-amr-0.1.3 ./configure make make install #amrnb support wget tar -xjvf amrnb- cd amrnb- ./configure make make install #amrwb support wget tar -xjvf amrwb- cd amrwb- ./configure make make install #ffmpeg wget 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 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: 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/ /usr/lib64/

Appendix 2. If the following prompt appears: ffmpeg: error while loading shared libraries: 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` => not found => not found => not found => not found => not found => not found =>not found => /lib64/ (0x00002ab7c0eb6000) => /lib64/ (0x00002ab7c100b000) => /lib64/ (0x00002ab7c1125000) /lib64/ (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 "||" /usr/local/lib/ /usr/local/lib/ /usr/local/lib/ /usr/local/lib/ /usr/local/lib/ /usr/local/lib/ #View link library configuration file more /etc/ | grep /usr/local/lib #If it is not included, you need to edit this article to add: vi /etc/ /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