Optimize media: always compress video to have maximum 1080 at the greatest size.
This commit is contained in:
parent
8feff65378
commit
ea73ee0481
2 changed files with 17 additions and 13 deletions
|
|
@ -171,17 +171,13 @@ class AndroidMediaPreProcessor @Inject constructor(
|
||||||
}
|
}
|
||||||
|
|
||||||
private suspend fun processVideo(uri: Uri, mimeType: String?, shouldBeCompressed: Boolean): MediaUploadInfo {
|
private suspend fun processVideo(uri: Uri, mimeType: String?, shouldBeCompressed: Boolean): MediaUploadInfo {
|
||||||
val resultFile = if (shouldBeCompressed) {
|
val resultFile = videoCompressor.compress(uri, shouldBeCompressed)
|
||||||
videoCompressor.compress(uri)
|
.onEach {
|
||||||
.onEach {
|
// TODO handle progress
|
||||||
// TODO handle progress
|
}
|
||||||
}
|
.filterIsInstance<VideoTranscodingEvent.Completed>()
|
||||||
.filterIsInstance<VideoTranscodingEvent.Completed>()
|
.first()
|
||||||
.first()
|
.file
|
||||||
.file
|
|
||||||
} else {
|
|
||||||
copyToTmpFile(uri)
|
|
||||||
}
|
|
||||||
val thumbnailInfo = thumbnailFactory.createVideoThumbnail(resultFile)
|
val thumbnailInfo = thumbnailFactory.createVideoThumbnail(resultFile)
|
||||||
val videoInfo = extractVideoMetadata(resultFile, mimeType, thumbnailInfo)
|
val videoInfo = extractVideoMetadata(resultFile, mimeType, thumbnailInfo)
|
||||||
return MediaUploadInfo.Video(
|
return MediaUploadInfo.Video(
|
||||||
|
|
|
||||||
|
|
@ -24,12 +24,20 @@ import javax.inject.Inject
|
||||||
class VideoCompressor @Inject constructor(
|
class VideoCompressor @Inject constructor(
|
||||||
@ApplicationContext private val context: Context,
|
@ApplicationContext private val context: Context,
|
||||||
) {
|
) {
|
||||||
fun compress(uri: Uri) = callbackFlow {
|
fun compress(uri: Uri, shouldBeCompressed: Boolean) = callbackFlow {
|
||||||
val tmpFile = context.createTmpFile(extension = "mp4")
|
val tmpFile = context.createTmpFile(extension = "mp4")
|
||||||
val future = Transcoder.into(tmpFile.path)
|
val future = Transcoder.into(tmpFile.path)
|
||||||
.setVideoTrackStrategy(
|
.setVideoTrackStrategy(
|
||||||
DefaultVideoStrategy.Builder()
|
DefaultVideoStrategy.Builder()
|
||||||
.addResizer(AtMostResizer(720, 480))
|
.addResizer(
|
||||||
|
AtMostResizer(
|
||||||
|
if (shouldBeCompressed) {
|
||||||
|
720
|
||||||
|
} else {
|
||||||
|
1080
|
||||||
|
}
|
||||||
|
)
|
||||||
|
)
|
||||||
.build()
|
.build()
|
||||||
)
|
)
|
||||||
.addDataSource(context, uri)
|
.addDataSource(context, uri)
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue