From 947f59fa6faa3f8b515c9f84ee9fe256a792e30b Mon Sep 17 00:00:00 2001 From: davotoula Date: Mon, 22 Sep 2025 21:09:53 +0200 Subject: [PATCH] add bitrate multiplier for 60fps content fix toast when new file larger than old --- .../service/uploads/MediaCompressor.kt | 3 +- .../service/uploads/VideoCompressionHelper.kt | 55 ++++++++++++------- 2 files changed, 37 insertions(+), 21 deletions(-) diff --git a/amethyst/src/main/java/com/vitorpamplona/amethyst/service/uploads/MediaCompressor.kt b/amethyst/src/main/java/com/vitorpamplona/amethyst/service/uploads/MediaCompressor.kt index 665547200..f916d0556 100644 --- a/amethyst/src/main/java/com/vitorpamplona/amethyst/service/uploads/MediaCompressor.kt +++ b/amethyst/src/main/java/com/vitorpamplona/amethyst/service/uploads/MediaCompressor.kt @@ -53,7 +53,8 @@ class MediaCompressorResult( * xxx 4. Don't upload converted file if compression results in larger file (return MediaCompressorResult(uri, contentType, null)) * xxx 5. Add toast message about file size saving * xxx 6. refactor (helper class for video compression) - * 7. Fix toast for case when compressed file is larger than original + * xxx 7. Fix toast for case when compressed file is larger than original + * xxx 8. fix ratio multiplier for framerate ->60 * * * Don't use Configuration.quality which only determines bitrate. Instead let's create aggressive bitrates based on input and selected quality diff --git a/amethyst/src/main/java/com/vitorpamplona/amethyst/service/uploads/VideoCompressionHelper.kt b/amethyst/src/main/java/com/vitorpamplona/amethyst/service/uploads/VideoCompressionHelper.kt index f53fae257..51a2e5eaa 100644 --- a/amethyst/src/main/java/com/vitorpamplona/amethyst/service/uploads/VideoCompressionHelper.kt +++ b/amethyst/src/main/java/com/vitorpamplona/amethyst/service/uploads/VideoCompressionHelper.kt @@ -23,6 +23,8 @@ package com.vitorpamplona.amethyst.service.uploads import android.content.Context import android.media.MediaMetadataRetriever import android.net.Uri +import android.os.Handler +import android.os.Looper import android.text.format.Formatter.formatFileSize import android.util.Log import android.widget.Toast @@ -128,9 +130,16 @@ class VideoCompressionHelper { val videoBitrateInMbps = if (videoInfo != null) { - val bitrate = compressionRules.getValue(mediaQuality).getValue(videoInfo.resolution.getStandardName()).getBitrateMbpsInt() - Log.d("VideoCompressionHelper", "Video bitrate calculated: ${bitrate}Mbps for ${videoInfo.resolution.getStandardName()} quality=$mediaQuality") - bitrate + val baseBitrate = compressionRules.getValue(mediaQuality).getValue(videoInfo.resolution.getStandardName()).getBitrateMbpsInt() + // Apply 1.5x multiplier for 60fps or higher videos + val adjustedBitrate = + if (videoInfo.framerate >= 60f) { + (baseBitrate * 1.5f).roundToInt() + } else { + baseBitrate + } + Log.d("VideoCompressionHelper", "Video bitrate calculated: ${adjustedBitrate}Mbps for ${videoInfo.resolution.getStandardName()} quality=$mediaQuality framerate=${videoInfo.framerate}fps") + adjustedBitrate } else { // Default/fallback logic when videoInfo is null Log.d("VideoCompressionHelper", "Video bitrate fallback: 2Mbps (videoInfo unavailable)") @@ -188,7 +197,8 @@ class VideoCompressionHelper { override fun onProgress( index: Int, percent: Float, - ) {} + ) { + } override fun onStart(index: Int) {} @@ -198,13 +208,6 @@ class VideoCompressionHelper { path: String?, ) { if (path != null) { - // Sanity check: if compressed file is larger than original, return original - if (originalSize > 0 && size >= originalSize) { - Log.d("VideoCompressionHelper", "Compressed file ($size bytes) is larger than original ($originalSize bytes). Using original file.") - continuation.resume(MediaCompressorResult(uri, contentType, null)) - return - } - val reductionPercent = if (originalSize > 0) { ((originalSize - size) * 100.0 / originalSize).toInt() @@ -212,16 +215,19 @@ class VideoCompressionHelper { 0 } - // Show compression result toast - if (originalSize > 0 && size > 0) { - val message = - "Video compressed: ${formatFileSize(applicationContext, size)} " + - "(${if (reductionPercent > 0) "-$reductionPercent%" else "+${-reductionPercent}%"})" + // Sanity check: if compressed file is larger than original, return original + if (originalSize > 0 && size >= originalSize) { + Log.d("VideoCompressionHelper", "Compressed file ($size bytes) is larger than original ($originalSize bytes). Using original file.") + applicationContext.showToast("Video compression didn't reduce size. Using original file.") + continuation.resume(MediaCompressorResult(uri, contentType, null)) + return + } - // Post on main thread for Toast - android.os.Handler(android.os.Looper.getMainLooper()).post { - Toast.makeText(applicationContext, message, Toast.LENGTH_LONG).show() - } + if (originalSize > 0 && size > 0) { + val sizeLabel = formatFileSize(applicationContext, size) + val percentLabel = if (reductionPercent >= 0) "-$reductionPercent%" else "+${-reductionPercent}%" + + applicationContext.showToast("Video compressed: $sizeLabel ($percentLabel)") } Log.d("VideoCompressionHelper", "Video compression success. Original size [$originalSize] -> Compressed size [$size] ($reductionPercent% reduction)") continuation.resume(MediaCompressorResult(Uri.fromFile(File(path)), contentType, size)) @@ -251,6 +257,15 @@ class VideoCompressionHelper { return result ?: MediaCompressorResult(uri, contentType, null) } + private fun Context.showToast( + message: String, + duration: Int = Toast.LENGTH_LONG, + ) { + Handler(Looper.getMainLooper()).post { + Toast.makeText(this, message, duration).show() + } + } + private fun getVideoInfo( uri: Uri, context: Context,