我想上传文件并转换缩略图.
我的代码是:
require 'streamio-ffmpeg'
module CarrierWave
module FFMPEG
module ClassMethods
def resample(bitrate)
process :resample => bitrate
end
def gen_video_thumb(width, height)
process :gen_video_thumb => [width, height]
end
end
#def is_video?
# ::FFMPEG::Movie.new(File.open(store_path)).frame_rate != nil
#end
def gen_video_thumb(width, height)
directory = File.dirname(current_path)
tmpfile = File.join(directory, "tmpfile")
FileUtils.move(current_path, tmpfile)
file = ::FFMPEG::Movie.new(tmpfile)
file.transcode(current_path, "-ss 00:00:01 -an -r 1 -vframes 1 -s #{width}x#{height}")
FileUtils.rm(tmpfile)
end
def resample(bitrate)
directory = File.dirname(current_path)
tmpfile = File.join(directory, "tmpfile")
File.move(current_path, tmpfile)
file = ::FFMPEG::Movie.new(tmpfile)
file.transcode(current_path, :audio_bitrate => bitrate)
File.delete(tmpfile)
end
end
end
我的上传者有
version :thumb do
process :resize_to_fill => [100, 70], :if=> :image?
process :gen_video_thumb => [100, 70], :if=> :video? do
process :convert => 'png'
end
end
和功能是.
protected
def image?(new_file)
::FFMPEG::Movie.new(new_file.file.path).frame_rate == nil
end
def video?(new_file)
::FFMPEG::Movie.new(new_file.file.path).frame_rate != nil
end
但问题是,视频上传,视频thubmail生成非常好.但它没有png扩展名.如果我上传一个mp4文件,它的缩略图也有一个mp4扩展名.但这是一个可以在浏览器中查看的图像.
如何纠正扩展问题?任何人都可以在代码中指出问题吗?
最佳答案 我最近通过覆盖:thumb版本的full_filename方法解决了这个问题
version :thumb do
# do your processing
process :whatever
# redefine the name for this version
def full_filename(for_file=file)
super.chomp('mp4') + 'png'
end
end
我调用super来获取默认值:thumb filename,然后将扩展名从mp4更改为png,但你可以做任何事情.
有关更多信息,carrierwave wiki在How to: Customize your version file names上有一篇很好的文章.查看其他维基页面了解很多想法.