Skip to content

Instantly share code, notes, and snippets.

@frankie-loves-jesus
Last active August 29, 2015 14:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save frankie-loves-jesus/dfdfadd7327d5c1085cc to your computer and use it in GitHub Desktop.
Save frankie-loves-jesus/dfdfadd7327d5c1085cc to your computer and use it in GitHub Desktop.

paperclip-ffmpeg autoconvert animated GIF to MP4

Problem: MP4s are empty (16k only)

Go to Default and then the latest post. Alternatively log in with email admin@example.com and password admin1234 and upload an animated GIF yourself.

photo.rb

class Photo < ActiveRecord::Base
  has_attached_file :attachment, :styles => {
    medium: {format: "flv"}
  }, :processors => [:ffmpeg]

  belongs_to :post, class_name: "Forem::Post"

  validates_attachment :attachment, presence: true, content_type: { content_type: ["image/jpg", "image/jpeg", "image/png", "image/gif"] }
end

_post.html.erb

<% for photo in post.photos %>
  <%= video_tag photo.attachment.url(:medium) %>
<% end %>

development.rb

Paperclip.options[:command_path] = "/usr/bin/"

ffmpeg

# ffmpeg
ffmpeg version 0.8.7-6:0.8.7-1ubuntu2
# which ffmpeg
/usr/bin/ffmpeg

development.log

Started POST "/default/topics" at 2014-12-02 23:44:09 +0000
Processing by Forem::TopicsController#create as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"e4utZ9TFEZh2OIDcyj3Fgko3KaQ/NxI0ysb+FDo/uKA=", "topic"=>{"subject"=>"testtttttttt", "posts_attribu
tes"=>{"0"=>{"text"=>"tatatatata", "photos_attributes"=>{"0"=>{"attachment"=>#<ActionDispatch::Http::UploadedFile:0x00000003724c20 @tempfile=#<File:/
tmp/RackMultipart20141202-27-9vxiez>, @original_filename="gif.gif", @content_type="image/gif", @headers="Content-Disposition: form-data; name=\"topic
[posts_attributes][0][photos_attributes][0][attachment]\"; filename=\"gif.gif\"\r\nContent-Type: image/gif\r\n">}}}}}, "commit"=>"Create Topic", "for
um_id"=>"default"}
  User Load (0.3ms)  SELECT  "users".* FROM "users"  WHERE "users"."id" = 1  ORDER BY "users"."id" ASC LIMIT 1
  Forem::Forum Load (0.2ms)  SELECT  "forem_forums".* FROM "forem_forums"  WHERE "forem_forums"."slug" = 'default'  ORDER BY "forem_forums"."name" AS
C LIMIT 1
  Forem::Category Load (0.2ms)  SELECT  "forem_categories".* FROM "forem_categories"  WHERE "forem_categories"."id" = ? LIMIT 1  [["id", 1]]
Command :: file -b --mime '/tmp/d38252762d3d4fd229faae637fd13f4e20141202-27-1koo3i1.gif'
[paperclip] [ffmpeg] ffprobe "/tmp/d38252762d3d4fd229faae637fd13f4e20141202-27-by75h8.gif" 2>&1
[paperclip] [ffmpeg] Command Success
[paperclip] [ffmpeg] Making...
[paperclip] [ffmpeg] Building Destination File: 'd38252762d3d4fd229faae637fd13f4e20141202-27-by75h8' + 'flv'
[paperclip] [ffmpeg] Destination File Built
[paperclip] [ffmpeg] Adding Geometry
[paperclip] [ffmpeg] Adding Format
[paperclip] [ffmpeg] Adding Source
[paperclip] [ffmpeg] Building Parameters
[paperclip] [ffmpeg] -i :source -y :dest
Command :: ffmpeg -i '/tmp/d38252762d3d4fd229faae637fd13f4e20141202-27-by75h8.gif' -y '/tmp/d38252762d3d4fd229faae637fd13f4e20141202-27-by75h82014120
2-27-62u4ha.flv'
ffmpeg version 0.8.7-6:0.8.7-1ubuntu2, Copyright (c) 2000-2013 the Libav developers
  built on Oct 11 2013 08:39:40 with gcc 4.8.1
