From 0bc7bd75cbabb31224d5214a4897d7bf50d981a0 Mon Sep 17 00:00:00 2001 From: Claire Date: Thu, 5 Oct 2023 14:58:46 +0200 Subject: [PATCH] Add passthrough options for audio attachments --- app/models/media_attachment.rb | 16 ++++++++++++++++ lib/paperclip/transcoder.rb | 6 +++++- 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/app/models/media_attachment.rb b/app/models/media_attachment.rb index b567003fb9..c1bc7dac94 100644 --- a/app/models/media_attachment.rb +++ b/app/models/media_attachment.rb @@ -146,6 +146,20 @@ class MediaAttachment < ApplicationRecord original: VIDEO_FORMAT.merge(passthrough_options: VIDEO_PASSTHROUGH_OPTIONS).freeze, }.freeze + AUDIO_PASSTHROUGH_OPTIONS = { + audio_codecs: ['acc'].freeze, + options: { + format: 'mp3', + convert_options: { + output: { + 'loglevel' => 'fatal', + 'map_metadata' => '-1', + 'c:a' => 'copy', + }.freeze, + }.freeze, + }.freeze, + }.freeze + AUDIO_STYLES = { original: { format: 'mp3', @@ -156,6 +170,8 @@ class MediaAttachment < ApplicationRecord 'q:a' => 2, }.freeze, }.freeze, + + passthrough_options: AUDIO_PASSTHROUGH_OPTIONS, }.freeze, }.freeze diff --git a/lib/paperclip/transcoder.rb b/lib/paperclip/transcoder.rb index d2d946d3ad..00fb4767bb 100644 --- a/lib/paperclip/transcoder.rb +++ b/lib/paperclip/transcoder.rb @@ -115,7 +115,11 @@ module Paperclip end def eligible_to_passthrough?(metadata) - @passthrough_options && @passthrough_options[:video_codecs].include?(metadata.video_codec) && @passthrough_options[:audio_codecs].include?(metadata.audio_codec) && @passthrough_options[:colorspaces].include?(metadata.colorspace) + return false if @passthrough_options.blank? + + %i(video_codec audio_codec colorspace).all? do |attribute| + @passthrough_options["#{attribute}s".to_sym].nil? || @passthrough_options["#{attribute}s".to_sym].include?(metadata.public_send(attribute)) + end end def update_attachment_type(metadata)