在我的"wave_uploader.rb“脚本中,我有以下代码:
class PictureUploader < CarrierWave::Uploader::Base
include CarrierWave::MiniMagick
include CarrierWave::MimeTypes
version :wav do
process :convert_to_mp3
def convert_to_mp3
temp_path = Tempfile.new([File.basename(current_path), '.mp3']).path
`ffmpeg -t 15 -i #{current_path} -acodec libmp3lame -f mp3 #{temp_path}`
File.unlink(current_path)
FileUtils.mv(temp_path, current_path)
end
def full_filename(for_file)
super.chomp(File.extname(super)) + '.mp3'
end
end我正在尝试将WAV文件转换为20秒的MP3文件,并在转换后删除WAV文件。上面的代码运行,但我找不到转换的MP3文件,所以我猜它没有正确工作。
在wave_uploader.rb的末尾,我的代码一旦被处理就返回唯一的名称,但是我把代码注释掉了,认为下面的代码导致WAV文件没有被转换成MP3。
# def filename
# "#{secure_token}.#{file.extension}" if original_filename.present?
# end
# def secure_token
# var = :"@#{mounted_as}_secure_token"
# model.instance_variable_get(var) or model.instance_variable_set(var, SecureRandom.uuid)
end任何帮助,将非常感谢如何使这一工作正确。
发布于 2015-10-01 17:11:28
我看到的一件事是:
`ffmpeg -t 15 -i #{current_path} -acodec libmp3lame -f mp3 #{temp_path}`如果ffmpeg不在您的路径中,那么操作系统将无法找到它,并将返回一个错误,但是,因为您使用的是回退,操作系统无法从STDERR返回一个字符串,这将显示错误。回退只返回STDOUT。
要调试这一点,请从命令行中尝试:
which ffmpeg如果找到了ffmpeg,而不是:
`ffmpeg -t 15 -i #{current_path} -acodec libmp3lame -f mp3 #{temp_path}`尝试:
puts `which ffmpeg`看看什么是输出。
我怀疑它不在您的路径中,所以您必须找到它的位置,并提供到磁盘上的位置的完整路径。
另外,最好是移动原始文件,将新文件移动到原始文件的名称,然后删除原始文件或将其保留为".bak“文件。这样,原始代码将一直保存到所有代码处理完毕为止:
FileUtils.mv(current_path, current_path + '.bak')
FileUtils.mv(temp_path, current_path)
File.unlink(current_path + '.bak') # <-- optionalhttps://stackoverflow.com/questions/32877388
复制相似问题