2

CarrierWave アップローダを備えたモデルがあります。

# app/models/video.rb

class Video < ActiveRecord::Base

  mount_uploader :the_video, VideoUploader

end

アップローダーは次のようになります。

# app/uploaders/video_uploader.rb

class VideoUploader < CarrierWave::Uploader::Base
  include CarrierWave::FLV

  storage :file

  def move_to_cache
    true
  end

  def move_to_store
    true
  end

  def store_dir
    "uploads/#{model.class.to_s.underscore}/#{model.id}"
  end

  def extension_white_list
    %w(avi mp4 mpg flv)
  end

  version :flv do
    process :put_to => 'flv'
  end

end

そして最も興味深い部分 - カスタム プロセッサ:

# lib/carrier_wave/flv.rb

require 'streamio-ffmpeg'

module CarrierWave
  module FLV
    extend ActiveSupport::Concern

    module ClassMethods
      def put_to(format)
        process :put_to => format
      end
    end

    def put_to(format = 'flv')
      directory = File.dirname(current_path)
      tmp_path  = File.join(directory, "tmpfile")

      File.rename current_path, tmp_path

      file = ::FFMPEG::Movie.new(tmp_path)
      file.transcode(current_path, {audio_codec: 'copy', video_codec: 'copy'})

      File.delete tmp_path
    end

  end
end

次のステップ:

irb(main):005:0> v = Video.new
irb(main):005:0> v.the_video = File.open('/path/to/video.mp4')
irb(main):005:0> v.save
irb(main):005:0> v.the_video.size
=> 0
irb(main):005:0> v.the_video.flv.size
=> 0

CarrierWave がオリジナルと flv バージョンを保存しなかったのはなぜですか?

もう 1 つ、video_uploader.rb のバージョン ブロックにコメントすると、すべて正常に動作します。

4

1 に答える 1