Skip to content

Commit

Permalink
Change video bitrate to always fit within size limit (mastodon#26970)
Browse files Browse the repository at this point in the history
  • Loading branch information
Gargron authored and renchap committed Sep 22, 2023
1 parent 21bf947 commit 5a5a442
Showing 1 changed file with 8 additions and 5 deletions.
13 changes: 8 additions & 5 deletions lib/paperclip/transcoder.rb
Expand Up @@ -41,11 +41,14 @@ def make
@output_options['vframes'] = 1
when 'mp4'
unless eligible_to_passthrough?(metadata)
bitrate = (metadata.width * metadata.height * 30 * BITS_PER_PIXEL) / 1_000

@output_options['b:v'] = "#{bitrate}k"
@output_options['maxrate'] = "#{bitrate + 192}k"
@output_options['bufsize'] = "#{bitrate * 5}k"
size_limit_in_bits = MediaAttachment::VIDEO_LIMIT * 8
desired_bitrate = (metadata.width * metadata.height * 30 * BITS_PER_PIXEL).floor
maximum_bitrate = (size_limit_in_bits / metadata.duration).floor - 192_000 # Leave some space for the audio stream
bitrate = [desired_bitrate, maximum_bitrate].min

@output_options['b:v'] = bitrate
@output_options['maxrate'] = bitrate + 192_000
@output_options['bufsize'] = bitrate * 5

if high_vfr?(metadata)
@output_options['vsync'] = 'vfr'
Expand Down

0 comments on commit 5a5a442

Please sign in to comment.