move framerate adjustment into getBitrateMbpsInt to prevent ,precision errors
Clean up logging
This commit is contained in:
+23
-36
@@ -86,18 +86,19 @@ data class CompressionRule(
|
|||||||
val bitrateMbps: Float,
|
val bitrateMbps: Float,
|
||||||
val description: String,
|
val description: String,
|
||||||
) {
|
) {
|
||||||
fun getBitrateMbpsInt(): Int {
|
fun getBitrateMbpsInt(framerate: Float): Int {
|
||||||
|
// Apply 1.5x multiplier for 60fps+ videos
|
||||||
|
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
|
// Library doesn't support float so we have to convert it to int and use 1 as minimum
|
||||||
return if (bitrateMbps < 1) {
|
return (bitrateMbps * multiplier).roundToInt().coerceAtLeast(1)
|
||||||
1
|
|
||||||
} else {
|
|
||||||
bitrateMbps.roundToInt()
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
class VideoCompressionHelper {
|
class VideoCompressionHelper {
|
||||||
companion object {
|
companion object {
|
||||||
|
private const val LOG_TAG = "VideoCompressionHelper"
|
||||||
|
|
||||||
private val compressionRules =
|
private val compressionRules =
|
||||||
mapOf(
|
mapOf(
|
||||||
CompressorQuality.LOW to
|
CompressorQuality.LOW to
|
||||||
@@ -147,28 +148,19 @@ class VideoCompressionHelper {
|
|||||||
|
|
||||||
val videoBitrateInMbps =
|
val videoBitrateInMbps =
|
||||||
if (videoInfo != null) {
|
if (videoInfo != null) {
|
||||||
val baseBitrate =
|
val bitrateMbpsInt =
|
||||||
compressionRules
|
compressionRules
|
||||||
.getValue(mediaQuality)
|
.getValue(mediaQuality)
|
||||||
.getValue(videoInfo.resolution.getStandard())
|
.getValue(videoInfo.resolution.getStandard())
|
||||||
.getBitrateMbpsInt()
|
.getBitrateMbpsInt(videoInfo.framerate)
|
||||||
|
|
||||||
// Apply 1.5x multiplier for 60fps+
|
|
||||||
val adjusted =
|
|
||||||
if (videoInfo.framerate >= 60f) {
|
|
||||||
(baseBitrate * 1.5f).roundToInt()
|
|
||||||
} else {
|
|
||||||
baseBitrate
|
|
||||||
}
|
|
||||||
|
|
||||||
Log.d(
|
Log.d(
|
||||||
"VideoCompressionHelper",
|
LOG_TAG,
|
||||||
"Bitrate: ${adjusted}Mbps for ${videoInfo.resolution.getStandard()} " +
|
"Bitrate: ${bitrateMbpsInt}Mbps for ${videoInfo.resolution.getStandard()} " +
|
||||||
"quality=$mediaQuality framerate=${videoInfo.framerate}fps",
|
"quality=$mediaQuality framerate=${videoInfo.framerate}fps.",
|
||||||
)
|
)
|
||||||
adjusted
|
|
||||||
} else {
|
} else {
|
||||||
Log.w("VideoCompressionHelper", "Video bitrate fallback: 2Mbps (videoInfo unavailable)")
|
Log.w(LOG_TAG, "Video bitrate fallback: 2Mbps (videoInfo unavailable)")
|
||||||
2
|
2
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -179,13 +171,13 @@ class VideoCompressionHelper {
|
|||||||
.getValue(mediaQuality)
|
.getValue(mediaQuality)
|
||||||
.getValue(videoInfo.resolution.getStandard())
|
.getValue(videoInfo.resolution.getStandard())
|
||||||
Log.d(
|
Log.d(
|
||||||
"VideoCompressionHelper",
|
LOG_TAG,
|
||||||
"Resizer: ${videoInfo.resolution.width}x${videoInfo.resolution.height} -> " +
|
"Resizer: ${videoInfo.resolution.width}x${videoInfo.resolution.height} -> " +
|
||||||
"${rules.width}x${rules.height} (${rules.description})",
|
"${rules.width}x${rules.height} (${rules.description})",
|
||||||
)
|
)
|
||||||
VideoResizer.limitSize(rules.width.toDouble(), rules.height.toDouble())
|
VideoResizer.limitSize(rules.width.toDouble(), rules.height.toDouble())
|
||||||
} else {
|
} else {
|
||||||
Log.d("VideoCompressionHelper", "Resizer: null (original resolution preserved)")
|
Log.d(LOG_TAG, "Resizer: null (original resolution preserved)")
|
||||||
null
|
null
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -224,7 +216,6 @@ class VideoCompressionHelper {
|
|||||||
if (path == null) {
|
if (path == null) {
|
||||||
applicationContext.notifyUser(
|
applicationContext.notifyUser(
|
||||||
"Video compression succeeded, but path was null",
|
"Video compression succeeded, but path was null",
|
||||||
"VideoCompressionHelper",
|
|
||||||
Log.WARN,
|
Log.WARN,
|
||||||
)
|
)
|
||||||
if (continuation.isActive) continuation.resume(null)
|
if (continuation.isActive) continuation.resume(null)
|
||||||
@@ -242,7 +233,6 @@ class VideoCompressionHelper {
|
|||||||
if (originalSize > 0 && size >= originalSize) {
|
if (originalSize > 0 && size >= originalSize) {
|
||||||
applicationContext.notifyUser(
|
applicationContext.notifyUser(
|
||||||
"Compressed file larger than original. Using original.",
|
"Compressed file larger than original. Using original.",
|
||||||
"VideoCompressionHelper",
|
|
||||||
Log.WARN,
|
Log.WARN,
|
||||||
)
|
)
|
||||||
if (continuation.isActive) {
|
if (continuation.isActive) {
|
||||||
@@ -260,12 +250,11 @@ class VideoCompressionHelper {
|
|||||||
if (reductionPercent >= 0) "-$reductionPercent%" else "+${-reductionPercent}%"
|
if (reductionPercent >= 0) "-$reductionPercent%" else "+${-reductionPercent}%"
|
||||||
applicationContext.notifyUser(
|
applicationContext.notifyUser(
|
||||||
"Video compressed: $sizeLabel ($percentLabel)",
|
"Video compressed: $sizeLabel ($percentLabel)",
|
||||||
"VideoCompressionHelper",
|
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
Log.d(
|
Log.d(
|
||||||
"VideoCompressionHelper",
|
LOG_TAG,
|
||||||
"Compression success: Original [$originalSize] -> " +
|
"Compression success: Original [$originalSize] -> " +
|
||||||
"Compressed [$size] ($reductionPercent% reduction)",
|
"Compressed [$size] ($reductionPercent% reduction)",
|
||||||
)
|
)
|
||||||
@@ -290,14 +279,13 @@ class VideoCompressionHelper {
|
|||||||
) {
|
) {
|
||||||
applicationContext.notifyUser(
|
applicationContext.notifyUser(
|
||||||
"Video compression failed: $failureMessage",
|
"Video compression failed: $failureMessage",
|
||||||
"VideoCompressionHelper",
|
|
||||||
Log.ERROR,
|
Log.ERROR,
|
||||||
)
|
)
|
||||||
if (continuation.isActive) continuation.resume(null)
|
if (continuation.isActive) continuation.resume(null)
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun onCancelled(index: Int) {
|
override fun onCancelled(index: Int) {
|
||||||
Log.w("VideoCompressionHelper", "Video compression cancelled")
|
Log.w(LOG_TAG, "Video compression cancelled")
|
||||||
if (continuation.isActive) continuation.resume(null)
|
if (continuation.isActive) continuation.resume(null)
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
@@ -315,13 +303,12 @@ class VideoCompressionHelper {
|
|||||||
if (cursor.moveToFirst()) cursor.getLong(sizeIndex) else 0L
|
if (cursor.moveToFirst()) cursor.getLong(sizeIndex) else 0L
|
||||||
} ?: 0L
|
} ?: 0L
|
||||||
} catch (e: Exception) {
|
} catch (e: Exception) {
|
||||||
Log.w("VideoCompressionHelper", "Failed to get file size: ${e.message}")
|
Log.w(LOG_TAG, "Failed to get file size: ${e.message}")
|
||||||
0L
|
0L
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun Context.notifyUser(
|
private fun Context.notifyUser(
|
||||||
message: String,
|
message: String,
|
||||||
logTag: String,
|
|
||||||
logLevel: Int = Log.DEBUG,
|
logLevel: Int = Log.DEBUG,
|
||||||
duration: Int = Toast.LENGTH_LONG,
|
duration: Int = Toast.LENGTH_LONG,
|
||||||
) {
|
) {
|
||||||
@@ -329,9 +316,9 @@ class VideoCompressionHelper {
|
|||||||
Toast.makeText(this, message, duration).show()
|
Toast.makeText(this, message, duration).show()
|
||||||
}
|
}
|
||||||
when (logLevel) {
|
when (logLevel) {
|
||||||
Log.ERROR -> Log.e(logTag, message)
|
Log.ERROR -> Log.e(LOG_TAG, message)
|
||||||
Log.WARN -> Log.w(logTag, message)
|
Log.WARN -> Log.w(LOG_TAG, message)
|
||||||
else -> Log.d(logTag, message)
|
else -> Log.d(LOG_TAG, message)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -364,13 +351,13 @@ class VideoCompressionHelper {
|
|||||||
null
|
null
|
||||||
}
|
}
|
||||||
} catch (e: Exception) {
|
} catch (e: Exception) {
|
||||||
Log.w("VideoCompressionHelper", "Failed to get video resolution: ${e.message}")
|
Log.w(LOG_TAG, "Failed to get video resolution: ${e.message}")
|
||||||
null
|
null
|
||||||
} finally {
|
} finally {
|
||||||
try {
|
try {
|
||||||
retriever?.release()
|
retriever?.release()
|
||||||
} catch (e: Exception) {
|
} catch (e: Exception) {
|
||||||
Log.w("VideoCompressionHelper", "Failed to release MediaMetadataRetriever: ${e.message}")
|
Log.w(LOG_TAG, "Failed to release MediaMetadataRetriever: ${e.message}")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user