Paperclip Video Upload

Paperclip for both images and Videos

Looks like the issue is that ImageMagick is trying to transcode the video, which it cannot.

The way to handle transcoding of videos within Paperclip is to use the paperclip-av-transcoder gem (formerly paperclip-ffmpeg). I only have experience with the latter:

class Attachment < ActiveRecord::Base
has_attached_file :url, :s3_protocol => :https ,
styles: lambda { |a| a.instance.is_image? ? medium: "300x300>", thumb: "100x100>", big: "1200x1200>", normal: "600x600>" } : {:thumb => { :geometry => "100x100#", :format => 'jpg', :time => 10}, :medium => { :geometry => "300x300#", :format => 'jpg', :time => 10}}}, processors: [:transcoder]

validates_attachment :url,
content_type: ['video/mp4'],
message: "Sorry, right now we only support MP4 video",
if: :is_video?

validates_attachment :url,
content_type: ['image/png', 'image/jpeg', 'image/jpg', 'image/gif'],
message: "Different error message",
if: :is_image?


def is_video?
url.instance.attachment_content_type =~ %r(video)

def is_image?
url.instance.attachment_content_type =~ %r(image)

Our old code & ref

Paperclip Video Upload

You should use video tag, not iframe (only video tag has autoplay options). Check here what formats are supported by what browser:

If you want crossbrowser solution, try VideoJS -
Here you can get a Rails plugin -

Video playback with paperclip rails

You can take a look at the Paperclip

In the model that will contain the video you will need the right format (ffmpeg) and validation for videos:

   has_attached_file :attachment,
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 :video, content_type: /\Avideo\/.*\Z/

Update from comment:

 has_attached_file :video, styles: {
:medium => {
:geometry => "640x480",
:format => 'mp4'
:thumb => { :geometry => "160x120", :format => 'jpeg', :time => 10}
}, :processors => [:transcoder]

how to load and validate the video using paperclip-Rails

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.

##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="<%=>" >
<source src="<%=>" />
<%if %>
<source type="video/mp4" src="<%=>" />

##javascript to handle video player

Rails video uploading

We got Paperclip working with video a while back


You'll have the same ambiguity whether you use CarrierWave or Paperclip (Rails' two main "attachment" handlers)

Any upload system only handles the transfer of file data between your PC, your Rails app & your db. Each of them (from my understanding). E.G Paperclip only creates an ActiveRecord object from your file, saves the data to your server's public dir & creates a record in your db


The question of video is one of using the right processor, rather than the right uploader:

has_attached_file :attachment,
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 ] }


You'll need to use a video processor such as ffmpeg for Paperclip:

gem "paperclip-ffmpeg", "~> 1.0.1"

You may have to install ffmpeg on your system to get the processor to work locally (Heroku has ffmpeg). This will allow you to use the video_tag helper:

<%= video_tag(@model.attachment.url) %>

There's a good tutorial about using ffmpeg with Paperclip here
And another tutorial here

Using Paperclip with Video upload to get :poster

you can use ffmpeg with paperclip to process the video and extract a thumbnail from where-ever you want (I normally grab from a few frames in to avoid initial black/blank frames) ... see for an example (the third script - video_thumbnail - is what you'll want to start with

Rails: Rendering video uploaded by paperclip

while I'm not 100% sure this is the problem, I believe the Paperclip library is deprecated and it does not work with ruby 3. If you use the latest ruby 2 version is should work for you. However, I would recommend not depending on paperclip for new projects.

Related Topics

Leave a reply