From 4921cf0aa0a7f2054e8a9b8c81ce685faafe002c Mon Sep 17 00:00:00 2001 From: Essem Date: Mon, 27 Mar 2023 11:47:18 -0500 Subject: [PATCH] Merge in media changes from unstodon --- app/models/media_attachment.rb | 41 +++++++++++++++++++++++----------- 1 file changed, 28 insertions(+), 13 deletions(-) diff --git a/app/models/media_attachment.rb b/app/models/media_attachment.rb index 62c9828c65..c715e4569d 100644 --- a/app/models/media_attachment.rb +++ b/app/models/media_attachment.rb @@ -58,9 +58,9 @@ class MediaAttachment < ApplicationRecord IMAGE_MIME_TYPES = %w(image/jpeg image/png image/gif image/heic image/heif image/webp image/avif).freeze IMAGE_CONVERTIBLE_MIME_TYPES = %w(image/heic image/heif).freeze - VIDEO_MIME_TYPES = %w(video/webm video/mp4 video/quicktime video/ogg).freeze - VIDEO_CONVERTIBLE_MIME_TYPES = %w(video/webm video/quicktime).freeze - AUDIO_MIME_TYPES = %w(audio/wave audio/wav audio/x-wav audio/x-pn-wave audio/vnd.wave audio/ogg audio/vorbis audio/mpeg audio/mp3 audio/webm audio/flac audio/aac audio/m4a audio/x-m4a audio/mp4 audio/3gpp video/x-ms-asf).freeze + VIDEO_MIME_TYPES = %w(video/webm video/mp4 video/quicktime video/ogg audio/webm).freeze + VIDEO_CONVERTIBLE_MIME_TYPES = %w(video/quicktime).freeze + AUDIO_MIME_TYPES = %w(audio/wave audio/wav audio/x-wav audio/x-pn-wave audio/vnd.wave audio/ogg audio/vorbis audio/mpeg audio/mp3 audio/flac audio/aac audio/m4a audio/x-m4a audio/mp4 audio/3gpp video/x-ms-asf audio/opus).freeze BLURHASH_OPTIONS = { x_comp: 4, @@ -69,7 +69,7 @@ class MediaAttachment < ApplicationRecord IMAGE_STYLES = { original: { - pixels: 2_073_600, # 1920x1080px + pixels: 3_686_400, # 2560x1440px file_geometry_parser: FastGeometryParser, }.freeze, @@ -114,8 +114,8 @@ class MediaAttachment < ApplicationRecord }.freeze VIDEO_PASSTHROUGH_OPTIONS = { - video_codecs: ['h264'].freeze, - audio_codecs: ['aac', nil].freeze, + video_codecs: ['h264', 'av1'].freeze, + audio_codecs: ['aac', 'opus', nil].freeze, colorspaces: ['yuv420p'].freeze, options: { format: 'mp4', @@ -149,12 +149,28 @@ class MediaAttachment < ApplicationRecord AUDIO_STYLES = { original: { - format: 'mp3', - content_type: 'audio/mpeg', + format: 'webm', + content_type: 'audio/webm', convert_options: { output: { 'loglevel' => 'fatal', - 'q:a' => 2, + 'c:a' => 'libopus', + 'b:a' => '96k', + }.freeze, + }.freeze, + passthrough_options: { + video_codecs: [nil].freeze, + audio_codecs: ['opus'].freeze, + colorspaces: [nil].freeze, + options: { + format: 'webm', + convert_options: { + output: { + 'loglevel' => 'fatal', + 'map_metadata' => '-1', + 'c:a' => 'copy', + }.freeze, + }.freeze, }.freeze, }.freeze, }.freeze, @@ -291,7 +307,7 @@ class MediaAttachment < ApplicationRecord private def file_styles(attachment) - if attachment.instance.file_content_type == 'image/gif' || VIDEO_CONVERTIBLE_MIME_TYPES.include?(attachment.instance.file_content_type) + if VIDEO_CONVERTIBLE_MIME_TYPES.include?(attachment.instance.file_content_type) VIDEO_CONVERTED_STYLES elsif IMAGE_CONVERTIBLE_MIME_TYPES.include?(attachment.instance.file_content_type) IMAGE_CONVERTED_STYLES @@ -305,9 +321,7 @@ class MediaAttachment < ApplicationRecord end def file_processors(instance) - if instance.file_content_type == 'image/gif' - [:gif_transcoder, :blurhash_transcoder] - elsif VIDEO_MIME_TYPES.include?(instance.file_content_type) + if VIDEO_MIME_TYPES.include?(instance.file_content_type) [:transcoder, :blurhash_transcoder, :type_corrector] elsif AUDIO_MIME_TYPES.include?(instance.file_content_type) [:image_extractor, :transcoder, :type_corrector] @@ -368,6 +382,7 @@ class MediaAttachment < ApplicationRecord end def image_geometry(file) + return {} if file.nil? width, height = FastImage.size(file.path) return {} if width.nil?