*** THIS PROGRAM IS DEPRECATED ***
This program is only provided for compatibility and will be removed in a future release. Please use avconv instead.
Input #0, image2, from '/tmp/d38252762d3d4fd229faae637fd13f4e20141202-27-by75h8.gif':
  Duration: 00:00:00.04, start: 0.000000, bitrate: N/A
    Stream #0.0: Video: gif, pal8, 320x123, 25 tbr, 25 tbn, 25 tbc
Incompatible pixel format 'pal8' for codec 'flv', auto-selecting format 'yuv420p'
[buffer @ 0x8cdd60] w:320 h:123 pixfmt:pal8
[avsink @ 0x8cdfe0] auto-inserting filter 'auto-inserted scaler 0' between the filter 'src' and the filter 'out'
[scale @ 0x8d0220] w:320 h:123 fmt:pal8 -> w:320 h:123 fmt:yuv420p flags:0x4
Output #0, flv, to '/tmp/d38252762d3d4fd229faae637fd13f4e20141202-27-by75h820141202-27-62u4ha.flv':
  Metadata:
    encoder         : Lavf53.21.1
    Stream #0.0: Video: flv, yuv420p, 320x123, q=2-31, 200 kb/s, 1k tbn, 25 tbc
Stream mapping:
  Stream #0.0 -> #0.0
    encoder         : Lavf53.21.1
    Stream #0.0: Video: flv, yuv420p, 320x123, q=2-31, 200 kb/s, 1k tbn, 25 tbc
Stream mapping:
  Stream #0.0 -> #0.0
Press ctrl-c to stop encoding
frame=    1 fps=  0 q=3.3 Lsize=      12kB time=0.04 bitrate=2508.0kbits/s
video:12kB audio:0kB global headers:0kB muxing overhead 1.835309%
Command :: file -b --mime '/tmp/d38252762d3d4fd229faae637fd13f4e20141202-27-by75h820141202-27-62u4ha.flv'
[paperclip] [ffmpeg] ffprobe "/tmp/d38252762d3d4fd229faae637fd13f4e20141202-27-by75h8.gif" 2>&1
[paperclip] [ffmpeg] Command Success
[paperclip] [ffmpeg] Making...
[paperclip] [ffmpeg] Building Destination File: 'd38252762d3d4fd229faae637fd13f4e20141202-27-by75h8' + 'png'
[paperclip] [ffmpeg] Destination File Built
[paperclip] [ffmpeg] Adding Geometry
[paperclip] [ffmpeg] Adding Format
[paperclip] [ffmpeg] Adding Source
[paperclip] [ffmpeg] Building Parameters
[paperclip] [ffmpeg] -ss 3 -i :source -vframes 1 -f image2 -y :dest
Command :: ffmpeg -ss 3 -i '/tmp/d38252762d3d4fd229faae637fd13f4e20141202-27-by75h8.gif' -vframes 1 -f image2 -y '/tmp/d38252762d3d4fd229faae637fd13f
4e20141202-27-by75h820141202-27-z75ldz.png'
ffmpeg version 0.8.7-6:0.8.7-1ubuntu2, Copyright (c) 2000-2013 the Libav developers
  built on Oct 11 2013 08:39:40 with gcc 4.8.1
*** THIS PROGRAM IS DEPRECATED ***
This program is only provided for compatibility and will be removed in a future release. Please use avconv instead.
/tmp/d38252762d3d4fd229faae637fd13f4e20141202-27-by75h8.gif: could not seek to position 3.000
Input #0, image2, from '/tmp/d38252762d3d4fd229faae637fd13f4e20141202-27-by75h8.gif':
  Duration: 00:00:00.04, start: 0.000000, bitrate: N/A
    Stream #0.0: Video: gif, pal8, 320x123, 25 tbr, 25 tbn, 25 tbc
[buffer @ 0xdced60] w:320 h:123 pixfmt:pal8
Output #0, image2, to '/tmp/d38252762d3d4fd229faae637fd13f4e20141202-27-by75h820141202-27-z75ldz.png':
  Metadata:
    encoder         : Lavf53.21.1
    Stream #0.0: Video: png, pal8, 320x123, q=2-31, 200 kb/s, 90k tbn, 25 tbc
