diff options
Diffstat (limited to 'app/models/media_attachment.rb')
-rw-r--r-- | app/models/media_attachment.rb | 110 |
1 files changed, 76 insertions, 34 deletions
diff --git a/app/models/media_attachment.rb b/app/models/media_attachment.rb index 2813d9200..59427a8cc 100644 --- a/app/models/media_attachment.rb +++ b/app/models/media_attachment.rb @@ -19,12 +19,14 @@ # description :text # scheduled_status_id :bigint(8) # blurhash :string +# processing :integer # class MediaAttachment < ApplicationRecord self.inheritance_column = nil enum type: [:image, :gifv, :video, :unknown, :audio] + enum processing: [:queued, :in_progress, :complete, :failed], _prefix: true MAX_DESCRIPTION_LENGTH = 1_500 @@ -55,6 +57,43 @@ class MediaAttachment < ApplicationRecord }, }.freeze + VIDEO_FORMAT = { + format: 'mp4', + content_type: 'video/mp4', + convert_options: { + output: { + 'loglevel' => 'fatal', + 'movflags' => 'faststart', + 'pix_fmt' => 'yuv420p', + 'vf' => 'scale=\'trunc(iw/2)*2:trunc(ih/2)*2\'', + 'vsync' => 'cfr', + 'c:v' => 'h264', + 'maxrate' => '1300K', + 'bufsize' => '1300K', + 'frames:v' => 60 * 60 * 3, + 'crf' => 18, + 'map_metadata' => '-1', + }, + }, + }.freeze + + VIDEO_PASSTHROUGH_OPTIONS = { + video_codecs: ['h264'], + audio_codecs: ['aac', nil], + colorspaces: ['yuv420p'], + options: { + format: 'mp4', + convert_options: { + output: { + 'loglevel' => 'fatal', + 'map_metadata' => '-1', + 'c:v' => 'copy', + 'c:a' => 'copy', + }, + }, + }, + }.freeze + VIDEO_STYLES = { small: { convert_options: { @@ -69,17 +108,7 @@ class MediaAttachment < ApplicationRecord blurhash: BLURHASH_OPTIONS, }, - original: { - keep_same_format: true, - convert_options: { - output: { - 'loglevel' => 'fatal', - 'map_metadata' => '-1', - 'c:v' => 'copy', - 'c:a' => 'copy', - }, - }, - }, + original: VIDEO_FORMAT.merge(passthrough_options: VIDEO_PASSTHROUGH_OPTIONS), }.freeze AUDIO_STYLES = { @@ -96,26 +125,6 @@ class MediaAttachment < ApplicationRecord }, }.freeze - VIDEO_FORMAT = { - format: 'mp4', - content_type: 'video/mp4', - convert_options: { - output: { - 'loglevel' => 'fatal', - 'movflags' => 'faststart', - 'pix_fmt' => 'yuv420p', - 'vf' => 'scale=\'trunc(iw/2)*2:trunc(ih/2)*2\'', - 'vsync' => 'cfr', - 'c:v' => 'h264', - 'maxrate' => '1300K', - 'bufsize' => '1300K', - 'frames:v' => 60 * 60 * 3, - 'crf' => 18, - 'map_metadata' => '-1', - }, - }, - }.freeze - VIDEO_CONVERTED_STYLES = { small: VIDEO_STYLES[:small], original: VIDEO_FORMAT, @@ -124,6 +133,9 @@ class MediaAttachment < ApplicationRecord IMAGE_LIMIT = (ENV['MAX_IMAGE_SIZE'] || 10.megabytes).to_i VIDEO_LIMIT = (ENV['MAX_VIDEO_SIZE'] || 40.megabytes).to_i + MAX_VIDEO_MATRIX_LIMIT = 2_304_000 # 1920x1200px + MAX_VIDEO_FRAME_RATE = 60 + belongs_to :account, inverse_of: :media_attachments, optional: true belongs_to :status, inverse_of: :media_attachments, optional: true belongs_to :scheduled_status, inverse_of: :media_attachments, optional: true @@ -156,6 +168,10 @@ class MediaAttachment < ApplicationRecord remote_url.blank? end + def not_processed? + processing.present? && !processing_complete? + end + def needs_redownload? file.blank? && remote_url.present? end @@ -203,12 +219,21 @@ class MediaAttachment < ApplicationRecord "#{x},#{y}" end + attr_writer :delay_processing + + def delay_processing? + @delay_processing + end + + after_commit :enqueue_processing, on: :create after_commit :reset_parent_cache, on: :update before_create :prepare_description, unless: :local? before_create :set_shortcode + before_create :set_processing before_post_process :set_type_and_extension + before_post_process :check_video_dimensions before_save :set_meta @@ -277,6 +302,21 @@ class MediaAttachment < ApplicationRecord end end + def set_processing + self.processing = delay_processing? ? :queued : :complete + end + + def check_video_dimensions + return unless (video? || gifv?) && file.queued_for_write[:original].present? + + movie = FFMPEG::Movie.new(file.queued_for_write[:original].path) + + return unless movie.valid? + + raise Mastodon::DimensionsValidationError, "#{movie.width}x#{movie.height} videos are not supported" if movie.width * movie.height > MAX_VIDEO_MATRIX_LIMIT + raise Mastodon::DimensionsValidationError, "#{movie.frame_rate.to_i}fps videos are not supported" if movie.frame_rate > MAX_VIDEO_FRAME_RATE + end + def set_meta meta = populate_meta @@ -322,9 +362,11 @@ class MediaAttachment < ApplicationRecord }.compact end - def reset_parent_cache - return if status_id.nil? + def enqueue_processing + PostProcessMediaWorker.perform_async(id) if delay_processing? + end - Rails.cache.delete("statuses/#{status_id}") + def reset_parent_cache + Rails.cache.delete("statuses/#{status_id}") if status_id.present? end end |