2012-02-03 5 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

私は最近: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 

ためfull_filenameメソッドをオーバーライドすることによってこれを解決し、私はデフォルト:thumbのファイル名を取得するためにsuperと呼ばれ、その後、pngmp4から拡張子を変更しましたが、あなたは何を行うことができます。

詳細については、carrierwave wikiは、How to: Customize your version file namesに関する良い記事があります。他のwikiページでたくさんのアイデアをチェックしてください。

関連する問題