carrierwave-video 0.0.2 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -22,7 +22,7 @@ Using bundler:
22
22
  class Video
23
23
  mount_uploader :file, VideoUploader
24
24
 
25
- def set_success
25
+ def set_success(format, opts)
26
26
  self.success = true
27
27
  end
28
28
  end
@@ -46,6 +46,8 @@ Watermark:
46
46
  Callbacks:
47
47
  These are methods defined on the model. They will be run as part of the transcoding process.
48
48
  Useful for notating failure and completion.
49
+ They will be called with the parameters sent to process.
50
+
49
51
  callbacks: {
50
52
  before_transcode: :method
51
53
  after_transcode: :method
@@ -1,5 +1,5 @@
1
1
  module Carrierwave
2
2
  module Video
3
- VERSION = "0.0.2"
3
+ VERSION = "0.1.0"
4
4
  end
5
5
  end
@@ -15,31 +15,30 @@ module CarrierWave
15
15
  # move upload to local cache
16
16
  cache_stored_file! if !cached?
17
17
 
18
- options = CarrierWave::Video::FfmpegOptions.new(format, opts)
18
+ @options = CarrierWave::Video::FfmpegOptions.new(format, opts)
19
19
  tmp_path = File.join( File.dirname(current_path), "tmpfile.#{format}" )
20
20
 
21
21
 
22
- with_trancoding_callbacks(opts) do
22
+ with_trancoding_callbacks do
23
23
  file = ::FFMPEG::Movie.new(current_path)
24
- file.transcode(tmp_path, options.format_options, options.encoder_options)
24
+ file.transcode(tmp_path, @options.format_options, @options.encoder_options)
25
25
  File.rename tmp_path, current_path
26
26
  end
27
27
  end
28
28
 
29
29
  private
30
- def with_trancoding_callbacks(opts, &block)
31
- callbacks = opts[:callbacks] || {}
32
- logger_opt = opts[:logger]
30
+ def with_trancoding_callbacks(&block)
31
+ callbacks = @options.callbacks
32
+ logger = @options.logger(model)
33
33
  begin
34
34
  send_callback(callbacks[:before_transcode])
35
- setup_logger(logger_opt)
35
+ setup_logger
36
36
  block.call
37
37
  send_callback(callbacks[:after_transcode])
38
38
  rescue => e
39
39
  send_callback(callbacks[:rescue])
40
40
 
41
- if logger_opt
42
- logger = model.send(logger_opt)
41
+ if logger
43
42
  logger.error "#{e.class}: #{e.message}"
44
43
  e.backtrace.each do |b|
45
44
  logger.error b
@@ -54,13 +53,13 @@ module CarrierWave
54
53
  end
55
54
 
56
55
  def send_callback(callback)
57
- model.send(callback) if callback.present?
56
+ model.send(callback, @options.format, @options.raw) if callback.present?
58
57
  end
59
58
 
60
- def setup_logger(opt)
61
- return unless opt.present?
59
+ def setup_logger
60
+ return unless @options.logger(model).present?
62
61
  @ffmpeg_logger = ::FFMPEG.logger
63
- ::FFMPEG.logger = model.send(opt)
62
+ ::FFMPEG.logger = @options.logger(model)
64
63
  end
65
64
 
66
65
  def reset_logger
@@ -2,12 +2,16 @@ module CarrierWave
2
2
  module Video
3
3
  class FfmpegOptions
4
4
  attr_reader :watermark_path, :watermark_position, :watermark_pixels,
5
- :format, :resolution
5
+ :format, :resolution, :callbacks
6
6
 
7
7
  def initialize(format, options)
8
8
  @format = format.to_s
9
9
  @watermark = options[:watermark].present?
10
10
  @resolution = options[:resolution] || "640x360"
11
+ @callbacks = options[:callbacks] || {}
12
+ @logger = options[:logger]
13
+ @unparsed = options
14
+
11
15
  if watermark?
12
16
  @watermark_path = options[:watermark][:path]
13
17
  @watermark_position = options[:watermark][:position].to_s || :bottom_right
@@ -15,6 +19,14 @@ module CarrierWave
15
19
  end
16
20
  end
17
21
 
22
+ def raw
23
+ @unparsed
24
+ end
25
+
26
+ def logger(model)
27
+ model.send(@logger) if @logger.present?
28
+ end
29
+
18
30
  def encoder_options
19
31
  {preserve_aspect_ratio: :width}
20
32
  end
@@ -55,22 +55,26 @@ describe CarrierWave::Video do
55
55
 
56
56
  context "callbacks set" do
57
57
  before { movie.should_receive(:transcode) }
58
+ let(:opts) do
59
+ { callbacks: {
60
+ before_transcode: :method1,
61
+ after_transcode: :method2,
62
+ rescue: :method3,
63
+ ensure: :method4
64
+ }
65
+ }
66
+ end
58
67
 
59
68
  context "no exceptions raised" do
60
69
  before { File.should_receive(:rename) }
61
70
 
62
71
  it "calls before_transcode, after_transcode, and ensure" do
63
- converter.model.should_receive(:method1).ordered
64
- converter.model.should_receive(:method2).ordered
72
+ converter.model.should_receive(:method1).with(format, opts).ordered
73
+ converter.model.should_receive(:method2).with(format, opts).ordered
65
74
  converter.model.should_not_receive(:method3)
66
- converter.model.should_receive(:method4).ordered
67
-
68
- converter.encode_video(format, callbacks: {
69
- before_transcode: :method1,
70
- after_transcode: :method2,
71
- rescue: :method3,
72
- ensure: :method4
73
- })
75
+ converter.model.should_receive(:method4).with(format, opts).ordered
76
+
77
+ converter.encode_video(format, opts)
74
78
  end
75
79
  end
76
80
 
@@ -78,19 +82,15 @@ describe CarrierWave::Video do
78
82
  let(:e) { StandardError.new("test error") }
79
83
  before { File.should_receive(:rename).and_raise(e) }
80
84
 
85
+
81
86
  it "calls before_transcode and ensure" do
82
- converter.model.should_receive(:method1).ordered
87
+ converter.model.should_receive(:method1).with(format, opts).ordered
83
88
  converter.model.should_not_receive(:method2)
84
- converter.model.should_receive(:method3).ordered
85
- converter.model.should_receive(:method4).ordered
89
+ converter.model.should_receive(:method3).with(format, opts).ordered
90
+ converter.model.should_receive(:method4).with(format, opts).ordered
86
91
 
87
92
  lambda do
88
- converter.encode_video(format, callbacks: {
89
- before_transcode: :method1,
90
- after_transcode: :method2,
91
- rescue: :method3,
92
- ensure: :method4
93
- })
93
+ converter.encode_video(format, opts)
94
94
  end.should raise_exception(e)
95
95
  end
96
96
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: carrierwave-video
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.1.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -13,7 +13,7 @@ date: 2012-04-11 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rspec
16
- requirement: &70331058547100 !ruby/object:Gem::Requirement
16
+ requirement: &70165811358460 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: '0'
22
22
  type: :development
23
23
  prerelease: false
24
- version_requirements: *70331058547100
24
+ version_requirements: *70165811358460
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: streamio-ffmpeg
27
- requirement: &70331058546500 !ruby/object:Gem::Requirement
27
+ requirement: &70165811357860 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ! '>='
@@ -32,10 +32,10 @@ dependencies:
32
32
  version: '0'
33
33
  type: :runtime
34
34
  prerelease: false
35
- version_requirements: *70331058546500
35
+ version_requirements: *70165811357860
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: carrierwave
38
- requirement: &70331058545760 !ruby/object:Gem::Requirement
38
+ requirement: &70165811357160 !ruby/object:Gem::Requirement
39
39
  none: false
40
40
  requirements:
41
41
  - - ! '>='
@@ -43,7 +43,7 @@ dependencies:
43
43
  version: '0'
44
44
  type: :runtime
45
45
  prerelease: false
46
- version_requirements: *70331058545760
46
+ version_requirements: *70165811357160
47
47
  description: Transcodes to html5-friendly videos.
48
48
  email:
49
49
  - rachelmheaton@gmail.com