mirror of
https://codeberg.org/Bazsalanszky/Infinity-For-Lemmy.git
synced 2024-11-10 04:37:25 +01:00
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:
commit
84653d7677
@ -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);
|
||||||
|
@ -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"
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user