Tamam, bu muhtemelen uygun değil ama oldukça iyi çalışıyor. Ben mp3 benim Sound
sınıfa Başka bir ek ekleyerek sona erdi ve onun içine kanca bir before_validation
filtre ekledi. Ayrıca, mevcut bazı ekleri olduğundan, mevcut kayıtların taşınması için reconvert_to_mp3
yöntemini oluşturdum.
has_attached_file :mp3,
:storage => :s3,
:s3_credentials => "#{RAILS_ROOT}/config/s3.yml",
:path => "sounds/:id/:style.:extension"
before_validation :convert_to_mp3
def reconvert_to_mp3
wavfile = Tempfile.new(".wav")
wavfile.binmode
open(wav.url) do |f|
wavfile << f.read
end
wavfile.close
convert_tempfile(wavfile)
end
def convert_to_mp3
tempfile = wav.queued_for_write[:original]
unless tempfile.nil?
convert_tempfile(tempfile)
end
end
def convert_tempfile(tempfile)
dst = Tempfile.new(".mp3")
cmd_args = [File.expand_path(tempfile.path), File.expand_path(dst.path)]
system("lame", *cmd_args)
dst.binmode
io = StringIO.new(dst.read)
dst.close
io.original_filename = "sound.mp3"
io.content_type = "audio/mpeg"
self.mp3 = io
end
Altta bir "dst.unlink" olmaması gerekir mi? Ya da Paperclip'in yakalayabilmesi için dosyayı siler misiniz? – user1618143
@daniel Çözüm için teşekkürler, harika çalışıyor. Ama üretim modunda bir sorunla karşı karşıyayım. mp3 Girdi dosyasında 'okuma hatası başlıkları' Bana bunda yardım eder misiniz şöyledir: Hata mı? Çok teşekkürler. – Vishal