Improve video preview loading time

This commit improves the loading time of posts with videos on the feed.

Closes #242
This commit is contained in:
Balazs Toldi 2024-07-18 21:32:53 +02:00
parent 706f51c4f5
commit b9e61518a4
3 changed files with 3 additions and 51 deletions

View File

@ -9,7 +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 {
@ -20,12 +20,6 @@ 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

@ -249,7 +249,8 @@ public class ParsePost {
post = new Post(id, communityInfo, author, postTimeMillis, title, permalink, downvotes, upvotes, postType, voteType, post = new Post(id, communityInfo, author, postTimeMillis, title, permalink, downvotes, upvotes, postType, voteType,
nComments, upvoteRatio, nsfw, locked, saved, deleted, distinguished, suggestedSort); nComments, upvoteRatio, nsfw, locked, saved, deleted, distinguished, suggestedSort);
Post.Preview preview = new Post.Preview(url, 0, 0, "", "");
post.setPreviews(new ArrayList<>(List.of(preview)));
post.setVideoUrl(url); post.setVideoUrl(url);
post.setVideoDownloadUrl(url); post.setVideoDownloadUrl(url);
} else if (!url.equals("")) { } else if (!url.equals("")) {

View File

@ -1,43 +0,0 @@
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"
}
}