Converting video to flash and mp4 ruby on rails?
FFMPEG is really what you need here.
As more jobs you'll send to it, as more it will eat of your RAM. But you can organize simple queue and background job for converting video.
I have never used gems for it but you can look this list:
- http://rubygems.org/gems/ffmpeg-ruby ( https://github.com/hackerdude/ffmpeg-ruby )
- or this fork http://github.com/gwik/ffmpeg-ruby
- http://rvideo.rubyforge.org/
Converting Videos In the Background ROR 3
First of all check this answer. For background jobs you want to use sidekiq. You'll need method that'll handle convertion. That method you'll call inside sidekiq's #perform. Inside this method you also can change states(just by changing string field value).
Video format or MIME type is not supported in ruby on rails
HTML <video>
tag (and thus the Rails video_tag
) does not support video/flv
format. You need to use some Flash-based player to play it. Or, better, transcode the video to MP4
or OGG
format.
how to load and validate the video using paperclip-Rails
Working with image
with paperclip
and with video
is different.Though the common point is that it will help you tu send the uploaded data to the server but you need to handle it with some processor.Just like you use ImageMagick
for images
,you should use FFMPEG
to encode/decode video
.
i will share the code which i usually use for video..but before that ,you must set up ffmpeg
to handle all formats of video just like you did for Imagemagick
.
here is set up for ffmpeg and dont forget to provide path in environment file using which ffmpeg
you need ffmpeg-paperclip as well for coding/decoding videos.
video.rb
##using s3
##convert the video into mp4 and also get screenshot of video at 5 sec
##add your own formats that you want
has_attached_file :video,
:styles => {
:mp4video => { :geometry => '520x390', :format => 'mp4',
:convert_options => { :output => { :vcodec => 'libx264',
:vpre => 'ipod640', :b => '250k', :bt => '50k',
:acodec => 'libfaac', :ab => '56k', :ac => 2 } } },
:preview => { :geometry => '300x300>', :format => 'jpg', :time => 5 }
},
processors: [:ffmpeg],
:storage => :s3,
:size => { :in => 0..25.megabytes },
:s3_permissions => :public_read,
:s3_credentials => S3_CREDENTIALS
validates_attachment_content_type :video, :content_type => /\Avideo\/.*\Z/
validates_presence_of :video
once you have saved the video,you need to use some plugins to show the video player along with your video in show
action.You can use Mediaelement.js(my favorite).Download it,dump it in js/css files accordingly,Include it in application.js and application.css.
in your show.html.erb
##add video tag to show video
<video autobuffer="autobuffer" preload="auto" style="width:100%;height:100%;" controls="controls" width="100%" height="100%" poster="<%= @video.video.url(:preview)%>" >
<source src="<%= @video.video.url%>" />
<%if @video.video.expiring_url(:mp4video).present? %>
<source type="video/mp4" src="<%= @video.video.url(:mp4video)%>" />
<%end%>
</video>
##javascript to handle video player
$('video').mediaelementplayer();
Video upload paperclip ffmepg
please always read the documentation. otherwise you will get comments a la RTFM.
when you use video_tag
it inserts an html5 video element into the page. read about it here: http://api.rubyonrails.org/classes/ActionView/Helpers/AssetTagHelper.html#method-i-video_tag
<video src="/trailers/hd.avi" width="16" height="16" />
this is probably not what you want for your jPlayer. when you inspect the html source, this should be obvious. i did never use jPlayer, but i assume that it would like to be pointed to the streaming source, which i guess is @post.file.url
in your case.
setting content-type for mp4 files on s3
Turns out that I needed to set a default s3 header content_type in the model. This isn't the best solution for me because at some point I might start allowing video containers other than mp4. But it gets me moving on to the next problem.
has_attached_file :video,
:storage => :s3,
:s3_credentials => "#{Rails.root.to_s}/config/s3.yml",
:path => "/video/:id/:filename",
:s3_headers => { "Content-Type" => "video/mp4" }
Related Topics
Add Current Time Before Log Message
Initializing Instance Variable as an Array - Ruby
How to Restart Rails from Within Rails
Breaking a Large Rails App into Smaller Apps
Rails Include Only Selected Columns from Relation
Sinatra + Heroku + Datamapper Deploy Issues with Dm-Sqlite-Adapter
How to Read from Redis Inside a Multi Block in Ruby
How to Refresh a Page with Turbolinks
Rails 3/ Git/ Bundler Fatal Could Not Parse Object
Trying to Get Svn2Git Working on Windows
Factory_Girl + Rspec Doesn't Seem to Roll Back Changes After Each Example
Single Custom Param Name in Routes for Nested Resources Rails 4.1
How to Use Ffmpeg on a Remote Machine via Ssh
Rails Console Fails with 'Switch to Inspect Mode' in Windows