about summary refs log tree commit diff
path: root/app/models
diff options
context:
space:
mode:
authorDavid Yip <yipdw@member.fsf.org>2018-02-17 00:02:37 -0600
committerDavid Yip <yipdw@member.fsf.org>2018-02-17 00:02:37 -0600
commit3d033a468748338b6036cb24bb00ea4e88656ae6 (patch)
treea1b3bcbe599a9298f4167769ea2c197f7ca596e6 /app/models
parente9052ceaafff786590fb66bee4550878cfb5a0df (diff)
parentc770b503c0e75015f8ba1ca90755b19326b64c3d (diff)
Merge remote-tracking branch 'tootsuite/master' into merge-upstream
 Conflicts:
	Gemfile
	config/locales/simple_form.pl.yml
Diffstat (limited to 'app/models')
-rw-r--r--app/models/media_attachment.rb40
1 files changed, 28 insertions, 12 deletions
diff --git a/app/models/media_attachment.rb b/app/models/media_attachment.rb
index 25b7fd085..6f17363c8 100644
--- a/app/models/media_attachment.rb
+++ b/app/models/media_attachment.rb
@@ -181,23 +181,39 @@ class MediaAttachment < ApplicationRecord
     meta = {}
 
     file.queued_for_write.each do |style, file|
-      begin
-        geo = Paperclip::Geometry.from_file file
-
-        meta[style] = {
-          width: geo.width.to_i,
-          height: geo.height.to_i,
-          size: "#{geo.width.to_i}x#{geo.height.to_i}",
-          aspect: geo.width.to_f / geo.height.to_f,
-        }
-      rescue Paperclip::Errors::NotIdentifiedByImageMagickError
-        meta[style] = {}
-      end
+      meta[style] = style == :small || image? ? image_geometry(file) : video_metadata(file)
     end
 
     meta
   end
 
+  def image_geometry(file)
+    geo = Paperclip::Geometry.from_file file
+
+    {
+      width:  geo.width.to_i,
+      height: geo.height.to_i,
+      size: "#{geo.width.to_i}x#{geo.height.to_i}",
+      aspect: geo.width.to_f / geo.height.to_f,
+    }
+  rescue Paperclip::Errors::NotIdentifiedByImageMagickError
+    {}
+  end
+
+  def video_metadata(file)
+    movie = FFMPEG::Movie.new(file.path)
+
+    return {} unless movie.valid?
+
+    {
+      width: movie.width,
+      height: movie.height,
+      frame_rate: movie.frame_rate,
+      duration: movie.duration,
+      bitrate: movie.bitrate,
+    }
+  end
+
   def appropriate_extension
     mime_type = MIME::Types[file.content_type]