mirror of
https://github.com/vitorpamplona/amethyst.git
synced 2025-11-10 22:56:41 +01:00
add bitrate multiplier for 60fps content
fix toast when new file larger than old
This commit is contained in:
@@ -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 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 5. Add toast message about file size saving
|
||||||
* xxx 6. refactor (helper class for video compression)
|
* 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
|
* Don't use Configuration.quality which only determines bitrate. Instead let's create aggressive bitrates based on input and selected quality
|
||||||
|
|||||||
@@ -23,6 +23,8 @@ package com.vitorpamplona.amethyst.service.uploads
|
|||||||
import android.content.Context
|
import android.content.Context
|
||||||
import android.media.MediaMetadataRetriever
|
import android.media.MediaMetadataRetriever
|
||||||
import android.net.Uri
|
import android.net.Uri
|
||||||
|
import android.os.Handler
|
||||||
|
import android.os.Looper
|
||||||
import android.text.format.Formatter.formatFileSize
|
import android.text.format.Formatter.formatFileSize
|
||||||
import android.util.Log
|
import android.util.Log
|
||||||
import android.widget.Toast
|
import android.widget.Toast
|
||||||
@@ -128,9 +130,16 @@ class VideoCompressionHelper {
|
|||||||
|
|
||||||
val videoBitrateInMbps =
|
val videoBitrateInMbps =
|
||||||
if (videoInfo != null) {
|
if (videoInfo != null) {
|
||||||
val bitrate = compressionRules.getValue(mediaQuality).getValue(videoInfo.resolution.getStandardName()).getBitrateMbpsInt()
|
val baseBitrate = compressionRules.getValue(mediaQuality).getValue(videoInfo.resolution.getStandardName()).getBitrateMbpsInt()
|
||||||
Log.d("VideoCompressionHelper", "Video bitrate calculated: ${bitrate}Mbps for ${videoInfo.resolution.getStandardName()} quality=$mediaQuality")
|
// Apply 1.5x multiplier for 60fps or higher videos
|
||||||
bitrate
|
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 {
|
} else {
|
||||||
// Default/fallback logic when videoInfo is null
|
// Default/fallback logic when videoInfo is null
|
||||||
Log.d("VideoCompressionHelper", "Video bitrate fallback: 2Mbps (videoInfo unavailable)")
|
Log.d("VideoCompressionHelper", "Video bitrate fallback: 2Mbps (videoInfo unavailable)")
|
||||||
@@ -188,7 +197,8 @@ class VideoCompressionHelper {
|
|||||||
override fun onProgress(
|
override fun onProgress(
|
||||||
index: Int,
|
index: Int,
|
||||||
percent: Float,
|
percent: Float,
|
||||||
) {}
|
) {
|
||||||
|
}
|
||||||
|
|
||||||
override fun onStart(index: Int) {}
|
override fun onStart(index: Int) {}
|
||||||
|
|
||||||
@@ -198,13 +208,6 @@ class VideoCompressionHelper {
|
|||||||
path: String?,
|
path: String?,
|
||||||
) {
|
) {
|
||||||
if (path != null) {
|
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 =
|
val reductionPercent =
|
||||||
if (originalSize > 0) {
|
if (originalSize > 0) {
|
||||||
((originalSize - size) * 100.0 / originalSize).toInt()
|
((originalSize - size) * 100.0 / originalSize).toInt()
|
||||||
@@ -212,16 +215,19 @@ class VideoCompressionHelper {
|
|||||||
0
|
0
|
||||||
}
|
}
|
||||||
|
|
||||||
// Show compression result toast
|
// Sanity check: if compressed file is larger than original, return original
|
||||||
if (originalSize > 0 && size > 0) {
|
if (originalSize > 0 && size >= originalSize) {
|
||||||
val message =
|
Log.d("VideoCompressionHelper", "Compressed file ($size bytes) is larger than original ($originalSize bytes). Using original file.")
|
||||||
"Video compressed: ${formatFileSize(applicationContext, size)} " +
|
applicationContext.showToast("Video compression didn't reduce size. Using original file.")
|
||||||
"(${if (reductionPercent > 0) "-$reductionPercent%" else "+${-reductionPercent}%"})"
|
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)")
|
Log.d("VideoCompressionHelper", "Video compression success. Original size [$originalSize] -> Compressed size [$size] ($reductionPercent% reduction)")
|
||||||
continuation.resume(MediaCompressorResult(Uri.fromFile(File(path)), contentType, size))
|
continuation.resume(MediaCompressorResult(Uri.fromFile(File(path)), contentType, size))
|
||||||
@@ -251,6 +257,15 @@ class VideoCompressionHelper {
|
|||||||
return result ?: MediaCompressorResult(uri, contentType, null)
|
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(
|
private fun getVideoInfo(
|
||||||
uri: Uri,
|
uri: Uri,
|
||||||
context: Context,
|
context: Context,
|
||||||
|
|||||||
Reference in New Issue
Block a user