Use FFmpegMediaMetadataRetriever for generating previews

This commit is contained in:
Luna712 2023-10-02 17:54:14 -06:00 committed by GitHub
parent 1d90858f64
commit 61403b8f59
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -2,7 +2,6 @@ package com.lagradost.cloudstream3.ui.player
import android.content.Context
import android.graphics.Bitmap
import android.media.MediaMetadataRetriever
import android.net.Uri
import android.util.Log
import androidx.annotation.WorkerThread
@ -13,6 +12,7 @@ import kotlinx.coroutines.isActive
import kotlin.math.absoluteValue
import kotlin.math.ceil
import kotlin.math.log2
import wseemann.media.FFmpegMediaMetadataRetriever
const val MAX_LOD = 6
const val MIN_LOD = 3
@ -67,8 +67,7 @@ class PreviewGenerator {
}
}
// also check out https://github.com/wseemann/FFmpegMediaMetadataRetriever
private val retriever: MediaMetadataRetriever = MediaMetadataRetriever()
private val retriever: FFmpegMediaMetadataRetriever = FFmpegMediaMetadataRetriever()
fun clear(keepCache: Boolean = false) {
if (keepCache) return
@ -111,11 +110,11 @@ class PreviewGenerator {
Log.i(TAG, "Started loading preview")
val durationMs =
retriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_DURATION)?.toLong()
retriever.extractMetadata(FFmpegMediaMetadataRetriever.METADATA_KEY_DURATION)?.toLong()
?: throw IllegalArgumentException("Bad video duration")
val durationUs = (durationMs * 1000L).toFloat()
//val width = retriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_VIDEO_WIDTH)?.toInt() ?: throw IllegalArgumentException("Bad video width")
//val height = retriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_VIDEO_HEIGHT)?.toInt() ?: throw IllegalArgumentException("Bad video height")
//val width = retriever.extractMetadata(FFmpegMediaMetadataRetriever.METADATA_KEY_VIDEO_WIDTH)?.toInt() ?: throw IllegalArgumentException("Bad video width")
//val height = retriever.extractMetadata(FFmpegMediaMetadataRetriever.METADATA_KEY_VIDEO_HEIGHT)?.toInt() ?: throw IllegalArgumentException("Bad video height")
// log2 # 10s durations in the video ~= how many segments we have
val maxLod = ceil(log2((durationMs / 10_000).toFloat())).toInt().coerceIn(MIN_LOD, MAX_LOD)
@ -130,7 +129,7 @@ class PreviewGenerator {
val frame = durationUs * fraction
val img = retriever.getFrameAtTime(
frame.toLong(),
MediaMetadataRetriever.OPTION_CLOSEST_SYNC
FFmpegMediaMetadataRetriever.OPTION_CLOSEST_SYNC
)
if (!scope.isActive) return
synchronized(images) {
@ -144,4 +143,4 @@ class PreviewGenerator {
}
}
}
}
}