2012-02-03 35 views
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擴展名。但這是一個圖像可以在瀏覽器中查看。

如何解決擴展問題?任何人都可以在代碼中指出問題嗎?

回答

2

我最近解決了這個通過重寫full_filename方法爲:thumb版本

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文件名,然後改變了從擴展到mp4png,但你可以做任何事情。

欲瞭解更多信息,carrierwave wiki有一篇關於How to: Customize your version file names的好文章。查看其他維基頁面的許多想法。

相關問題