Skip to content

Commit

Permalink
add seeking support for creating thumbnails of video
Browse files Browse the repository at this point in the history
  • Loading branch information
attenzione committed Mar 18, 2015
1 parent b8f777e commit 72d8891
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 7 deletions.
10 changes: 10 additions & 0 deletions lib/paperclip/paperclip_processors/transcoder.rb
Expand Up @@ -52,6 +52,12 @@ def make
@cli.add_source(@file.path)
@cli.add_destination(dst.path)
@cli.reset_input_filters

if output_is_image?
@time = @time.call(@meta, @options) if @time.respond_to?(:call)
@cli.filter_seek @time
end

if @convert_options.present?
if @convert_options[:input]
@convert_options[:input].each do |h|
Expand Down Expand Up @@ -94,6 +100,10 @@ def format_geometry geometry
return unless geometry.present?
return geometry.gsub(/[#!<>)]/, '')
end

def output_is_image?
!!@format.to_s.match(/jpe?g|png|gif$/)
end
end

class Attachment
Expand Down
8 changes: 6 additions & 2 deletions spec/spec_helper.rb
Expand Up @@ -41,6 +41,10 @@ class Document < ActiveRecord::Base
ac: 2
}
}
},
thumb: {
format: 'jpg',
time: 0
}
},
processors: [:transcoder]
Expand All @@ -53,8 +57,8 @@ class Document < ActiveRecord::Base
styles: {
small: "100x100"
},
processors: [:transcoder, :thumbnail]
processors: [:transcoder, :thumbnail]

do_not_validate_attachment_file_type :video
do_not_validate_attachment_file_type :image
end
11 changes: 6 additions & 5 deletions spec/transcoder/transcoder_spec.rb
Expand Up @@ -3,24 +3,25 @@
describe Paperclip::Transcoder do
let(:supported) { File.new(Dir.pwd + '/spec/support/assets/sample.mp4') }
let(:unsupported) { File.new(File.expand_path('spec/support/assets/image.png')) }

let(:destination) { Pathname.new("#{Dir.tmpdir}/transcoder/") }

describe "supported formats" do
let(:subject) { Paperclip::Transcoder.new(supported) }
let(:document) { Document.create(video: Rack::Test::UploadedFile.new(supported, 'video/mp4')) }

describe ".transcode" do
it { expect(File.exists?(document.video.path(:small))).to eq true }
it { expect(File.exists?(document.video.path(:thumb))).to eq true }
end
end

describe "unsupported formats" do
let(:subject) { Paperclip::Transcoder.new(unsupported) }
let(:document) { Document.create(image: Rack::Test::UploadedFile.new(unsupported, 'image/png')) }
describe ".transcode" do
it { expect(File.exists?(document.image.path(:small))).to eq true }
end
end

end

0 comments on commit 72d8891

Please sign in to comment.