Skip to content

Commit fa755e5

Browse files
authored
Use variable bitrate mode when transcoding to ensure compatibility with old devices (#5223)
* Use variable bitrate mode when transcoding This should be compatible with more devices that may lack the needed codecs to properly encode using constant bitrate mode (CBR). * Fix video output size (again)
1 parent 2535b08 commit fa755e5

File tree

3 files changed

+11
-12
lines changed

3 files changed

+11
-12
lines changed

libraries/androidutils/src/main/kotlin/io/element/android/libraries/androidutils/media/VideoCompressorHelper.kt

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,11 @@ class VideoCompressorHelper(
2727
fun getOutputSize(inputSize: Size): Size {
2828
val resultMajor = min(inputSize.major(), maxSize)
2929
val aspectRatio = inputSize.major().toFloat() / inputSize.minor().toFloat()
30-
return Size(resultMajor, (resultMajor / aspectRatio).roundToInt())
30+
return if (inputSize.width >= inputSize.height) {
31+
Size(resultMajor, (resultMajor / aspectRatio).roundToInt())
32+
} else {
33+
Size((resultMajor / aspectRatio).roundToInt(), resultMajor)
34+
}
3135
}
3236

3337
/**

libraries/mediaupload/impl/src/main/kotlin/io/element/android/libraries/mediaupload/impl/ThumbnailFactory.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,8 +123,8 @@ class ThumbnailFactory @Inject constructor(
123123
val thumbnailResult = ThumbnailResult(
124124
file = thumbnailFile,
125125
info = ThumbnailInfo(
126-
height = bitmapThumbnail.height.toLong(),
127126
width = bitmapThumbnail.width.toLong(),
127+
height = bitmapThumbnail.height.toLong(),
128128
mimetype = mimeTypeToThumbnailMimeType(mimeType),
129129
size = thumbnailFile.length()
130130
),

libraries/mediaupload/impl/src/main/kotlin/io/element/android/libraries/mediaupload/impl/VideoCompressor.kt

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,8 @@ class VideoCompressor @Inject constructor(
6161
val width = metadata?.width ?: Int.MAX_VALUE
6262
val height = metadata?.height ?: Int.MAX_VALUE
6363

64-
val videoResizeEffect = videoCompressorConfig.videoCompressorHelper?.let {
65-
val outputSize = it.getOutputSize(Size(width, height))
64+
val videoResizeEffect = run {
65+
val outputSize = videoCompressorConfig.videoCompressorHelper.getOutputSize(Size(width, height))
6666
if (metadata?.rotation == 90 || metadata?.rotation == 270) {
6767
// If the video is rotated, we need to swap width and height
6868
Presentation.createForWidthAndHeight(
@@ -89,19 +89,14 @@ class VideoCompressor @Inject constructor(
8989
val inputMediaItem = MediaItem.fromUri(uri)
9090
val outputMediaItem = EditedMediaItem.Builder(inputMediaItem)
9191
.setFrameRate(newFrameRate)
92-
.run {
93-
if (videoResizeEffect != null) {
94-
setEffects(Effects(emptyList(), listOf(videoResizeEffect)))
95-
} else {
96-
this
97-
}
98-
}
92+
.setEffects(Effects(emptyList(), listOf(videoResizeEffect)))
9993
.build()
10094

10195
val encoderFactory = DefaultEncoderFactory.Builder(context)
10296
.setRequestedVideoEncoderSettings(
10397
VideoEncoderSettings.Builder()
104-
.setBitrateMode(MediaCodecInfo.EncoderCapabilities.BITRATE_MODE_CBR)
98+
// Use VBR which is generally better for quality and compatibility, although slightly worse for file size
99+
.setBitrateMode(MediaCodecInfo.EncoderCapabilities.BITRATE_MODE_VBR)
105100
.setBitrate(newBitrate)
106101
.build()
107102
)

0 commit comments

Comments
 (0)