UPLOAD ANY MEDIA FILE IN RAILS USING PAPERCLIP

Steps to Upload any type of media in ruby on rails by paperclip

Upload video in Rails

Gemfile

gem 'paperclip'
gem 'paperclip-ffmpeg'
gem 'paperclip-av-transcoder'

Apply validation in model

has_attached_file :file, :styles => {:medium => { :geometry => "300x300", :format => 'flv'},:thumb => {:geometry => "100x100#", :format => 'jpg', :time => 15}
    }, :processors => [:ffmpeg]
 

When you will get some validation errors then we can use this below code :-

validates_attachment_content_type :file, content_type: %w(video/mp4 video/3gp video/webm image/jpeg image/jpg image/png) 
  

Install ffmpeg on your system

sudo apt-add-repository ppa:jon-severinsson/ffmpeg
sudo apt-get update
sudo apt-get install ffmpeg


When we will get some ppa error then run:-

http://stackoverflow.com/questions/29125229/how-to-reinstall-ffmpeg-clean-on-ubuntu-14-04

Play video in rails using video_tag

video_tag("trailer.ogg", controls: true, autobuffer: true)

or we can use

gem 'jw_player_helper'

<%= video_player({:file => "/video/pf2011.flv", :image => "/video/pf2011.jpg"}) %>

This will play video in any format then you do not need to use any encoder.

Upload Audio


 gem "paperclip", "~> 3.0"

 rails g paperclip audio
 model 

 has_attached_file :audio
 validates_attachment :audio, content_type: { content_type: "media/x-wav" }

Play audio by using rails audio_tag
  
<%=audio_tag("sound.wav", autoplay: true, controls: true)%>

OR WE CAN USE

  has_attached_file :file,
          styles: lambda { |a| a.instance.is_image? ? {:small => "x200>", :medium => "x300>", :large => "x400>"}  : {:thumb => { :geometry => "100x100#", :format => 'jpg', :time => 10}, :medium => { :geometry => "300x300#", :format => 'jpg', :time => 10}}},
          :processors => lambda { |a| a.is_video? ? [ :ffmpeg ] : [ :thumbnail ] }

  validates_attachment_content_type :file, content_type: %w(video/mp4 video/quicktime video/3gp video/webm image/jpeg image/jpg image/png image/gif)
 

  def is_video?
    file.content_type =~ %r(video)
  end

  def is_image?
    file.content_type =~ %r(image)
  end

 If we are getting problem with FFMPEG 



with carrier-wave and paperclip then we need to run the below command :-

sudo add-apt-repository ppa:jonathonf/ffmpeg-3

sudo apt update && sudo apt install ffmpeg libav-tools x264 x265
We can follow the link below :-

After that getting some other issue like :-

 

*** No MethodError Exception: undefined method 'split' for : "content-type": Symbol

THIS PROBLEM IS GETTING WITH

ruby-2.2.1

WE NEED TO CHANGE OUR RVM:-
WE NEED TO USE :-


ruby-2.3.1


 
Post a Comment

Popular posts from this blog

CARRIERWAVE - UPLOAD ANY MEDIA (AUDIO , VIDEO AND IMAGE ) FILE IN RAILS

RAILS: UPLOADING PHOTOS VIA AMAZON S3 AND PAPERCLIP (UPLOADING FILES TO S3 IN RUBY WITH PAPERCLIP)

SELECT COUNTRY WITH STATE AND IT'S CITIES RAILS

UNINITIALIZED CONSTANT PAPERCLIP::STORAGE::S3::AWS

HOW TO CALL RAILS API FROM SIMPLE HTML PAGE.

Building native extensions. This could take a while... ERROR: Error installing mysql: ERROR: Failed to build gem native extension.

CREATE API FOR USER SIGN UP, LOGIN AND CREATE PUBLISHER WITH CURRENT USER