Converting audio files in Rails using Paperclip
You can use Paperclip Processors to convert audio files in Rails. I was using Rails to receive .mp3 files and converting them to .m4a and .amr so that iOS devices and Android devices can play the respective audio files. You have to first install ffmpeg
in the system that you have, and oh dear… It can really be quite a pain in installing ffmpeg. I had a hard time installing on my Ubuntu machine because of all the dependencies which I needed. I forgot how I did it, but more information on how to install it can be found at https://trac.ffmpeg.org/wiki/CompilationGuide/MacOSX
Luckily for Mac users with homebrew, you can just install by using the following command:
brew install ffmpeg --with-fdk-aac --with-ffplay --with-freetype --with-libass --with-libquvi --with-libvorbis --with-libvpx --with-opus --with-x265
We will be using ffmpeg mainly to convert our audio files. To convert .mp3 file to .amr, you can use do:
$ ffmpeg -i audio1.mp3 -ar 22050 audio1.amr
This converts an input named audio1.mp3
to audio1.amr
with an audio rate of 22.05Khz.
Now, it’s time to use Rails to make full use of ffmpeg
. First, setup a model that contains an audio file with paperclip:
In this model, we have an AudioMessage ActiveRecord model. We are using Paperclip to store the file audio
. We have set the .mp3 files to be converted to .m4a format via the m4a_processor, and the .amr format via the .amr_processor. Now it’s time to setup the processors. We will need to create a file for each processor in /lib/paperclip_processors
. Paperclip automatically uses this directory to look for its processor files. Let’s start by creating our amr_processor
.
The AmrProcessor class we use here subclasses from the Processor class provided by Paperclip. These Processor class allows you to modify the files that you attach to the model, and does not only apply to audio. You can use it for image processing such as cropping or inserting watermarks. For the initialization, we use super
to inherit the default behaviour from Paperclip::Processor
.
Next we need to make some attributes accessible for the class.
All these will be used to determine the command we will use to run our ffmpeg command. It is now time to fill in the make
method which is provided by Paperclip::Processor but will need to be modified to give the behaviour of converting our audio file.
First, we declare our source file, and the output that we want. Note that @format
here is amr
, so that the eventual output will be in .amr format. The parameters
term is used to run alongside with ffmpeg through Paperclip.run()
method where you can fill in interpolation values in the string that we have in parameters
:
parameters = "-y -i :source -ar :sample_rate -ab :bit_rate :dest"
Paperclip.run()
eventually will run the ffmpeg command stated near the beginning of this post, with all the sample rate and bitrates determined by our parameters in the initialize
method. The named arguments we have in Paperclip.run will substitute the symbol strings we have in parameters
to run the command such as:
ffmpeg -y -i source.mp3 -ar 8000 -ab 12.2k destination.amr
With this, your processor is set! The audio conversion should happen immediately after you upload an audio file tagged to the AudioMessage object. Do the same for the M4aProcessor, and you will be able to convert the uploaded file into both .amr and .m4a formats automatically.
M4aProcessor: