使用带有 carrier_wave gem 的 FFMPEG 将 WAV 转换为 MP3
Convert WAV into MP3 using FFMPG with the carrier_wave gem
在我的 "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 的末尾,我有代码 returns 处理后的唯一名称,但我注释掉了代码,认为下面的代码导致 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
如果您能提供有关如何使这项工作正常进行的任何帮助,我们将不胜感激。
我看到的一件事是:
`ffmpeg -t 15 -i #{current_path} -acodec libmp3lame -f mp3 #{temp_path}`
如果 ffmpeg
不在您的路径中,那么 OS 将无法找到它,并且 return 会出错,但是,因为您正在使用反引号,OS 不能 return 来自 STDERR 的字符串,这是显示错误的地方。仅反引号 return 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') # <-- optional
在我的 "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 的末尾,我有代码 returns 处理后的唯一名称,但我注释掉了代码,认为下面的代码导致 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
如果您能提供有关如何使这项工作正常进行的任何帮助,我们将不胜感激。
我看到的一件事是:
`ffmpeg -t 15 -i #{current_path} -acodec libmp3lame -f mp3 #{temp_path}`
如果 ffmpeg
不在您的路径中,那么 OS 将无法找到它,并且 return 会出错,但是,因为您正在使用反引号,OS 不能 return 来自 STDERR 的字符串,这是显示错误的地方。仅反引号 return 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') # <-- optional