Polyov
Polyov

Reputation: 2321

Rails: Paperclip produces different results for similar videos

I am using the Paperclip Gem to handle video uploads on my Rails app. I've followed the instructions in a few other stackoverflow questions and external tutorials to get video uploads working. However, currently there is a strange glitch with the system that I don't understand.

When uploading two different videos, the app will be able to show a thumbnail and no video for one upload, and no thumbnail but video for another one.

Both videos use the same codecs, are around the same length, but are a bit different in their dimensions & file size.

the video details

When I upload them, test1 has a thumbnail, but my browser will not load the video file. It exists in my file system but Chrome will not play it.

The opposite happens for test2. Its thumbnail in my file system is zero bytes, but the video loads fine in my browser.

Here are the paperclip upload parameters for my object:

has_attached_file :video, :styles => {
    :medium => { :format => 'mp4' },
    :thumb => { :geometry => "500x500#", :format => 'jpg', :time => 10 }
  }, :processors => [:transcoder]
validates_attachment_presence :video
validates_attachment :video, content_type: { content_type: ["video/mp4", "video/mov", "video/wav", "video/wmv"] }

I've created a Github repo with an example app that reproduces the issue, at least on my system.

Upvotes: 0

Views: 208

Answers (3)

nikolayp
nikolayp

Reputation: 17949

The issue is in: :time => 10 parameter

Change it on: :time => 1

This change means: create screenshot on 1st second instead of 10th second. Because the second movie has only 10 second length, during the first movie has 12 seconds length. Thus the first movie has screenshot, and second don't and uses previous one.

Wish it helped you!

Upvotes: 2

aergistal
aergistal

Reputation: 31219

I was able to play both files in multiple browsers without issue.

However, the reasons why test1.mp4 might not (seem) to work are:

  1. test1.mp4 has the moov atom at the end of the file:

    AtomicParsley test1.mp4 -T
    Atom ftyp @ 0 of size: 32, ends @ 32
    Atom free @ 32 of size: 8, ends @ 40
    Atom mdat @ 40 of size: 1310866, ends @ 1310906
    Atom moov @ 1310906 of size: 9926, ends @ 1320832
    

    This means the browser must fully download the file before it can play it. If you're on a slow connection it might take a while and appear like it's not working.

    test2.mp4 has the moov atom at the beginning of the file meaning it can be played back before the download finishes:

     AtomicParsley test2.mp4 -T
     Atom ftyp @ 0 of size: 32, ends @ 32
     Atom moov @ 32 of size: 7464, ends @ 7496
    

    You can move the moov atom to the beginning of the file using the ffmpeg movflags faststart option or with the qt-faststart tool.

    It shouldn't be an issue if you're using your local file system, so see below.

  2. test1.mp4 uses the Main H.264 profile while test2.mp4 uses the Constrained Baseline profile. This can be an issue on some mobile devices as the Baseline profile has the most support. However it shouldn't be an issue with newer devices.

    You could try to re-encode it with the Baseline profile to see if this is the issue.

Upvotes: 1

Erica
Erica

Reputation: 221

Please do follow this link https://github.com/thoughtbot/paperclip/wiki/Thumbnail-Generation

and i think validation for video can be done in this way:

validates_attachment :video, :presence => true

rather than doing with presence and again without it.

For image i followed :

validates_attachment :attachment, content_type: { content_type: ["image/jpeg", "image/gif", "image/png"] }

Hope this will work for you :-)

Upvotes: 0

Related Questions