Ok, esto probablemente no es óptimo, pero funciona bastante bien. Terminé agregando otro archivo adjunto a mi clase Sound
para el mp3, y agregué un filtro before_validation
para conectarlo. Además, como tenía algunos archivos adjuntos de wav, creé un método reconvert_to_mp3
para manejar la migración de los registros existentes.
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
¿No debería haber un 'dst.unlink' en la parte inferior? ¿O eso eliminaría el archivo antes de que Paperclip pueda atraparlo? – user1618143
@daniel Gracias por la solución está funcionando genial. Pero estoy enfrentando un problema en el modo de producción. El error es el siguiente: 'Error al leer los encabezados en el archivo de entrada mp3' ¿Podría ayudarme con esto? Muchas gracias. – Vishal