Add previews to video (MP4, WEBM) posts

This commit is contained in:
tinsukE 2023-09-07 16:45:22 +02:00
parent 89583cfe29
commit 1c75710424
2 changed files with 50 additions and 0 deletions

View File

@ -9,6 +9,7 @@ import eu.toldi.infinityforlemmy.apis.RedgifsAPI;
import eu.toldi.infinityforlemmy.post.enrich.CompositePostEnricher;
import eu.toldi.infinityforlemmy.post.enrich.PostEnricher;
import eu.toldi.infinityforlemmy.post.enrich.RedGifsPostEnricher;
import eu.toldi.infinityforlemmy.post.enrich.VideoPostEnricher;
@Module
abstract class PostEnricherModule {
@ -19,6 +20,12 @@ abstract class PostEnricherModule {
return new RedGifsPostEnricher(redgifsAPI);
}
@Provides
@IntoSet
static PostEnricher provideVideoPostEnricher() {
return new VideoPostEnricher();
}
@Provides
static PostEnricher providePostEnricher(Set<PostEnricher> postEnrichers) {
return new CompositePostEnricher(postEnrichers);

View File

@ -0,0 +1,43 @@
package eu.toldi.infinityforlemmy.post.enrich
import android.media.MediaMetadataRetriever
import android.util.Log
import eu.toldi.infinityforlemmy.post.Post
class VideoPostEnricher : PostEnricher {
override fun enrich(posts: Collection<Post>) {
for (post in posts) {
if (post.postType != Post.VIDEO_TYPE || post.previews.isNotEmpty()) {
continue
}
val url = post.videoUrl ?: continue
if (!url.endsWith(".mp4", ignoreCase = true) &&
!url.endsWith(".webm", ignoreCase = true)
) {
continue
}
val retriever = try {
MediaMetadataRetriever().apply {
setDataSource(url);
}
} catch (e: Exception) {
Log.w(TAG, "Error retrieving metadata", e)
continue
}
val width = retriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_VIDEO_WIDTH)
?.toIntOrNull() ?: -1
val height = retriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_VIDEO_HEIGHT)
?.toIntOrNull() ?: -1
// Glide can extract thumbnails from video URLs (it uses MediaMetadataRetriever too)
post.previews = ArrayList(listOf(Post.Preview(url, width, height, null, null)))
}
}
companion object {
private const val TAG = "VideoPostEnricher"
}
}