Optimize media: always compress video to have maximum 1080 at the greatest size.

This commit is contained in:
Benoit Marty
2024-10-31 10:53:44 +01:00
parent 8feff65378
commit ea73ee0481
2 changed files with 17 additions and 13 deletions

View File

@@ -171,17 +171,13 @@ class AndroidMediaPreProcessor @Inject constructor(
}
private suspend fun processVideo(uri: Uri, mimeType: String?, shouldBeCompressed: Boolean): MediaUploadInfo {
val resultFile = if (shouldBeCompressed) {
videoCompressor.compress(uri)
.onEach {
// TODO handle progress
}
.filterIsInstance<VideoTranscodingEvent.Completed>()
.first()
.file
} else {
copyToTmpFile(uri)
}
val resultFile = videoCompressor.compress(uri, shouldBeCompressed)
.onEach {
// TODO handle progress
}
.filterIsInstance<VideoTranscodingEvent.Completed>()
.first()
.file
val thumbnailInfo = thumbnailFactory.createVideoThumbnail(resultFile)
val videoInfo = extractVideoMetadata(resultFile, mimeType, thumbnailInfo)
return MediaUploadInfo.Video(

View File

@@ -24,12 +24,20 @@ import javax.inject.Inject
class VideoCompressor @Inject constructor(
@ApplicationContext private val context: Context,
) {
fun compress(uri: Uri) = callbackFlow {
fun compress(uri: Uri, shouldBeCompressed: Boolean) = callbackFlow {
val tmpFile = context.createTmpFile(extension = "mp4")
val future = Transcoder.into(tmpFile.path)
.setVideoTrackStrategy(
DefaultVideoStrategy.Builder()
.addResizer(AtMostResizer(720, 480))
.addResizer(
AtMostResizer(
if (shouldBeCompressed) {
720
} else {
1080
}
)
)
.build()
)
.addDataSource(context, uri)