5

在我的应用程序中,我已经有用于上传图像的回形针,但为了接受图像和视频,要求发生了变化。图像已经在具有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>标签将视频呈现给用户吗?

4

1 回答 1

7

看起来问题是ImageMagick试图对视频进行转码,但它不能。

处理内部视频转码的方法Paperclip是使用paperclip-av-transcodergem(以前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

我们的旧代码参考

于 2016-01-31T22:00:11.923 回答