Saving Video Files in Rails

…衆ロ難τιáo~ 提交于 2019-12-24 01:44:15

问题


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.


回答1:


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.

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/

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

Then in your model.

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



来源:https://stackoverflow.com/questions/26132806/saving-video-files-in-rails

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!