Converting and Playing AMR Audio Files in Web Browsers with PHP and FFmpeg

Overview

This solutino involves three main components:

  1. Install FFmpeg on the server
  2. Use FFmpeg to convert AMR files to MP3 format (executed via PHP)
  3. Play the MP3 files using HTML5 audio elements in the browser

Server Setup: Installling FFmpeg on CentOS

Prerequisites

Install the required build tools:

yum install -y automake autoconf libtool gcc gcc-c++

Compiling Dependencies

YASM Assembler

Required for newer FFmpeg versions with assembly optimizations:

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 Encoder

Provides MP3 encoding capabilities:

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

OpenCore AMR Codecs

Adds AMR format 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

AMR-NB and AMR-WB Codecs

For narrowband and wideband AMR 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

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 Core

Compile FFmpeg with all codec support:

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

After compilation, reload the dynamic linker:

ldconfig

Basic Usage

ffmpeg -i input.mp3 -ac 1 -ar 8000 output.amr    # MP3 to AMR
ffmpeg -i input.amr output.mp3                   # AMR to MP3

Troubleshooting Shared Libraries

Missing libmp3lame on 64-bit Systems

If encountering errors like libmp3lame.so.0: cannot open shared object file, create a symlink:

ln -s /usr/local/lib/libmp3lame.so.0.0.0 /usr/lib64/libmp3lame.so.0

Library Path Configuration

Verify library paths with:

ldd `which ffmpeg`

If libraries are not found, update /etc/ld.so.conf:

vi /etc/ld.so.conf

Add the following lines:

/usr/local/lib
/usr/local/lib64

Then reload:

ldconfig

PHP Integration

Use PHP's system() function to execute FFmpeg commands for automatic conversion:

<?php
$inputFile = './' . $audioData['voice_path'];
$outputFile = $inputFile . '.mp3';

if (file_exists($outputFile)) {
    // Already converted
} else {
    $cmd = '/usr/local/bin/ffmpeg -i ' . $inputFile . ' ' . $outputFile;
    system($cmd, $statusCode);
}

Determine the FFmpeg binary path if direct execution fails:

which ffmpeg
# or
find / -name ffmpeg -type f 2>/dev/null

Web Playback Implementation

Use HTML5 audio elements to play converted MP3 files:

<audio controls>
    <source src="path/to/converted/file.mp3" type="audio/mpeg">
    Your browser does not support audio playback.
</audio>

Technical Background

FFmpeg is an open-source, cross-platform multimedia framework released under LGPL/GPL licenses. It provides a complete solution for recording, converting, and streaming audio and video content. The core library libavcodec contains highly optimized audio/video codecs developed specifically for portability and quality.

Note: The AMR-NB and AMR-WB compilation steps may attempt to access 3GPP servers during the build process. These downloads typically timeout, but they are not essential for basic AMR to MP3 conversion functionality.

Tags: ffmpeg amr mp3 PHP audio

Posted on Wed, 13 May 2026 17:45:12 +0000 by Pha