Change video bitrate to always fit within size limit (#26970)

th-downstream
Eugen Rochko 1 year ago committed by GitHub
parent 416e215991
commit a8e7fb0c4d

@ -41,11 +41,14 @@ module Paperclip
@output_options['vframes'] = 1 @output_options['vframes'] = 1
when 'mp4' when 'mp4'
unless eligible_to_passthrough?(metadata) unless eligible_to_passthrough?(metadata)
bitrate = (metadata.width * metadata.height * 30 * BITS_PER_PIXEL) / 1_000 size_limit_in_bits = MediaAttachment::VIDEO_LIMIT * 8
desired_bitrate = (metadata.width * metadata.height * 30 * BITS_PER_PIXEL).floor
@output_options['b:v'] = "#{bitrate}k" maximum_bitrate = (size_limit_in_bits / metadata.duration).floor - 192_000 # Leave some space for the audio stream
@output_options['maxrate'] = "#{bitrate + 192}k" bitrate = [desired_bitrate, maximum_bitrate].min
@output_options['bufsize'] = "#{bitrate * 5}k"
@output_options['b:v'] = bitrate
@output_options['maxrate'] = bitrate + 192_000
@output_options['bufsize'] = bitrate * 5
if high_vfr?(metadata) if high_vfr?(metadata)
@output_options['vsync'] = 'vfr' @output_options['vsync'] = 'vfr'

Loading…
Cancel
Save