在 Rails 中保存视频文件

And*_*Ice 2 video ruby-on-rails file attachment

好的,所以我过去使用回形针上传图像和视频。我想知道。有没有一种简单的方法可以在 Rails 中保存视频?我已经制定了上传文件的表格,我想知道是否应该将其另存为某种类型。(显然不是字符串,而是沿着这些线。)我只想拥有一个包含所有三种文件类型的视频播放器。(ogg、mp4、wav)。只是每个人都保存在数据库中自己的行中。

Jus*_*tin 5

您可能想要看看paperclip-ffmpeg。我会将不同的格式保存为回形针样式。这应该看起来与典型的回形针图像上传非常相似,您可以在其中处理图像以生成不同大小的图像,例如缩略图

免责声明:通过这种方式,您需要在您的服务器上安装 ffmpeg。如果您使用的是 Mac 并使用自制软件,则此链接很有帮助。http://www.renevolution.com/how-to-install-ffmpeg-on-mac-os-x/

在下面的示例中,假设您的数据库中的文件附件设置为data.

运行迁移以将适当的列添加到您的表中。

> rails g migration add_data_to_videos data:attachment
Run Code Online (Sandbox Code Playgroud)

然后在你的模型中。

# Validations
validates_attachment_presence :data
validates_attachment_size :data, less_than: 100.megabytes # if you want a max file size
validates_attachment_content_type :data, content_type: /\Avideo\/.*\Z/ # or you can specify individual content types you want to allow

has_attached_file :data,
  url: '/videos/:id/:style/:basename.:extension', # whatever you want
  styles: {
    poster: { size: '640x480', format: 'jpg' }, # this takes a snapshot of the video to have as your poster
    v_large_webm: { geometry: '640x480', format: 'webm' }, # your webm format
    v_large_mp4: { geometry: '640x480', format: 'mp4' } # your mp4 format
  },
  processors: [:ffmpeg],
  auto_rotate: true
Run Code Online (Sandbox Code Playgroud)

使用此设置,您的视图将与使用带有图像的回形针非常相似。

# To display the video (HTML5 way using HAML)
%video{controls: '', height: 'auto', width: '100%', poster: @video.data.url(:poster)}
  %source{src: @video.data.url(:v_large_mp4), type: 'video/mp4'}
  %source{src: @video.data.url(:v_large_webm), type: 'video/webm'}
    Your browser does not support the video tag.
Run Code Online (Sandbox Code Playgroud)

我还建议考虑使用后台作业进行处理。也许是DelayedJob。