使用回形针播放视频 rails

Video playback with paperclip rails

我想设置视频上传,以便用户可以查看和播放它们。我是 rails 的新手,有人知道怎么做吗?

你可以看看Paperclip

在包含视频的模型中,您需要正确的格式 (ffmpeg) 和视频验证:

   has_attached_file :attachment,
          styles: lambda { |a| a.instance.is_image? ? {:small => "x200>", :medium => "x300>", :large => "x400>"}  : {:thumb => { :geometry => "100x100#", :format => 'jpg', :time => 10}, :medium => { :geometry => "300x300#", :format => 'jpg', :time => 10}}},
          :processors => lambda { |a| a.is_video? ? [ :ffmpeg ] : [ :thumbnail ] }
    validates_attachment_content_type :video, content_type: /\Avideo\/.*\Z/ 

评论更新:

 has_attached_file :video, styles: {
    :medium => {
      :geometry => "640x480",
      :format => 'mp4'
    },
    :thumb => { :geometry => "160x120", :format => 'jpeg', :time => 10}
  }, :processors => [:transcoder]

有一些实时代码可以实现这个 here:

#Gemfile
gem "paperclip-ffmpeg", "~> 1.2.0"

#app/models/attachment.rb
class Attachment < ActiveRecord::Base

   has_attached_file :attachment,
        styles: {:thumb => { geometry: "100x100#", format: 'jpg', time: 10}, medium: { gemometry: "300x300#", format: 'jpg', time: 10}},
        processors: [ :ffmpeg ]

end

你遇到的主要问题是调用 preprocessing

当您使用 Paperclip 上传任何附件时,它必须在存储之前对其进行处理。处理转换为正确的格式并调整它的大小(你也可以用它做其他很酷的事情)。

视频处理与图像处理略有不同,您首先必须处理视频以便您的服务器可以播放它,然后您还必须从中提取一些屏幕截图。

因此,您需要一个自定义处理器,@Czanfar 在他的回答中发布的是 ffmpegtranscoder

之间的选择

我也在这里写了一个答案: Rails video uploading


顺便说一句,这是我去见制作回形针的人的时候:

感谢 Cyzanfars 的回答,我开始工作了!

Make sure you install ffmpeg

brew install ffmpeg --with-fdk-aac --with-ffplay --with-freetype --with-frei0r --with-libass --with-libvo-aacenc --with-libvorbis --with-libvpx --with-opencore-amr --with-openjpeg --with-opus --with-rtmpdump --with-schroedinger --with-speex --with-theora --with-tools