2
我有CarrierWave上傳模型: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評論版塊 - 一切正常。
我的問題不在轉換部分。例如,如果我們刪除put_to方法的內容,它將無法按預期工作。 – peanut
是的,這將只是原來的格式嗎? – uday
是的,在這種情況下,原始文件不會被保存:(video.rb中的版本塊甚至無法保存原始文件。 – peanut