Ruby 使用回形针在文件系统和S3上保存附件
我正在使用wav-mp3的变体作为我的回形针处理器,我需要能够在本地和远程保存附件。这是处理器:Ruby 使用回形针在文件系统和S3上保存附件,ruby,ruby-on-rails-3,amazon-s3,paperclip,Ruby,Ruby On Rails 3,Amazon S3,Paperclip,我正在使用wav-mp3的变体作为我的回形针处理器,我需要能够在本地和远程保存附件。这是处理器: module Paperclip class Ffmpeg < Processor attr_accessor :file, :params, :format def initialize file, options = {}, attachment = nil super @file = file @para
module Paperclip
class Ffmpeg < Processor
attr_accessor :file, :params, :format
def initialize file, options = {}, attachment = nil
super
@file = file
@params = options[:params]
@current_format = File.extname(@file.path)
@basename = File.basename(@file.path, @current_format)
@format = options[:format]
end
def make
src = @file
dst = Tempfile.new([@basename, @format ? ".#{@format}" : ''])
begin
parameters = []
parameters << @params
parameters << ":source"
parameters << ":dest"
parameters = parameters.flatten.compact.join(" ").strip.squeeze(" ")
cmd = "-y -i #{File.expand_path(src.path)} #{File.expand_path(dst.path)} -acodec mp3"
success = Paperclip.run("ffmpeg", cmd)
# This was an idea but I don't know how to access the Tempfile's name from the controller
FileUtils.copy_file(File.expand_path(dst.path), "#{Rails.root}/tmp/#{File.basename(src.path)}" )
rescue PaperclipCommandLineError => e
raise PaperclipError, "There was an error converting #{@basename} to mp3"
end
dst
end
end
end
模块回形针
类Ffmpeg<处理器
属性访问器:文件,:参数,:格式
def初始化文件,选项={},附件=nil
超级的
@file=file
@params=选项[:params]
@当前_格式=File.extname(@File.path)
@basename=File.basename(@File.path,@current\u格式)
@格式=选项[:格式]
结束
def制造
src=@file
dst=Tempfile.new([@basename,@format?”。#{@format}”:“”)
开始
参数=[]
参数
class AudioFile < ActiveRecord::Base
establish_connection "audio_file"
has_attached_file :wav,
:styles => {
:mp3 => {
#:params => "-encode --scale 1 --vbr-new -V7",
#:params => "-i
:format => "mp3" }
},
:default_style => :normal,
:processors => [:ffmpeg],
:storage => :s3,
:s3_credentials => "#{Rails.root}/config/s3.yml",
:path => "/call_recordings/:filename"
after_post_process :get_duration
def get_duration
# This what I'm ultimately trying to do:
#self.duration = `mp3info -p "%S" #{File.expand_path(dst.path)}`
end
end