Stream as html5 audio or convert .amr to .ogg

别等时光非礼了梦想. 提交于 2019-12-04 11:04:10

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 :-)

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!