Simple SoX wrapper for CarrierWave uploader that allows audio file conversion and watermarking
Add this line to your application's Gemfile:
gem 'carrierwave-audio'
And then execute:
$ bundle
Or install it yourself as:
$ gem install carrierwave-audio
Include CarrierWave::Audio into your CarrierWave uploader class:
class AudioUploader < CarrierWave::Uploader::Base
include CarrierWave::Audio
end
This gem provides two new processors for uploading audio files, convert
and watermark
.
If you'd like to convert from your initially uploaded file-type to a different one, use convert
like so:
version :mp3 do
process :convert => [{output_format: :mp3}]
def full_filename(for_file)
"#{super.chomp(File.extname(super))}.mp3"
end
end
process :convert
accepts a hash with one or two parameters:
process :convert => [{output_format:, output_options:}]
output_format
- Accepts a symbol. The only currently available option is the default, :mp3
.
output_options
- Optional. Sox options for the output file (see ruby-sox and the SoX documentation). Defaults to:
{
type: output_format.to_s,
rate: 44100,
channels: 2,
compression: 128
}
If you'd like to add a watermark over the top of your file, use watermark
like so:
version :watermarked do
process :watermark => [{
output_format: :mp3,
watermark_file: "#{Rails.root}/db/watermark.mp3"
}]
def full_filename(for_file)
"#{super.chomp(File.extname(super))}.mp3"
end
end
process :watermark
accepts a hash with up to three parameters:
process :watermark => [{watermark_file:, output_format:, output_options:}]
watermark_file
- REQUIRED. Path to where your watermarked file is stored.
VERY IMPORTANT: The watermarked file must be a 44.1k, 2-channel mp3. It needs to be a long file. It'll be truncated to fit the length of your uploaded file.
output_format
- Accepts a symbol. The only currently available option is the default, :mp3
.
output_options
- Optional. Sox options for the output file (see ruby-sox and the SoX documentation). Defaults to:
{
type: output_format.to_s,
rate: 44100,
channels: 2,
compression: 128
}
- Fork it
- Create your feature branch (
git checkout -b my-new-feature
) - Commit your changes (
git commit -am 'Add some feature'
) - Push to the branch (
git push origin my-new-feature
) - Create new Pull Request