Stream mapping:
  Stream #0.0 -> #0.0
Press ctrl-c to stop encoding
frame=    0 fps=  0 q=0.0 Lsize=      -0kB time=10000000000.00 bitrate=  -0.0kbits/s
video:0kB audio:0kB global headers:0kB muxing overhead -inf%
   (0.2ms)  begin transaction
  Forem::Topic Exists (0.3ms)  SELECT  1 AS one FROM "forem_topics"  WHERE ("forem_topics"."id" IS NOT NULL) AND "forem_topics"."slug" = 'testttttttt
t' LIMIT 1
Command :: file -b --mime '/tmp/d38252762d3d4fd229faae637fd13f4e20141202-27-zt4x8a.gif'
  SQL (0.7ms)  INSERT INTO "forem_topics" ("created_at", "forum_id", "slug", "subject", "updated_at", "user_id") VALUES (?, ?, ?, ?, ?, ?)  [["create
d_at", "2014-12-02 23:44:10.643036"], ["forum_id", 1], ["slug", "testtttttttt"], ["subject", "testtttttttt"], ["updated_at", "2014-12-02 23:44:10.643
036"], ["user_id", 1]]
  SQL (0.3ms)  INSERT INTO "forem_posts" ("created_at", "text", "topic_id", "updated_at", "user_id") VALUES (?, ?, ?, ?, ?)  [["created_at", "2014-12
-02 23:44:10.645842"], ["text", "tatatatata"], ["topic_id", 24], ["updated_at", "2014-12-02 23:44:10.645842"], ["user_id", 1]]
  Forem::Topic Load (0.1ms)  SELECT  "forem_topics".* FROM "forem_topics"  WHERE "forem_topics"."id" = ? LIMIT 1  [["id", 24]]
  SQL (7.2ms)  UPDATE "forem_topics" SET "last_post_at" = '2014-12-02 23:44:10.645842' WHERE "forem_topics"."id" = 24
  Forem::Subscription Load (0.1ms)  SELECT  "forem_subscriptions".* FROM "forem_subscriptions"  WHERE "forem_subscriptions"."topic_id" = ?  ORDER BY
"forem_subscriptions"."id" ASC LIMIT 1000  [["topic_id", 24]]
  SQL (0.1ms)  UPDATE "forem_posts" SET "notified" = 't' WHERE "forem_posts"."id" = 24
  SQL (0.1ms)  UPDATE "forem_posts" SET "state" = 'approved' WHERE "forem_posts"."id" = 24
Binary data inserted for `string` type on column `attachment_content_type`
  SQL (0.2ms)  INSERT INTO "photos" ("attachment_content_type", "attachment_file_name", "attachment_file_size", "attachment_updated_at", "created_at"
, "post_id", "updated_at") VALUES (?, ?, ?, ?, ?, ?, ?)  [["attachment_content_type", "image/gif"], ["attachment_file_name", "gif.gif"], ["attachment
_file_size", 1660134], ["attachment_updated_at", "2014-12-02 23:44:09.634684"], ["created_at", "2014-12-02 23:44:10.665365"], ["post_id", 24], ["upda
ted_at", "2014-12-02 23:44:10.665365"]]
   (0.1ms)  SELECT COUNT(*) FROM "forem_subscriptions"  WHERE "forem_subscriptions"."topic_id" = ? AND "forem_subscriptions"."subscriber_id" = 1  [["
topic_id", 24]]
  SQL (0.3ms)  INSERT INTO "forem_subscriptions" ("subscriber_id", "topic_id") VALUES (?, ?)  [["subscriber_id", 1], ["topic_id", 24]]
  SQL (0.1ms)  UPDATE "forem_topics" SET "state" = 'approved' WHERE "forem_topics"."id" = 24
   (10.3ms)  commit transaction
Redirected to http://web-ad58b87a-3e24-48bb-afdf-941bfbb400a0.runnable.com/default/topics/testtttttttt
Completed 302 Found in 1105ms (ActiveRecord: 21.1ms)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment