Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to load and validate the video using paperclip-Rails

please help solve the problem.

using a paperclip I organized upload images. it works.

Now I would like to organize a video upload. I changed this model: model:

class Video < ActiveRecord::Base
  validates :title, presence: true
  validates :video, presence: true

  belongs_to :user

  has_attached_file   :video, 
                      :styles => { :medium => "300x300>", :thumb => "100x100>" }, 
                      :default_url => "/images/:style/missing.png"

  validates_attachment_content_type :video, :content_type => /\Avideo\/.*\Z/  
  validates_attachment_file_name :video, :matches => [/3gp\Z/, /mp4\Z/, /flv\Z/]  

 validate :file_size_validation, :if => "video?"  

  def file_size_validation
    errors[:video] << "should be less than 2MB" if video.size.to_i > 30.megabytes
  end  
end

video controller:

def create
  @video = Video.new(video_params)   

  if @video.save
    @video.update_attributes(user: current_user)

    flash[:success] = :video_created
    redirect_to @video
  else
    flash.now[:error] = :user_not_created
    render 'new'
  end
end

form:

<%= form_for(@video) do |f| %>
    <%= f.text_field  :title %>
    <%= f.file_field  :video %>
    <%= f.submit %>
<% end %>

after attempting to upload the video I get the console the following error message:

Started POST "/videos" for 127.0.0.1 at 2015-07-23 14:17:32 +0300
Processing by VideosController#create as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"RF3w8PzZ9LZ4pdqRPqRvHMQ+nYDL4om0YHAMlzTm8tf3iFmVmKxXSYKap3C0ogEnOkifm9i01oXs/rTX9zmtPA==", "video"=>{"title"=>"tty", "video"=>#<ActionDispatch::Http::UploadedFile:0x007f6624f10770 @tempfile=#<Tempfile:/tmp/RackMultipart20150723-13428-u7s8i8.flv>, @original_filename="mmm.flv", @content_type="video/x-flv", @headers="Content-Disposition: form-data; name=\"video[video]\"; filename=\"mmm.flv\"\r\nContent-Type: video/x-flv\r\n">}, "commit"=>"Create Video"}
  User Load (0.3ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = $1  ORDER BY "users"."id" ASC LIMIT 1  [["id", 8]]
Command :: file -b --mime '/tmp/c4efd5020cb49b9d3257ffa0fbccc0ae20150723-13428-6gn39i.flv'
Command :: identify -format '%wx%h,%[exif:orientation]' '/tmp/c4efd5020cb49b9d3257ffa0fbccc0ae20150723-13428-1eailfd.flv[0]' 2>/dev/null
[paperclip] An error was received while processing: #<Paperclip::Errors::NotIdentifiedByImageMagickError: Paperclip::Errors::NotIdentifiedByImageMagickError>
Command :: identify -format '%wx%h,%[exif:orientation]' '/tmp/c4efd5020cb49b9d3257ffa0fbccc0ae20150723-13428-1eailfd.flv[0]' 2>/dev/null
[paperclip] An error was received while processing: #<Paperclip::Errors::NotIdentifiedByImageMagickError: Paperclip::Errors::NotIdentifiedByImageMagickError>
   (0.2ms)  BEGIN
Command :: file -b --mime '/tmp/c4efd5020cb49b9d3257ffa0fbccc0ae20150723-13428-khstwa.flv'
   (0.2ms)  ROLLBACK

on screen displays the following error message:

Video Paperclip::Errors::NotIdentifiedByImageMagickError

wherein imageMagik installed in my system:

sudo apt-get install imagemagick

db:

  create_table "videos", force: :cascade do |t|
    t.datetime "created_at",         null: false
    t.datetime "updated_at",         null: false
    t.string   "title"
    t.integer  "user_id"
    t.string   "video_file_size"
    t.string   "video_updated_at"
    t.string   "video_file_name"
    t.string   "video_content_type"
  end
like image 537
stackov8 Avatar asked Jul 23 '15 11:07

stackov8


People also ask

How do you use a paperclip in rails?

Setting Up PaperclipTo set up Paperclip, first we need to install the ImageMagick dependency. Paperclip uses ImageMagick to resize images after upload. If you are using another system, you can get download and install instructions from the ImageMagick website. Run bundle install to finish it up.

What is Paperclip in Rails?

Paperclip is an easy file attachment library for Rails Applications. Attached files are saved to the file system, database or cloud and referenced in the browser by an easily understandable specification.


2 Answers

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();
like image 172
Milind Avatar answered Oct 13 '22 19:10

Milind


paperclip's processor is designed for images, so it cannot handle videos. You're getting the error saying that ImageMagick did not recognize this file as an image. Fair enough, it's not an image!

Still, you could write a post-processor yourself, and paperclip's github page offers an example of the syntax that would be needed to call a custom processing class:

has_attached_file :scan, :styles => { :text => { :quality => :better } },
                         :processors => [:ocr]

This defines a variation of the attachment called text that is obtained by processing the uploaded file with a (hypothetical) Paperclip::Ocr class with options hash { :quality => :better }

Choice of the exact conversion tool is up to you, but you do want to make sure that processing is done asynchronously, not inside the usual request-response cycle, you may want to use a background queue like Sidekiq, Que or Rails' (4.2+) ActiveJob.

like image 38
D-side Avatar answered Oct 13 '22 20:10

D-side