CARRIERWAVE - UPLOAD ANY MEDIA (AUDIO , VIDEO AND IMAGE ) FILE IN RAILS
UPLOADING ANY MEDIA FILE WITH CARRIER-WAVE RAILS
Multiple file uploads
Add it to your Gemfile:
gem 'mime-types'gem 'mini_magick'
gem 'rmagick'
gem 'carrierwave', '~> 1.0'
gem 'carrierwave-video'
gem 'carrierwave-video-thumbnailer'
gem 'carrierwave-ffmpeg'
Run
bundle
Start off by generating an uploader:
app/uploaders/vimeo_uploader.rb
rails generate uploader Vimeo
Check out this file for some hints on how you can customize your uploader. It should look something like this:
class VimeoUploader < CarrierWave::Uploader::Base
storage :file
end
storage :file
end
If we want to upload video or image then we will have to use :-
class VimeoUploader < CarrierWave::Uploader::Base
include CarrierWave::MiniMagick
include CarrierWave::Video
include CarrierWave::Video::Thumbnailer
include CarrierWave::FFmpeg
storage :file
include CarrierWave::MiniMagick
include CarrierWave::Video
include CarrierWave::Video::Thumbnailer
include CarrierWave::FFmpeg
storage :file
def store_dir
"uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
end
version :video, :if => :video? do
process :encode
end
version :thumb do
process thumbnail: [{format: 'jpg', quality: 8, size: 360, logger: Rails.logger}]
def full_filename for_file
jpg_name for_file, version_name
end
end
version :medium do
process thumbnail: [{format: 'jpg', quality: 85, size: 300, logger: Rails.logger}]
def full_filename for_file
jpg_name for_file, version_name
end
end
def encode
video = FFMPEG::Movie.new(@file.path)
video_transcode = video.transcode(@file.path)
end
# Create different versions of your uploaded files:
version :image,:if => :image? do
process :resize_to_fit => [740, 300]
end
def jpg_name for_file, version_name
%Q{#{version_name}_#{for_file.chomp(File.extname(for_file))}.jpg}
end
protected
def image?(new_file)
new_file.content_type.include? 'image'
end
def video?(new_file)
new_file.content_type.include? 'video'
end
end
"uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
end
version :video, :if => :video? do
process :encode
end
version :thumb do
process thumbnail: [{format: 'jpg', quality: 8, size: 360, logger: Rails.logger}]
def full_filename for_file
jpg_name for_file, version_name
end
end
version :medium do
process thumbnail: [{format: 'jpg', quality: 85, size: 300, logger: Rails.logger}]
def full_filename for_file
jpg_name for_file, version_name
end
end
def encode
video = FFMPEG::Movie.new(@file.path)
video_transcode = video.transcode(@file.path)
end
# Create different versions of your uploaded files:
version :image,:if => :image? do
process :resize_to_fit => [740, 300]
end
def jpg_name for_file, version_name
%Q{#{version_name}_#{for_file.chomp(File.extname(for_file))}.jpg}
end
protected
def image?(new_file)
new_file.content_type.include? 'image'
end
def video?(new_file)
new_file.content_type.include? 'video'
end
end
Add a string column to the model you want to mount the uploader by creating a migration:
rails g migration add_vimeo_file_to_users vimeo_file:string
rake db:migrate
Open your model file and mount the uploader:
class User < ActiveRecord::Base
mount_uploader :vimeo_file, VimeoUploader
end
rake db:migrate
Open your model file and mount the uploader:
class User < ActiveRecord::Base
mount_uploader :vimeo_file, VimeoUploader
end
In your form :-
<%= f.file_field :vimeo_file %>
In controller :-
def create
@user = User.new
@user.file = params[:vimeo_file]
@user.save
end
@user = User.new
@user.file = params[:vimeo_file]
@user.save
end
For Installing the Dependencies :-
Installing FFMPEG
sudo apt-get install ppa-purge
sudo ppa-purge ppa:jon-severinsson/ffmpeg
sudo add-apt-repository ppa:mc3man/trusty-media
sudo apt-get update
sudo apt-get dist-upgrade
sudo apt-get install ffmpeg
sudo ppa-purge ppa:jon-severinsson/ffmpeg
sudo add-apt-repository ppa:mc3man/trusty-media
sudo apt-get update
sudo apt-get dist-upgrade
sudo apt-get install ffmpeg
We can follow :-
https://stackoverflow.com/questions/29125229/how-to-reinstall-ffmpeg-clean-on-ubuntu-14-04
http://www.powercms.in/blog/how-install-ffmpeg-ubuntu-1404-1410-and-linux-mint
http://www.powercms.in/blog/how-install-ffmpeg-ubuntu-1404-1410-and-linux-mint
After getting error with encoding error like this:-
then we need to run:-
sudo add-apt-repository ppa:jonathonf/ffmpeg-3
sudo apt update && sudo apt install ffmpeg libav-tools x264 x265
http://ubuntuhandbook.org/index.php/2016/09/install-ffmpeg-3-1-ubuntu-16-04-ppa/
sudo apt update && sudo apt install ffmpeg libav-tools x264 x265
http://ubuntuhandbook.org/index.php/2016/09/install-ffmpeg-3-1-ubuntu-16-04-ppa/
Install ffmpegthumbnailer
Install ImageMagick
sudo apt-get install imagemagick libmagickwand-dev
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