83 lines
2.2 KiB
Ruby
83 lines
2.2 KiB
Ruby
class MixUploader < CarrierWave::Uploader::Base
|
|
# include CarrierWaveDirect::Uploader
|
|
include CarrierWave::MimeTypes
|
|
|
|
process :set_content_type
|
|
process :add_metadata
|
|
|
|
version :mp3_url do
|
|
process :create_mp3
|
|
|
|
def full_filename(file)
|
|
model.filename('mp3') if model.id
|
|
end
|
|
end
|
|
|
|
def initialize(*)
|
|
super
|
|
JamRuby::UploaderConfiguration.set_aws_private_configuration(self)
|
|
end
|
|
|
|
# Add a white list of extensions which are allowed to be uploaded.
|
|
def extension_white_list
|
|
%w(ogg)
|
|
end
|
|
|
|
def store_dir
|
|
nil
|
|
end
|
|
|
|
def md5
|
|
@md5 ||= ::Digest::MD5.file(current_path).hexdigest
|
|
end
|
|
|
|
def filename
|
|
model.filename('ogg') if model.id
|
|
end
|
|
|
|
|
|
def add_metadata
|
|
# add this metadata to ogg file without disturbing audio
|
|
# JamRecordingId=438
|
|
# JamMixId=438
|
|
# JamType=Mix
|
|
# secret sauce is -codec copy (or -acodec), and a bunch of -metadata arguments.
|
|
# after done, stomp input file with new one
|
|
|
|
input_file = current_path
|
|
output_file = current_path + '.new.ogg'
|
|
codec_param = RUBY_PLATFORM.include?('darwin') ? 'codec' : 'acodec'
|
|
ffmpeg_cmd = "#{APP_CONFIG.ffmpeg_path} -i \"#{input_file}\" -#{codec_param} copy -metadata JamRecordingId=#{model.recording_id} -metadata JamMixId=#{model.id} -metadata JamType=Mix \"#{output_file}\""
|
|
system(ffmpeg_cmd)
|
|
|
|
unless $? == 0
|
|
raise "ffmpeg metadata copy failed. cmd=#{ffmpeg_cmd}"
|
|
end
|
|
|
|
FileUtils.mv output_file, input_file
|
|
end
|
|
|
|
def create_mp3
|
|
# add this metadata to mp3 file
|
|
# JamRecordingId=438
|
|
# JamMixId=438
|
|
# JamType=Mix
|
|
input_file = current_path
|
|
output_file = current_path + '.new.mp3'
|
|
ffmpeg_cmd = "#{APP_CONFIG.ffmpeg_path} -i \"#{input_file}\" -ab 128k -metadata JamRecordingId=#{model.recording_id} -metadata JamMixId=#{model.id} -metadata JamType=Mix \"#{output_file}\""
|
|
system(ffmpeg_cmd)
|
|
|
|
unless $? == 0
|
|
raise "ffmpeg mp3 convert failed. cmd=#{ffmpeg_cmd}"
|
|
end
|
|
|
|
model.mp3_md5 = Digest::MD5.file(output_file).hexdigest
|
|
model.mp3_length = File.size(output_file)
|
|
model.mp3_url = model.filename('mp3')
|
|
file.instance_variable_set(:@content_type, 'audio/mpeg')
|
|
|
|
FileUtils.mv output_file, input_file
|
|
end
|
|
|
|
end
|