Skip to content

Commit

Permalink
add progress option for progress notifications from ffmpeg
Browse files Browse the repository at this point in the history
  • Loading branch information
svoynow committed Aug 7, 2013
1 parent 7bfea7e commit e7a4a68
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 2 deletions.
10 changes: 9 additions & 1 deletion lib/carrierwave/video.rb
Expand Up @@ -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
Expand Down
5 changes: 5 additions & 0 deletions lib/carrierwave/video/ffmpeg_options.rb
Expand Up @@ -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
Expand All @@ -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
Expand Down
14 changes: 13 additions & 1 deletion 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
Expand Down Expand Up @@ -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) }

Expand Down

0 comments on commit e7a4a68

Please sign in to comment.