图片和视频的回形针

Paperclip for both images and Videos

在我的应用程序中,我已经有了用于上传图片的回形针,但为了同时接受图片和视频,要求发生了变化。这些图像已经在具有 belongs_to 关系的模型中定义,称为 Attachment。由于视频也将是同一父模型(在我的例子中是文章)的附件,我想为图像和视频重用相同的 Attachment 模型。这是个好主意吗?

我的attachment.rb的代码是:

class Attachment < ActiveRecord::Base

  belongs_to :article 

  has_attached_file :url, :s3_protocol => :https , 
    styles: { 
      medium: "300x300>", thumb: "100x100>", big: "1200x1200>", normal: "600x600>" 
    }

  validates_attachment_content_type :url, content_type: /\Aimage|\Avideo\/.*\Z/

  validates_attachment :url, content_type: { content_type: ["image/jpeg", "image/gif", "image/png", "video/mp4"] }

end

但实际上,它并未在 S3 上存储视频。我在终端中得到以下内容

Command :: file -b --mime '/var/folders/v_/pf2bsxnj1y37ccd2pjksv7z80000gn/T/f3e2afc957bb9fb2aa3e77e69359c48920160131-13311-pyeez1.mp4'
Command :: identify -format '%wx%h,%[exif:orientation]' '/var/folders/v_/pf2bsxnj1y37ccd2pjksv7z80000gn/T/8dc7385648e2164764b72fda6fd9099a20160131-13311-1l40ccn.mp4[0]' 2>/dev/null
[paperclip] An error was received while processing: #<Paperclip::Errors::NotIdentifiedByImageMagickError: Paperclip::Errors::NotIdentifiedByImageMagickError>
Command :: identify -format '%wx%h,%[exif:orientation]' '/var/folders/v_/pf2bsxnj1y37ccd2pjksv7z80000gn/T/8dc7385648e2164764b72fda6fd9099a20160131-13311-1l40ccn.mp4[0]' 2>/dev/null
[paperclip] An error was received while processing: #<Paperclip::Errors::NotIdentifiedByImageMagickError: Paperclip::Errors::NotIdentifiedByImageMagickError>
Command :: identify -format '%wx%h,%[exif:orientation]' '/var/folders/v_/pf2bsxnj1y37ccd2pjksv7z80000gn/T/8dc7385648e2164764b72fda6fd9099a20160131-13311-1l40ccn.mp4[0]' 2>/dev/null
[paperclip] An error was received while processing: #<Paperclip::Errors::NotIdentifiedByImageMagickError: Paperclip::Errors::NotIdentifiedByImageMagickError>
Command :: identify -format '%wx%h,%[exif:orientation]' '/var/folders/v_/pf2bsxnj1y37ccd2pjksv7z80000gn/T/8dc7385648e2164764b72fda6fd9099a20160131-13311-1l40ccn.mp4[0]' 2>/dev/null
[paperclip] An error was received while processing: #<Paperclip::Errors::NotIdentifiedByImageMagickError: Paperclip::Errors::NotIdentifiedByImageMagickError>
Command :: file -b --mime '/var/folders/v_/pf2bsxnj1y37ccd2pjksv7z80000gn/T/f3e2afc957bb9fb2aa3e77e69359c48920160131-13311-kvb5nr.mp4'

第二个问题是,由于我在前端使用 AngularJS,我是否可以利用 HTML <video> 标签将视频呈现给用户?

看起来问题是 ImageMagick 正在尝试 transcode 视频,但它不能。

Paperclip 中处理视频转码的方法是使用 paperclip-av-transcoder gem (formerly paperclip-ffmpeg)。我只对后者有经验:

#app/models/attachment.rb
class Attachment < ActiveRecord::Base
    has_attached_file :url, :s3_protocol => :https , 
       styles:  lambda { |a| a.instance.is_image? ?  medium: "300x300>", thumb: "100x100>", big: "1200x1200>", normal: "600x600>" } : {:thumb => { :geometry => "100x100#", :format => 'jpg', :time => 10}, :medium => { :geometry => "300x300#", :format => 'jpg', :time => 10}}}, processors: [:transcoder]

    validates_attachment :url,
        content_type: ['video/mp4'],
        message:      "Sorry, right now we only support MP4 video",
        if:           :is_video?

    validates_attachment :url,
        content_type: ['image/png', 'image/jpeg', 'image/jpg', 'image/gif'],
        message:      "Different error message",
        if:           :is_image?

  private

  def is_video?
    url.instance.attachment_content_type =~ %r(video)
  end

  def is_image?
    url.instance.attachment_content_type =~ %r(image)
  end
end

Our old code & ref