Merge pull request 'Add previews to video (MP4, WEBM) posts' (#225) from tinsukE/Eternity:video-preview into master

Reviewed-on: https://codeberg.org/Bazsalanszky/Eternity/pulls/225
Reviewed-by: Bazsalanszky <bazsalanszky@noreply.codeberg.org>
This commit is contained in:
Bazsalanszky 2024-01-03 13:39:25 +00:00
commit 84653d7677
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.CompositePostEnricher;
import eu.toldi.infinityforlemmy.post.enrich.PostEnricher; import eu.toldi.infinityforlemmy.post.enrich.PostEnricher;
import eu.toldi.infinityforlemmy.post.enrich.RedGifsPostEnricher; import eu.toldi.infinityforlemmy.post.enrich.RedGifsPostEnricher;
import eu.toldi.infinityforlemmy.post.enrich.VideoPostEnricher;
@Module @Module
abstract class PostEnricherModule { abstract class PostEnricherModule {
@ -19,6 +20,12 @@ abstract class PostEnricherModule {
return new RedGifsPostEnricher(redgifsAPI); return new RedGifsPostEnricher(redgifsAPI);
} }
@Provides
@IntoSet
static PostEnricher provideVideoPostEnricher() {
return new VideoPostEnricher();
}
@Provides @Provides
static PostEnricher providePostEnricher(Set<PostEnricher> postEnrichers) { static PostEnricher providePostEnricher(Set<PostEnricher> postEnrichers) {
return new CompositePostEnricher(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"
}
}