Home  >  Article  >  Backend Development  >  How to convert amr to mp3 in php

How to convert amr to mp3 in php

藏色散人
藏色散人Original
2023-02-09 09:36:014448browse

How to convert amr to mp3 in php: 1. Install ffmpeg on the server; 2. Use the "ffmpeg -i" command to convert amr to mp3 format; 3. Use the HTML5 audio tag to play on the web page mp3 files will do.

How to convert amr to mp3 in php

The operating environment of this tutorial: Windows 10 system, PHP version 8.1, DELL G3 computer

How to convert amr to mp3 with php ?

PHP Convert 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 using the exec function)

3 . Use the HTML5 audio tag to play mp3 files on the web page

The following are the operation details:

1. Install ffmpeg on the server, taking cenos as an example

Reference here: http://my.oschina.NET/ethan09/blog/372435

It should be noted that in the following method, the installation of amrnb and amrwb into the make link will Requesting a URL of 3gp is generally not available. You can use crtl c to cancel its process, and the format can be converted if these two are not needed.

You must be in the Linux environment when receiving the request To convert amr to mp3, you can directly use the exe method encapsulated in a third-party jar package under Windows, but Linux is not supported. After crawling the Internet, it said that it can be achieved by using ffmpeg plus the 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 code package

#yasm:汇编器,新版本的ffmpeg增加了汇编代码

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音频解码

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支持

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支持

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支持

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



#加载配置

#最后写入config后,终端运行ffmpeg命令,出现success和已安装的扩展,则运行成功。

ldconfig

3. How to use

ffmpeg -i 1.mp3 -ac 1 -ar 8000 1.amr  #MP3转换AMR
ffmpeg -i 1.amr 1.mp3                 #AMR转换MP3
Appendix:

Appendix 1. The default installation directory of ffmpeg is "/usr/local/lib". In some 64-bit systems, the software directory is "/usr/lib64". ## may occur during the compilation process. #"ffmpeg: error while loading shared libraries: libmp3lame.so.0: cannot open shared object file: No such file or 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 which dynamic link libraries of ffmpeg were not found in the following way:

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)

#如果类似于上面的输出内容,查找以上类库,会发现全部在/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



#查看链接库配置文件

more  /etc/ld.so.conf | grep /usr/local/lib

#如果不包含的话,需要编辑此文添加:

vi /etc/ld.so.conf

/usr/local/lib

/usr/local/lib64

#运行配置命令

ldconfig

About ffmpeg:

FFmpeg is an open source free cross-platform The platform's video and audio streaming solution is free software and licensed under the LGPL or GPL (depending on which component you choose). It provides a complete solution for recording, converting and streaming audio and video. It contains the very advanced audio/video codec library libavcodec. In order to ensure high portability and codec quality, many codecs in libavcodec were developed from scratch. Its official website is: http://www.ffmpeg.org

Finally, please refer to http://linux.it.Net.cn/e/Linuxit/2014/0828/3980.html## for part of the content.

#2. Use the ffmpeg command

After completing the first step, you can use ffmpeg --help to see if it is installed correctly. If not, please check if it is installed correctly. Forgot to use make install

The conversion command is ffmpeg -i 1.amr 2.mp3

will convert 1.amr to 2.mp3

三, use php to execute linux instructions ffmpeg

Of course, you cannot convert files by running linux instructions in the server, so we use php to execute linux instructions to process amr files

Use the exec function to execute

$amr = './'.$vo['voice'];
$mp3 = $amr.'.mp3';

if(file_exists($mp3) == true){
    // exit('无需转换');
}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, because I cannot directly run the ffmpeg command using PHP. If your command is not in this directory, you can use locate or find to find the directory where ffmpeg is located

Recommended study: "

PHP Video Tutorial

"

The above is the detailed content of How to convert amr to mp3 in php. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn