Use new bps api of lightcompressorlibrary fork

This commit is contained in:
davotoula
2025-09-25 21:41:28 +02:00
parent 4c3eaf972c
commit 6e4e432adf

View File

@@ -38,10 +38,8 @@ import kotlinx.coroutines.withTimeoutOrNull
import java.io.File import java.io.File
import java.util.UUID import java.util.UUID
import kotlin.coroutines.resume import kotlin.coroutines.resume
import kotlin.math.roundToInt
// TODO: add Auto setting. Focus on small fast streams. 4->1080p, 1080p->720p, 720p and below stay the same resolution. Use existing matrix to determine bitrate. // TODO: add Auto setting. Focus on small fast streams. 4->1080p, 1080p->720p, 720p and below stay the same resolution. Use existing matrix to determine bitrate.
// TODO: use bps api and don't floor at 1Mbps
data class VideoInfo( data class VideoInfo(
val resolution: VideoResolution, val resolution: VideoResolution,
val framerate: Float, val framerate: Float,
@@ -82,18 +80,19 @@ enum class VideoStandard(
override fun toString(): String = label override fun toString(): String = label
} }
private const val MBPS_TO_BPS_MULTIPLIER = 1_000_000
data class CompressionRule( data class CompressionRule(
val width: Int, val width: Int,
val height: Int, val height: Int,
val bitrateMbps: Float, val bitrateMbps: Float,
val description: String, val description: String,
) { ) {
fun getBitrateMbpsInt(framerate: Float): Int { fun getBitrateBps(framerate: Float): Int {
// Apply 1.5x multiplier for 60fps+ videos // Apply 1.5x multiplier for 60fps+ videos
val multiplier = if (framerate >= 60f) 1.5f else 1.0f val multiplier = if (framerate >= 60f) 1.5f else 1.0f
// Library doesn't support float so we have to convert it to int and use 1 as minimum return (bitrateMbps * multiplier).toInt() * MBPS_TO_BPS_MULTIPLIER
return (bitrateMbps * multiplier).roundToInt().coerceAtLeast(1)
} }
} }
@@ -146,22 +145,19 @@ object VideoCompressionHelper {
): MediaCompressorResult { ): MediaCompressorResult {
val videoInfo = getVideoInfo(uri, applicationContext) val videoInfo = getVideoInfo(uri, applicationContext)
val videoBitrateInMbps = val videoBitrateInBps =
if (videoInfo != null) { videoInfo?.let { info ->
val bitrateMbpsInt = val bitrateBps =
compressionRules compressionRules
.getValue(mediaQuality) .getValue(mediaQuality)
.getValue(videoInfo.resolution.getStandard()) .getValue(info.resolution.getStandard())
.getBitrateMbpsInt(videoInfo.framerate) .getBitrateBps(info.framerate)
Log.d( Log.d(LOG_TAG, "Bitrate: ${bitrateBps}bps for ${info.resolution.getStandard()} quality=$mediaQuality framerate=${info.framerate}fps.")
LOG_TAG, bitrateBps
"Bitrate: ${bitrateMbpsInt}Mbps for ${videoInfo.resolution.getStandard()} " + } ?: run {
"quality=$mediaQuality framerate=${videoInfo.framerate}fps.",
)
} else {
Log.w(LOG_TAG, "Video bitrate fallback: 2Mbps (videoInfo unavailable)") Log.w(LOG_TAG, "Video bitrate fallback: 2Mbps (videoInfo unavailable)")
2 2 * MBPS_TO_BPS_MULTIPLIER
} }
val resizer = val resizer =
@@ -194,7 +190,7 @@ object VideoCompressionHelper {
storageConfiguration = AppSpecificStorageConfiguration(), storageConfiguration = AppSpecificStorageConfiguration(),
configureWith = configureWith =
Configuration( Configuration(
videoBitrateInMbps = videoBitrateInMbps, videoBitrateInBps = videoBitrateInBps.toLong(),
resizer = resizer, resizer = resizer,
videoNames = listOf(UUID.randomUUID().toString()), videoNames = listOf(UUID.randomUUID().toString()),
isMinBitrateCheckEnabled = false, isMinBitrateCheckEnabled = false,