Merge pull request #7351 from hayaksi1/fix/4191-video-bitrate-clamp

Never re-encode a video at a higher bitrate than the source
This commit is contained in:
Benoit Marty
2026-08-11 17:18:50 +02:00
committed by GitHub
2 changed files with 45 additions and 2 deletions
@@ -34,11 +34,14 @@ internal object VideoCompressorConfigFactory {
val newFrameRate = min(originalFrameRate, DEFAULT_FRAME_RATE) val newFrameRate = min(originalFrameRate, DEFAULT_FRAME_RATE)
// If we need to resize the video, we also want to recalculate the bitrate // If we need to resize the video, we also want to recalculate the bitrate
val newBitrate = resizer.calculateOptimalBitrate(Size(width, height), newFrameRate) val optimalBitrate = resizer.calculateOptimalBitrate(Size(width, height), newFrameRate).toInt()
val sourceBitrate = metadata?.bitrate?.takeIf { it > 0 }
val newBitrate = sourceBitrate?.let { min(optimalBitrate.toLong(), it).toInt() } ?: optimalBitrate
return VideoCompressorConfig( return VideoCompressorConfig(
videoCompressorHelper = resizer, videoCompressorHelper = resizer,
newBitRate = newBitrate.toInt(), newBitRate = newBitrate,
newFrameRate = newFrameRate, newFrameRate = newFrameRate,
) )
} }
@@ -33,6 +33,46 @@ class VideoCompressorConfigFactoryTest : RobolectricTest() {
assertThat(videoCompressorConfig.newBitRate).isNotEqualTo(VideoEncoderSettings.NO_VALUE) assertThat(videoCompressorConfig.newBitRate).isNotEqualTo(VideoEncoderSettings.NO_VALUE)
} }
@Test
fun `the bitrate is clamped to the source bitrate when the source is already low`() {
val metadata = VideoFileMetadata(width = 720, height = 720, bitrate = 615_000, frameRate = 25, rotation = 0)
val videoCompressorConfig = VideoCompressorConfigFactory.create(
metadata = metadata,
preset = VideoCompressionPreset.STANDARD,
)
assertThat(videoCompressorConfig.newBitRate).isEqualTo(615_000)
}
@Test
fun `the bitrate is not raised for a high bitrate source`() {
val metadata = VideoFileMetadata(width = 1920, height = 1080, bitrate = 20_000_000, frameRate = 30, rotation = 0)
val videoCompressorConfig = VideoCompressorConfigFactory.create(
metadata = metadata,
preset = VideoCompressionPreset.STANDARD,
)
assertThat(videoCompressorConfig.newBitRate).isLessThan(20_000_000)
assertThat(videoCompressorConfig.newBitRate).isGreaterThan(0)
}
@Test
fun `an unknown bitrate falls back to the calculated optimal`() {
val unknown = VideoCompressorConfigFactory.create(
metadata = VideoFileMetadata(width = 1280, height = 720, bitrate = -1, frameRate = 30, rotation = 0),
preset = VideoCompressionPreset.STANDARD,
)
val zero = VideoCompressorConfigFactory.create(
metadata = VideoFileMetadata(width = 1280, height = 720, bitrate = 0, frameRate = 30, rotation = 0),
preset = VideoCompressionPreset.STANDARD,
)
assertThat(unknown.newBitRate).isGreaterThan(0)
assertThat(zero.newBitRate).isEqualTo(unknown.newBitRate)
}
@Test @Test
fun `if the video should be compressed and is larger than 720p it will be resized`() { fun `if the video should be compressed and is larger than 720p it will be resized`() {
// Given // Given