Fix MP4 (H264 + AAC) video files being needlessly re-encoded (#13239)
parent
670d0be32b
commit
ccd8ceac47
|
@ -77,6 +77,22 @@ class MediaAttachment < ApplicationRecord
|
||||||
},
|
},
|
||||||
}.freeze
|
}.freeze
|
||||||
|
|
||||||
|
VIDEO_PASSTHROUGH_OPTIONS = {
|
||||||
|
video_codec_whitelist: ['h264'],
|
||||||
|
audio_codec_whitelist: ['aac', nil],
|
||||||
|
options: {
|
||||||
|
format: 'mp4',
|
||||||
|
convert_options: {
|
||||||
|
output: {
|
||||||
|
'loglevel' => 'fatal',
|
||||||
|
'map_metadata' => '-1',
|
||||||
|
'c:v' => 'copy',
|
||||||
|
'c:a' => 'copy',
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}.freeze
|
||||||
|
|
||||||
VIDEO_STYLES = {
|
VIDEO_STYLES = {
|
||||||
small: {
|
small: {
|
||||||
convert_options: {
|
convert_options: {
|
||||||
|
@ -91,7 +107,7 @@ class MediaAttachment < ApplicationRecord
|
||||||
blurhash: BLURHASH_OPTIONS,
|
blurhash: BLURHASH_OPTIONS,
|
||||||
},
|
},
|
||||||
|
|
||||||
original: VIDEO_FORMAT,
|
original: VIDEO_FORMAT.merge(passthrough_options: VIDEO_PASSTHROUGH_OPTIONS),
|
||||||
}.freeze
|
}.freeze
|
||||||
|
|
||||||
AUDIO_STYLES = {
|
AUDIO_STYLES = {
|
||||||
|
|
|
@ -5,12 +5,20 @@ module Paperclip
|
||||||
# to check when uploaded videos are actually gifv's
|
# to check when uploaded videos are actually gifv's
|
||||||
class VideoTranscoder < Paperclip::Processor
|
class VideoTranscoder < Paperclip::Processor
|
||||||
def make
|
def make
|
||||||
meta = ::Av.cli.identify(@file.path)
|
movie = FFMPEG::Movie.new(@file.path)
|
||||||
|
actual_options = options
|
||||||
|
passthrough_options = actual_options[:passthrough_options]
|
||||||
|
actual_options = passthrough_options[:options] if passthrough?(movie, passthrough_options)
|
||||||
|
|
||||||
attachment.instance.type = MediaAttachment.types[:gifv] unless meta[:audio_encode]
|
attachment.instance.type = MediaAttachment.types[:gifv] unless movie.audio_codec
|
||||||
options[:format] = File.extname(attachment.instance.file_file_name)[1..-1] if options[:keep_same_format]
|
|
||||||
|
|
||||||
Paperclip::Transcoder.make(file, options, attachment)
|
Paperclip::Transcoder.make(file, actual_options, attachment)
|
||||||
|
end
|
||||||
|
|
||||||
|
private
|
||||||
|
|
||||||
|
def passthrough?(movie, options)
|
||||||
|
options && options[:video_codec_whitelist].include?(movie.video_codec) && options[:audio_codec_whitelist].include?(movie.audio_codec)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in New Issue