diff --git a/README.md b/README.md index 62896f0..6a2673c 100644 --- a/README.md +++ b/README.md @@ -10,6 +10,7 @@ This is a replacement for ( https://github.com/owahab/paperclip-ffmpeg ). [![Coverage Status](https://coveralls.io/repos/ruby-av/paperclip-av-transcoder/badge.png?branch=master)](https://coveralls.io/r/ruby-av/paperclip-av-transcoder?branch=master) [![Code Climate](https://codeclimate.com/github/ruby-av/paperclip-av-transcoder/badges/gpa.svg)](https://codeclimate.com/github/ruby-av/paperclip-av-transcoder) [![Dependency Status](https://gemnasium.com/ruby-av/paperclip-av-transcoder.svg)](https://gemnasium.com/ruby-av/paperclip-av-transcoder) +[![Gem Version](https://badge.fury.io/rb/paperclip-av-transcoder.svg)](http://badge.fury.io/rb/paperclip-av-transcoder) ## Installation @@ -42,15 +43,47 @@ This will produce: 1. A transcoded `:medium` FLV file with the requested dimensions if they will match the aspect ratio of the original file, otherwise, width will be maintained and height will be recalculated to keep the original aspect ration. 2. A screenshot `:thumb` with the requested dimensions regardless of the aspect ratio. -You may optionally add `_meta` to your model and it will get populated with information about the processed video. +### Meta Data -The geometry parameters are: +Then paperclip-av-transcoder can optionally add uploaded file meta data to a database column for `_meta`. + +Example: Given a model called `User` with an attachment field named `:avatar`, create a new migration to add an `avatar_meta` column to the `users` table. +``` +def change + add_column :users, :avatar_meta, :data_type +end +``` +You can use a data type of `:json`, `:jsonb`, `:hstore` or even just `:string`. Check what data types your database supports. + +### `geometry` + +The `geometry` option has the following available modifiers: 1. '!' - Keep the same aspect of the image/video, but with the passed dimesion. 2. '#' - Pad the image/video. 3. '<' - Enlarge the image/video. 4. '>' - Shrink the image/video. +### `convert_options` + +The `convert_options` option lets you specify custom command line options to be sent to the `ffmpeg` command. The options are split into `output` and `input`, which define where in the pipeline they will be applied. Read more about which flags go where on the [official documentation](https://ffmpeg.org/ffmpeg.html). + +For example, sending in the `-an` flag would look like this: + +```ruby +has_attached_file :video, styles: { + mobile: { + format: "mp4", + convert_options: { + output: { + an: nil # Remove audio track resulting in a silent movie, passing in nil results in `-an`, + name: "value" # Results in `-name value` in the command line + } + } + }, +} +``` + ## Contributing 1. Fork it ( https://github.com/ruby-av/paperclip-av-transcoder/fork ) diff --git a/lib/paperclip/av/transcoder/version.rb b/lib/paperclip/av/transcoder/version.rb index a212f99..ce319b6 100644 --- a/lib/paperclip/av/transcoder/version.rb +++ b/lib/paperclip/av/transcoder/version.rb @@ -1,7 +1,7 @@ module Paperclip module Av module Transcoder - VERSION = "0.6.2" + VERSION = "0.6.4" end end end diff --git a/lib/paperclip/paperclip_processors/transcoder.rb b/lib/paperclip/paperclip_processors/transcoder.rb index 9ebce7e..d67efea 100755 --- a/lib/paperclip/paperclip_processors/transcoder.rb +++ b/lib/paperclip/paperclip_processors/transcoder.rb @@ -14,10 +14,11 @@ def initialize file, options = {}, attachment = nil @cli = ::Av.cli @meta = ::Av.cli.identify(@file.path) @whiny = options[:whiny].nil? ? true : options[:whiny] - - @convert_options = options[:convert_options] + + @convert_options = set_convert_options(options) + @format = options[:format] - + @geometry = options[:geometry] unless @geometry.nil? modifier = @geometry[0] @@ -28,14 +29,16 @@ def initialize file, options = {}, attachment = nil @enlarge_only = @keep_aspect && modifier == '<' @shrink_only = @keep_aspect && modifier == '>' end - + @time = options[:time].nil? ? 3 : options[:time] @auto_rotate = options[:auto_rotate].nil? ? false : options[:auto_rotate] @pad_color = options[:pad_color].nil? ? "black" : options[:pad_color] - + + @convert_options[:output][:s] = format_geometry(@geometry) if @geometry.present? + attachment.instance_write(:meta, @meta) if attachment end - + # Performs the transcoding of the +file+ into a thumbnail/video. Returns the Tempfile # that contains the new image/video. def make @@ -43,12 +46,19 @@ def make @cli.add_source @file dst = Tempfile.new([@basename, @format ? ".#{@format}" : '']) dst.binmode - + if @meta - log "Transocding supported file #{@file.path}" + log "Transcoding supported file #{@file.path}" @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 + @cli.filter_rotate @meta[:rotate] if @auto_rotate && !@meta[:rotate].nil? + end + if @convert_options.present? if @convert_options[:input] @convert_options[:input].each do |h| @@ -76,10 +86,25 @@ def make end dst end - + def log message Paperclip.log "[transcoder] #{message}" end + + def set_convert_options options + return options[:convert_options] if options[:convert_options].present? + options[:convert_options] = {output: {}} + return options[:convert_options] + end + + 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 diff --git a/paperclip-av-transcoder.gemspec b/paperclip-av-transcoder.gemspec index 1dcebd9..c62aa5c 100644 --- a/paperclip-av-transcoder.gemspec +++ b/paperclip-av-transcoder.gemspec @@ -24,7 +24,7 @@ Gem::Specification.new do |spec| spec.add_development_dependency "rails", ">= 4.0.0" spec.add_development_dependency "sqlite3" spec.add_development_dependency "coveralls" - + spec.add_dependency "paperclip", ">=2.5.2" - spec.add_dependency "av", ">= 0.7.1" + spec.add_dependency "av", "~> 0.9.0" end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index ed48510..1735ebf 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -41,6 +41,10 @@ class Document < ActiveRecord::Base ac: 2 } } + }, + thumb: { + format: 'jpg', + time: 0 } }, processors: [:transcoder] @@ -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 \ No newline at end of file diff --git a/spec/transcoder/transcoder_spec.rb b/spec/transcoder/transcoder_spec.rb index 3468168..33ff890 100644 --- a/spec/transcoder/transcoder_spec.rb +++ b/spec/transcoder/transcoder_spec.rb @@ -3,18 +3,19 @@ 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')) } @@ -22,5 +23,5 @@ it { expect(File.exists?(document.image.path(:small))).to eq true } end end - + end \ No newline at end of file