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'
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]
}, :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
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
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
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 WITHruby-2.2.1
WE NEED TO CHANGE OUR RVM:-
WE NEED TO USE :-
ruby-2.3.1
Comments