Skip to content

Commit 72d8891

Browse files
committed
add seeking support for creating thumbnails of video
1 parent b8f777e commit 72d8891

File tree

3 files changed

+22
-7
lines changed

3 files changed

+22
-7
lines changed

lib/paperclip/paperclip_processors/transcoder.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,12 @@ def make
5252
@cli.add_source(@file.path)
5353
@cli.add_destination(dst.path)
5454
@cli.reset_input_filters
55+
56+
if output_is_image?
57+
@time = @time.call(@meta, @options) if @time.respond_to?(:call)
58+
@cli.filter_seek @time
59+
end
60+
5561
if @convert_options.present?
5662
if @convert_options[:input]
5763
@convert_options[:input].each do |h|
@@ -94,6 +100,10 @@ def format_geometry geometry
94100
return unless geometry.present?
95101
return geometry.gsub(/[#!<>)]/, '')
96102
end
103+
104+
def output_is_image?
105+
!!@format.to_s.match(/jpe?g|png|gif$/)
106+
end
97107
end
98108

99109
class Attachment

spec/spec_helper.rb

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,10 @@ class Document < ActiveRecord::Base
4141
ac: 2
4242
}
4343
}
44+
},
45+
thumb: {
46+
format: 'jpg',
47+
time: 0
4448
}
4549
},
4650
processors: [:transcoder]
@@ -53,8 +57,8 @@ class Document < ActiveRecord::Base
5357
styles: {
5458
small: "100x100"
5559
},
56-
processors: [:transcoder, :thumbnail]
57-
60+
processors: [:transcoder, :thumbnail]
61+
5862
do_not_validate_attachment_file_type :video
5963
do_not_validate_attachment_file_type :image
6064
end

spec/transcoder/transcoder_spec.rb

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,24 +3,25 @@
33
describe Paperclip::Transcoder do
44
let(:supported) { File.new(Dir.pwd + '/spec/support/assets/sample.mp4') }
55
let(:unsupported) { File.new(File.expand_path('spec/support/assets/image.png')) }
6-
6+
77
let(:destination) { Pathname.new("#{Dir.tmpdir}/transcoder/") }
8-
8+
99
describe "supported formats" do
1010
let(:subject) { Paperclip::Transcoder.new(supported) }
1111
let(:document) { Document.create(video: Rack::Test::UploadedFile.new(supported, 'video/mp4')) }
12-
12+
1313
describe ".transcode" do
1414
it { expect(File.exists?(document.video.path(:small))).to eq true }
15+
it { expect(File.exists?(document.video.path(:thumb))).to eq true }
1516
end
1617
end
17-
18+
1819
describe "unsupported formats" do
1920
let(:subject) { Paperclip::Transcoder.new(unsupported) }
2021
let(:document) { Document.create(image: Rack::Test::UploadedFile.new(unsupported, 'image/png')) }
2122
describe ".transcode" do
2223
it { expect(File.exists?(document.image.path(:small))).to eq true }
2324
end
2425
end
25-
26+
2627
end

0 commit comments

Comments
 (0)