How to package SoX binary with MP3 support for a NodeJS AWS Lambda Function with limitations of AWS's Linux AMI?

强颜欢笑 提交于 2019-12-03 08:57:43

Took me 3 months to finally post, and I figure it out the same day.

After doing some more research I came to the conclusion that I needed to create a static build of SoX with the necessary dependencies to convert MP3. I ended up putting together a script to create a static build based on this blog post, and this blog post.

The reason I combined a bit from both was because I was using Amazons Linux AMI, which is very barebones. So all the debian and apt-get stuff mentioned in the second blog would not likely work. However, I needed to ensure I had a complier installed so I wouldn't run into any errors. Below is what I ended up running. I did the whole thing as root, since privileges are fairly restricted on EC2 Amazon Linux AMI instances. The only thing that needs to be ran as the regular user is the PATH export at the very end.

sudo yum update
sudo yum install gcc44 gcc-c++ libgcc44 cmake –y

# now grab sox and its dependencies
mkdir -p deps
mkdir -p deps/unpacked
mkdir -p deps/built
mkdir -p deps/built/libmad
mkdir -p deps/built/sox
mkdir -p deps/built/lame
wget -O deps/sox-14.4.1.tar.bz2 "http://downloads.sourceforge.net/project/sox/sox/14.4.1/sox-14.4.1.tar.bz2?r=http%3A%2F%2Fsourceforge.net%2Fprojects%2Fsox%2Ffiles%2Fsox%2F14.4.1%2F&ts=1416316415&use_mirror=heanet"
wget -O deps/libmad-0.15.1b.tar.gz "http://downloads.sourceforge.net/project/mad/libmad/0.15.1b/libmad-0.15.1b.tar.gz?r=http%3A%2F%2Fsourceforge.net%2Fprojects%2Fmad%2Ffiles%2Flibmad%2F0.15.1b%2F&ts=1416316482&use_mirror=heanet"
wget -O deps/lame-3.99.5.tar.gz "http://downloads.sourceforge.net/project/lame/lame/3.99/lame-3.99.5.tar.gz?r=http%3A%2F%2Fsourceforge.net%2Fprojects%2Flame%2Ffiles%2Flame%2F3.99%2F&ts=1416316457&use_mirror=kent"

# unpack the dependencies
pushd deps/unpacked
tar xvfpj ../sox-14.4.1.tar.bz2
tar xvfpz ../libmad-0.15.1b.tar.gz
tar xvfpz ../lame-3.99.5.tar.gz
popd

# build libmad, statically
pushd deps/unpacked/libmad-0.15.1b
./configure --disable-shared --enable-static --prefix=$(realpath ../../built/libmad)
# Patch makefile to remove -fforce-mem
sed s/-fforce-mem//g < Makefile > Makefile.patched
cp Makefile.patched Makefile
make
make install
popd

# build lame, statically
pushd deps/unpacked/lame-3.99.5
./configure --disable-shared --enable-static --prefix=$(realpath ../../built/lame)
make
make install
popd

# build sox, statically
pushd deps/unpacked/sox-14.4.1
./configure --disable-shared --enable-static --prefix=$(realpath ../../built/sox) \
    LDFLAGS="-L$(realpath ../../built/libmad/lib) -L$(realpath ../../built/lame/lib)" \
    CPPFLAGS="-I$(realpath ../../built/libmad/include) -I$(realpath ../../built/lame/include)" \
    --with-mad --with-lame --without-oggvorbis --without-oss --without-sndfile --without-flac  --without-gomp
make -s
make install
popd

cp deps/built/sox/bin/sox .
rm -rf deps/built
rm -rf deps/unpacked

After this I ran the following as my normal user

export PATH=$PATH:/home/ec2-user

I could then run sox! Just typing in sox in the terminal should bring up sox the command list in your terminal window.

Even better, I was able to then download the single sox linux executable file, and then upload it to a brand new EC2 instance. The only things I had to run on the new instance to get it to work was the following:

sudo yum update
sudo yum install gcc44 gcc-c++ libgcc44 cmake –y
export PATH=$PATH:/home/ec2-user

I was worried that I would have to try and build it again without making the yum update or yum installs, since I ultimately want to put this in a Lambda function, but that was not necessary! When I added it to my test Lambda function and uploaded, it worked without an issue according to the cloud logs. That must mean the Amazon Linux AMI used within Lambda containers already has the yum updates and proper compilers installed.

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