Question

I want to stream .amr audio files on my server. After spending a couple hours of research, it's clear to me that this is not feasible under the current state of html5 audio. Following this disappointing finding, I spent several additional hours looking for a simple way to convert .amr files to .ogg, with similarly disappointing results.

I'm shocked that after well over 10 years of use, there is no simple way to play/convert files encoded under this standard, but can someone please help me to find a usable solution? The closest viable options seem to be sox and ffmpeg. I'm ideally looking for a concise set of instructions for converting .amr to .ogg directly from php but using the command line would be fine.

I am about to dig into the specifics of using these two libraries but figured that I would post here in hopes that some kind soul might help enlighten others that are under time constraints and so do not wish to spend an afternoon untangling the use-case specifics of what should be a straightforward task. If someone else does not post, I will answer the question myself.

Here are some of the other 'answers' that I've found which led me to post here:

If you are about to mark this as 'off-topic' or close the question for some other reason, please consider the following:

A search on https://superuser.com/search?q=convert+.amr+to+.ogg, which is stated in at least one of the above links as the appropriate forum for the question, (as of today) results in 4 posts, none of which even remotely address this question.

The SO Posting Guidelines seem to me to be completely in alignment with this kind of question. I'm baffled as to why someone would close the questions that came up in my search. I'm a very experienced developer, asking a question directly related to development work that I'm doing, and have been blocked by an issue that I would like to provide a solution for to aid in other developers being held up by similar issues.

The fact that nothing came up in my search to save me from doing hours of research leads me to see this as a worthy question for SO. Please help restore my sentiment for this site which is a valuable resource but has left me cold due to so many of these kinds of senseless and seemingly indiscriminate post closings.

Thanks...

Was it helpful?

Solution

If you're in an environment where the tools are already installed, converting the file is actually a very simple procedure. For ffmpeg and sox, the commands in their most basic forms are just:

ffmpeg -i ./file.amr ./file.ogg

or

sox ./file.amr ./file.ogg

So if we upload a file with an input name of 'audio', from PHP this would look something like:

<?php
if (isset($_FILES['audio']) && is_file($TmpFile=$_FILES['audio']['tmp_name'])) {
    // I name uploaded files as curr ms timestamp and track file data via db. 
    $NewFile = './uploads/'.round(microtime(true)*1000).'.ogg';  
    shell_exec("ffmpeg -i {$TmpFile} -acodec libvorbis ".$NewFile);
    // You'll want to add other file data to the database here...
} else {
    // Deal with bad file uploads...
}
?>

Unfortunately, my server environment doesn't have these tools installed, so I have to compile them manually. For my case specifically, I'm using a virtual shared host so I don't have universal access privileges on the system. Keep in mind that this is reflected in some of the code I'm using to install the various ffmpeg components.

cd ~/
curl -O http://ffmpeg.org/releases/ffmpeg-2.0.tar.gz
tar -xzvf ffmpeg-2.0.tar.gz
mv -fv ./ffmpeg-2.0 ./ffmpeg
rm -v ./ffmpeg-2.0.tar.gz

cd ~/ffmpeg
curl -O http://downloads.xiph.org/releases/ogg/libogg-1.3.1.tar.gz
tar -xzvf libogg-1.3.1.tar.gz
rm -v libogg-1.3.1.tar.gz
cd libogg-1.3.1
./configure --prefix="$HOME/ffmpeg" --disable-shared
make
make install
make distclean

cd ~/ffmpeg/
curl -O http://downloads.xiph.org/releases/vorbis/libvorbis-1.3.3.tar.gz
tar -xzvf libvorbis-1.3.3.tar.gz
rm -v libvorbis-1.3.3.tar.gz
cd libvorbis-1.3.3
./configure --prefix="$HOME/ffmpeg" --with-ogg="$HOME/ffmpeg" --disable-shared
make
make install
make distclean

cd ~/ffmpeg/
curl -O -L http://sourceforge.net/projects/opencore-amr/files/opencore-amr/opencore-amr-0.1.3.tar.gz
tar -xzvf opencore-amr-0.1.3.tar.gz
rm -v opencore-amr-0.1.3.tar.gz
cd opencore-amr-0.1.3
./configure --prefix="$HOME/ffmpeg" --disable-shared --bindir="$HOME/bin"
make
make install
make distclean

cd ~/ffmpeg/
curl -O http://www.tortall.net/projects/yasm/releases/yasm-1.2.0.tar.gz
tar -xzvf yasm-1.2.0.tar.gz
rm -v yasm-1.2.0.tar.gz
cd yasm-1.2.0
./configure --prefix="$HOME/ffmpeg" --bindir="$HOME/bin"
make
make install
make distclean


cd ~/ffmpeg/
mkdir ~/ffmpeg/tmp
chmod 777 ~/ffmpeg/tmp
export TMPDIR="$HOME/ffmpeg/tmp"
export PKG_CONFIG_PATH="$HOME/ffmpeg/lib/pkgconfig"
./configure --prefix="$HOME/ffmpeg" --extra-cflags="-I$HOME/ffmpeg/include" --extra-ldflags="-L$HOME/ffmpeg/lib" --bindir="$HOME/bin" --extra-libs="-ldl" --enable-gpl --enable-nonfree --enable-version3 --enable-libopencore-amrnb --enable-libopencore-amrwb --enable-libvorbis
make
make install
make distclean
rm -rfv $TMPDIR
export TMPDIR=""
export PKG_CONFIG_PATH=""

You could just copy and paste the above bash commands into a shell, or you could put them into an executable script, and it should take care of everything. You'll probably want to check for the newest versions of each component and update the code accordingly. You may also want to add other libraries if you're working with different file formats as well, but this boiled down to what I needed specifically.

You could also use:

git clone git://source.ffmpeg.org/ffmpeg.git ffmpeg

and eliminate the first block of code in the installation script if you have git installed, or if you have neither git nor curl you could just download each and upload to your server or use fget or something similar.

In all, this amounted to several hours of hunting down files, tracing compilation errors, tracking down the correct options for my scenario, and other aspects of this kind of tedium that makes up the worst of exactly what I like least about development work. I hope that this post might save others from this kind of unnecessary pain and suffering :-)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top