Home Backend Development PHP Problem How to convert amr to mp3 in php

How to convert amr to mp3 in php

Feb 09, 2023 am 09:36 AM
php mp3 amr

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 of this Website
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

Hot AI Tools

Undress AI Tool

Undress AI Tool

Undress images for free

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

ArtGPT

ArtGPT

AI image generator for creative art from text prompts.

Stock Market GPT

Stock Market GPT

AI powered investment research for smarter decisions

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

How to implement a singleton pattern in PHP? How to implement a singleton pattern in PHP? Sep 25, 2025 am 12:27 AM

Singleton pattern ensures that a class has only one instance and provides a global access point for scenarios where a single object coordinates the operation of the system, such as database connections or configuration management. 2. Its basic structure includes: private static attribute storage instances, private constructors prevent external creation, private cloning methods prevent copying, and public static methods (such as getInstance()) for obtaining instances. 3. Get a unique instance in PHP by calling getInstance() method, and returns the same object reference no matter how many times it is called. 4. Under the standard PHP request model, thread safety is not necessary to be considered, but synchronization issues need to be paid attention to in long run or multi-threaded environments, and PHP itself does not support native lock mechanism. 5. Although singletons are useful,

How to use the null coalescing operator (??) in PHP? How to use the null coalescing operator (??) in PHP? Sep 25, 2025 am 01:28 AM

Answer: PHP's empty merge operator (??) is used to check whether a variable or array key exists and is not null. If it is true, it returns its value, otherwise it returns the default value. It avoids the use of lengthy isset() checks, is suitable for handling undefined variables and array keys, such as $username=$userInput??'guest', and supports chain calls, such as $theme=$userTheme??$defaultTheme??'dark', which is especially suitable for form, configuration, and user input processing, but only excludes null values, empty strings, 0 or false are considered valid values ​​to return.

How to get URL parameters in PHP? How to get URL parameters in PHP? Sep 24, 2025 am 05:11 AM

Use $_GET to get URL parameters, such as ?name=John&age=25; check existence through isset or empty merge operators, and filter and verify data with filter_input to ensure security.

How to disable a function in PHP? How to disable a function in PHP? Sep 24, 2025 am 02:40 AM

TodisableaPHPfunction,usedisable_functionsinphp.iniforbuilt-infunctionslikeexecorsystem,whichblocksthemgloballyforsecurity;foruser-definedfunctions,preventexecutionbywrappingtheminconditions,renaming,commentingout,orcontrollingfileinclusionviaautoloa

How to download a file from a URL in PHP? How to download a file from a URL in PHP? Sep 24, 2025 am 05:45 AM

Answer: Use file_get_contents and cURL to download URL files, the former is simple but restricted, while the latter is more flexible and supports streaming. Examples include directly reading and writing files, cURL initialization setting options and saving, adding error handling and HTTP status checking. Large files are recommended to stream download in blocks to save memory, ensuring that the directory is writable and handle exceptions properly.

How to implement an interface in a PHP class? How to implement an interface in a PHP class? Sep 25, 2025 am 05:34 AM

Use the implements keyword to implement the interface, and the class must provide specific implementations of all methods in the interface. 2. Define the interface to declare the method using the interface keyword. 3. Class implements interface and overrides methods. 4. Create an object and call the method to output the result. 5. A class can implement multiple interfaces to ensure code specification and maintainability.

How to sanitize user input to prevent XSS in PHP? How to sanitize user input to prevent XSS in PHP? Sep 25, 2025 am 05:19 AM

TopreventXSSinPHP,sanitizeuserinputandescapeoutputbasedoncontextusinghtmlspecialchars()forHTML,json_encode()forJavaScript,andvalidatestrictlywithfilter_var()forexpecteddatatypes,whileavoidingdeprecatedfunctionsandusingContent-Security-Policyheadersfo

How to use GET and POST methods in an HTML form with PHP? How to use GET and POST methods in an HTML form with PHP? Sep 25, 2025 am 03:46 AM

The GET method attaches data to the URL, which is suitable for non-sensitive information; the POST method sends data through the request body, which is more secure and suitable for sensitive information.

See all articles