Using Paperclip with FFmpeg and multiple output formats: how to store in the database and show in the view a specific output format?
Because I want to use HTML5 video I need to convert videos uploaded by the user into multiple codecs. I am following this tutorial: http://jimneath.org/2008/06/03/converting-videos-with-rails-converting-the-video/ but it only outputs FLV, a format I don't even want at all.
I know I can add more FFmpeg commands an开发者_C百科d convert multiple times, but the problem lies in the database and the view. How can I define multiple source_file_name
s in the DB, and how can I specify in @video.source.url
which format I want? Should I subclass the Video
model, add more table columns for each type or something else?
Paperclip allows you to specify multiple styles. Generally, this is used to specify multiple sizes for uploaded images; Paperclip processes the image once for each style and places them in the filesystem accordingly. By specifying different video formats for your styles and subclassing Paperclip::Processor
, you can create your own video formats. In your model, you'd do something like this:
has_attached_file :video, :styles => { :mpeg, :ogg, :wmv }, :processors => [:my_custom_processor]
And then create a custom Processor that runs the correct FFmpeg command based on each style. See the documentation for more info, but here is a snippet:
Paperclip processors allow you to modify attached files when they are attached in any way you are able. Paperclip itself uses command-line programs for its included Thumbnail processor, but custom processors are not required to follow suit.
Processors are required to be defined inside the Paperclip module and are also required to be a subclass of Paperclip::Processor. There is only one method you must implement to properly be a subclass: #make, but #initialize may also be of use. Both methods accept 3 arguments: the file that will be operated on (which is an instance of File), a hash of options that were defined in has_attached_file’s style hash, and the Paperclip::Attachment itself.
All #make needs to return is an instance of File (Tempfile is acceptable) which contains the results of the processing.
See Paperclip.run for more information about using command-line utilities from within Processors.
When you create a link to a Paperclip attachment, you pass the style to determine which to link to:
<%= link_to "mpeg video", @model.video.url(:mpeg) %>
<%= link_to "ogg video", @model.video.url(:ogg) %>
I suggest you tryout paperclip-ffmpeg gem.
精彩评论