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.content.Context
import android.graphics.Bitmap import android.graphics.Bitmap
import android.media.MediaMetadataRetriever
import android.net.Uri import android.net.Uri
import android.util.Log import android.util.Log
import androidx.annotation.WorkerThread import androidx.annotation.WorkerThread
@ -13,6 +12,7 @@ import kotlinx.coroutines.isActive
import kotlin.math.absoluteValue import kotlin.math.absoluteValue
import kotlin.math.ceil import kotlin.math.ceil
import kotlin.math.log2 import kotlin.math.log2
import wseemann.media.FFmpegMediaMetadataRetriever
const val MAX_LOD = 6 const val MAX_LOD = 6
const val MIN_LOD = 3 const val MIN_LOD = 3
@ -67,8 +67,7 @@ class PreviewGenerator {
} }
} }
// also check out https://github.com/wseemann/FFmpegMediaMetadataRetriever private val retriever: FFmpegMediaMetadataRetriever = FFmpegMediaMetadataRetriever()
private val retriever: MediaMetadataRetriever = MediaMetadataRetriever()
fun clear(keepCache: Boolean = false) { fun clear(keepCache: Boolean = false) {
if (keepCache) return if (keepCache) return
@ -111,11 +110,11 @@ class PreviewGenerator {
Log.i(TAG, "Started loading preview") Log.i(TAG, "Started loading preview")
val durationMs = val durationMs =
retriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_DURATION)?.toLong() retriever.extractMetadata(FFmpegMediaMetadataRetriever.METADATA_KEY_DURATION)?.toLong()
?: throw IllegalArgumentException("Bad video duration") ?: throw IllegalArgumentException("Bad video duration")
val durationUs = (durationMs * 1000L).toFloat() val durationUs = (durationMs * 1000L).toFloat()
//val width = retriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_VIDEO_WIDTH)?.toInt() ?: throw IllegalArgumentException("Bad video width") //val width = retriever.extractMetadata(FFmpegMediaMetadataRetriever.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 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 // 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) 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 frame = durationUs * fraction
val img = retriever.getFrameAtTime( val img = retriever.getFrameAtTime(
frame.toLong(), frame.toLong(),
MediaMetadataRetriever.OPTION_CLOSEST_SYNC FFmpegMediaMetadataRetriever.OPTION_CLOSEST_SYNC
) )
if (!scope.isActive) return if (!scope.isActive) return
synchronized(images) { synchronized(images) {