Paperclip支持图片和视频吗?

5
在我的应用程序中,我已经使用了paperclip来上传图像,但需求已更改,需要接受图像和视频。这些图像已经在一个使用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>标签将视频呈现给用户?

你是如何转码视频的? - Richard Peck
1个回答

7

看起来问题在于ImageMagick试图对视频进行转码,但它无法完成。

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

我们的旧代码 & 参考


1
以上代码存在两个问题。第一个问题是在 medium: "300x300>" 之前应该有一个 { 来标记哈希的开始。第二个问题是 processors: [:transcoder] 标志将在所有上传文件上运行转码器,而不仅仅是视频,因此当您将图像发送给 ffmpeg 时,它将失败。 - Eric Smith

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接