Monday, 21 April 2014

Validate Video with his length using paperclip in rails

1. Download ffmpeg on mac.

2. Run ‘which ffmpeg’ on console. It will show the path if it is saved or not.

3. Now write  ‘ gem "paperclip-ffmpeg" ’ in Gemfile.
  and Write the following code in config/development.rb

Paperclip.options[:command_path] = "/usr/local/bin/"
4. add the following field in the view
   <%= f.file_field :vid %>

4. Write code in model file

class Post < ActiveRecord::Base
   attr_accessible :content, :user_id, :vid
   has_attached_file :vid, :styles => {
   :medium => { :geometry => "640x480", :format => 'flv' },
   :thumb => { :geometry => "100x100#", :format => 'jpg', :time => 10 }
 }, :processors => [:ffmpeg]

validate :get_video_duration

 def get_video_duration
   vid = self.vid_meta[:length].split(/[:.]/)
   count = vid[0].to_i*3600+vid[1].to_i*60+vid[2].to_i
   if count > 8
     errors.add(:duration, "Length Is Greater For Video")


get_duration_videos code for not to upload more than 8 sec video.

5. Add the following columns in present table using given migration

class AddVidToPosts < ActiveRecord::Migration
 def change
   add_column :posts, :vid, :string
add_column :posts, :vid_file_name, :string
add_column :posts, :vid_meta, :text
add_column :posts, :vid_file_size, :string

6. In the show form Write the following code

  <%= video_tag @post.vid %>
  <%=image_tag @post.vid(:thumb).to_s %>
<a href="<%=@post.vid%>" > Download video </a>