在Rails中保存视频文件 [英] Saving Video Files in Rails

查看:149
本文介绍了在Rails中保存视频文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Okay, so i've used paperclip in the past to upload images and videos. I was wondering. Is there a simple way to save a video in rails? I've got the form worked out to upload files, and i'm wondering if there is a certain type I should save it as. (Obviously not string, but along those lines.) I simply want to have a video player with all three file types. (ogg, mp4, wav). Just each one saved in their own row in the database.

推荐答案

您可能想看看

You are probably going to want to take a look at paperclip-ffmpeg. I would save the different formats as paperclip styles. This should look very similar to your typical paperclip image upload where you process the image to generate different sizes, like thumbnail for example.

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

Disclaimer: With this way, you will need to install ffmpeg on your server. If you are on an Mac and use homebrew, this link is helpful. http://www.renevolution.com/how-to-install-ffmpeg-on-mac-os-x/

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

In the example below, let's say your file attachment in your database is set to data.

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

Run the migration to add the appropriate columns to your table.

> rails g migration add_data_to_videos data:attachment

然后在您的模型中.

# 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

使用此设置,您的视图将与在图像中使用回形针非常相似.

With this setup, your views will be very similar to using paperclip with images.

# 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.

我还建议您考虑使用后台作业进行处理.也许是延迟工作.

I would also recommend looking into using background jobs to do the processing. Perhaps DelayedJob.

这篇关于在Rails中保存视频文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