From 16566635985cfeb4586196eaca953032eebaa00f Mon Sep 17 00:00:00 2001 From: Eugen Rochko Date: Mon, 30 Jul 2018 19:33:05 +0200 Subject: [PATCH] Convert MOV and WEBM to MP4, raise maximum limit to 40MB (#8101) Separate size limits for images and video. Images remain at 8MB, while videos can be up to 40MB. --- app/models/media_attachment.rb | 54 ++++++++++++++++++++-------------- 1 file changed, 32 insertions(+), 22 deletions(-) diff --git a/app/models/media_attachment.rb b/app/models/media_attachment.rb index 63c6d5af84c..1e4fae3de74 100644 --- a/app/models/media_attachment.rb +++ b/app/models/media_attachment.rb @@ -25,10 +25,11 @@ class MediaAttachment < ApplicationRecord enum type: [:image, :gifv, :video, :unknown] IMAGE_FILE_EXTENSIONS = ['.jpg', '.jpeg', '.png', '.gif'].freeze - VIDEO_FILE_EXTENSIONS = ['.webm', '.mp4', '.m4v'].freeze + VIDEO_FILE_EXTENSIONS = ['.webm', '.mp4', '.m4v', '.mov'].freeze - IMAGE_MIME_TYPES = ['image/jpeg', 'image/png', 'image/gif'].freeze - VIDEO_MIME_TYPES = ['video/webm', 'video/mp4'].freeze + IMAGE_MIME_TYPES = ['image/jpeg', 'image/png', 'image/gif'].freeze + VIDEO_MIME_TYPES = ['video/webm', 'video/mp4', 'video/quicktime'].freeze + VIDEO_CONVERTIBLE_MIME_TYPES = ['video/webm', 'video/quicktime'].freeze IMAGE_STYLES = { original: { @@ -54,7 +55,25 @@ class MediaAttachment < ApplicationRecord }, }.freeze - LIMIT = 8.megabytes + VIDEO_FORMAT = { + format: 'mp4', + convert_options: { + output: { + 'movflags' => 'faststart', + 'pix_fmt' => 'yuv420p', + 'vf' => 'scale=\'trunc(iw/2)*2:trunc(ih/2)*2\'', + 'vsync' => 'cfr', + 'c:v' => 'h264', + 'b:v' => '500K', + 'maxrate' => '1300K', + 'bufsize' => '1300K', + 'crf' => 18, + }, + }, + }.freeze + + IMAGE_LIMIT = 8.megabytes + VIDEO_LIMIT = 40.megabytes belongs_to :account, inverse_of: :media_attachments, optional: true belongs_to :status, inverse_of: :media_attachments, optional: true @@ -65,8 +84,9 @@ class MediaAttachment < ApplicationRecord convert_options: { all: '-quality 90 -strip' } validates_attachment_content_type :file, content_type: IMAGE_MIME_TYPES + VIDEO_MIME_TYPES - validates_attachment_size :file, less_than: LIMIT - remotable_attachment :file, LIMIT + validates_attachment_size :file, less_than: IMAGE_LIMIT, unless: :video? + validates_attachment_size :file, less_than: VIDEO_LIMIT, if: :video? + remotable_attachment :file, VIDEO_LIMIT include Attachmentable @@ -122,25 +142,15 @@ class MediaAttachment < ApplicationRecord if f.instance.file_content_type == 'image/gif' { small: IMAGE_STYLES[:small], - original: { - format: 'mp4', - convert_options: { - output: { - 'movflags' => 'faststart', - 'pix_fmt' => 'yuv420p', - 'vf' => 'scale=\'trunc(iw/2)*2:trunc(ih/2)*2\'', - 'vsync' => 'cfr', - 'c:v' => 'h264', - 'b:v' => '500K', - 'maxrate' => '1300K', - 'bufsize' => '1300K', - 'crf' => 18, - }, - }, - }, + original: VIDEO_FORMAT, } elsif IMAGE_MIME_TYPES.include? f.instance.file_content_type IMAGE_STYLES + elsif VIDEO_CONVERTIBLE_MIME_TYPES.include?(f.instance.file_content_type) + { + small: VIDEO_STYLES[:small], + original: VIDEO_FORMAT, + } else VIDEO_STYLES end