我正在尝试使用回形针将mp3上传到S3,但是当我尝试使用自定义处理器创建质量较低的音频文件时,我遇到了问题。
它似乎有效,但不是在128k输出整首歌,而是只用前6秒。 (当我尝试在命令行使用ffmpeg时,它工作正常)并且它为两个“样式”而不仅仅是片段样式,原始处理也是我不想要的。
class Song < ActiveRecord::Base
belongs_to :record
#after_save :create_metadata
Paperclip.interpolates :record_id do |attachment, style|
attachment.instance.record_id
end
has_attached_file :mp3,
:styles => {
:snippet => { :processors => [:audio_prehear] }
},
:storage => 's3',
:s3_credentials => "#{RAILS_ROOT}/config/s3_credentials.yml"....
module Paperclip
class AudioPrehear < Processor
attr_accessor :resolution, :whiny
def initialize(file, options = {}, attachment = nil)
super
@file = file
@whiny = options[:whiny].nil? ? true : options[:whiny]
@basename = File.basename(@file.path, File.extname(@file.path))
end
def make
target = File.dirname(@file.path) + "/" + @basename + ".mp3"
convert File.expand_path(file.path), target
dst = File.open target
end
def convert (infile, outfile)
cmd = "-y -i #{infile} -ab 128k #{outfile}"
begin
success = Paperclip.run('ffmpeg', cmd)
rescue PaperclipCommandLineError
raise PaperclipError, "There was an error processing the preview for #{@basename}" if whiny
end
end
end
end
[paperclip] ffmpeg -y -i /var/folders/Ue/UehOEbyxHkS9voyeIiUx3++++TI/-Tmp-/stream20101223-93966-18vy7po-0.mp3 -ab 128k /var/folders/Ue/UehOEbyxHkS9voyeIiUx3++++TI/-Tmp-/stream20101223-93966-18vy7po-0.mp3 2>/dev/null
....
[paperclip] Saving attachments.
[paperclip] saving music/7635/snippet/monkey.mp3
[paperclip] saving music/7635/original/monkey.mp3
....