我有带有 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 版本?
还有一件事,当我在 video_uploader.rb 中评论版本块时 - 一切正常。