4
我想上傳文件併爲其轉換縮略圖。如何使用CarrierWave更正電影縮略圖生成的文件擴展名
我的代碼是:
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擴展名。但這是一個圖像可以在瀏覽器中查看。
如何解決擴展問題?任何人都可以在代碼中指出問題嗎?