Skip to content
Browse files

add progress option for progress notifications from ffmpeg

  • Loading branch information...
1 parent 7bfea7e commit e7a4a688d131ff441b0a25be2b5560f134d83f36 @svoynow committed
Showing with 27 additions and 2 deletions.
  1. +9 −1 lib/carrierwave/video.rb
  2. +5 −0 lib/carrierwave/video/ffmpeg_options.rb
  3. +13 −1 spec/lib/carrierwave_video_spec.rb
View
10 lib/carrierwave/video.rb
@@ -53,8 +53,16 @@ def encode_video(format, opts={})
yield(file, @options.format_options) if block_given?
+ progress = @options.progress(model)
+
with_trancoding_callbacks do
- file.transcode(tmp_path, @options.format_params, @options.encoder_options)
+ if progress
+ file.transcode(tmp_path, @options.format_params, @options.encoder_options) {
+ |value| progress.call(value)
+ }
+ else
+ file.transcode(tmp_path, @options.format_params, @options.encoder_options)
+ end
File.rename tmp_path, current_path
end
end
View
5 lib/carrierwave/video/ffmpeg_options.rb
@@ -10,6 +10,7 @@ def initialize(format, options)
@callbacks = options[:callbacks] || {}
@logger = options[:logger]
@unparsed = options
+ @progress = options[:progress]
@format_options = defaults.merge(options)
end
@@ -22,6 +23,10 @@ def logger(model)
model.send(@logger) if @logger.present?
end
+ def progress(model)
+ lambda {|val| model.send(@progress, val)}
+ end
+
def encoder_options
{ preserve_aspect_ratio: :width }
end
View
14 spec/lib/carrierwave_video_spec.rb
@@ -1,7 +1,7 @@
require 'spec_helper'
describe CarrierWave::Video do
- class Video; end
+ class Video;end
class TestVideoUploader
include CarrierWave::Video
@@ -150,6 +150,18 @@ def model
end
end
+ context "with progress set" do
+ before do
+ File.should_receive(:rename)
+ movie.stub(:transcode).and_yield(0.0).and_yield(1.0)
+ end
+ it "logs progress" do
+ converter.model.should_receive(:progress).with(0.0)
+ converter.model.should_receive(:progress).with(1.0)
+ converter.encode_video(format, progress: :progress)
+ end
+ end
+
context "with watermark set" do
before { File.should_receive(:rename) }

0 comments on commit e7a4a68

Please sign in to comment.
Something went wrong with that request. Please try again.