Android Coil3视频封面抽取封面帧存Disk缓存,Kotlin(2)
Android Coil3视频封面抽取封面帧存Disk缓存,Kotlin(2)
implementation 'com.github.wseemann:FFmpegMediaMetadataRetriever-core:1.0.20'implementation 'com.github.wseemann:FFmpegMediaMetadataRetriever-native:1.0.20'
主要在 Android Coil3视频封面抽取封面帧存Disk缓存,Kotlin-CSDN博客 的基础上,提升视频解码(基于FFmpegMediaMetadataRetriever快速解码 https://github.com/wseemann/FFmpegMediaMetadataRetriever )后,把视频取出来的封面帧Bitmap写入缓存的性能,提高写缓存和读缓存的速度,通过Bitmap在压缩阶段的压缩格式和质量实现。变为视频封面的有损压缩写入磁盘缓存文件。
PNG -> JPEG
100 -> 80
对比发现,磁盘缓存文件缩小到原先的1/10,十分之一,很显然,读写速度与文件大小尺寸成正比。
import android.graphics.Bitmap
import android.graphics.ImageDecoder
import android.util.Log
import coil3.ImageLoader
import wseemann.media.FFmpegMediaMetadataRetriever
import java.io.BufferedOutputStream
import java.io.FileOutputStreamobject VideoUtil {const val TAG = "fly/VideoUtil"const val BUF_SIZE = 1024 * 32fun decodeVideo(item: MyData?): Bitmap? {var bitmap: Bitmap? = nullvar metadataRetriever: FFmpegMediaMetadataRetriever? = nulltry {metadataRetriever = FFmpegMediaMetadataRetriever()var t = System.currentTimeMillis()metadataRetriever.setDataSource(item?.path)Log.d(TAG, "setDataSource ${System.currentTimeMillis() - t} ms $item")t = System.currentTimeMillis()bitmap = metadataRetriever.frameAtTimeLog.d(TAG, "frameAtTime ${System.currentTimeMillis() - t} ms $item")} catch (e: Exception) {Log.e(TAG, "${e.message} $item")} finally {try {metadataRetriever?.release()} catch (e: Exception) {Log.e(TAG, "release ${e.message} $item")}}return bitmap}fun readCache(il: ImageLoader, item: MyData?): Bitmap? {var bitmap: Bitmap? = nullval snapShot = il.diskCache?.openSnapshot(item.toString())if (snapShot != null) {Log.d(TAG, "命中Disk缓存 $item")val source = ImageDecoder.createSource(snapShot.data.toFile())try {bitmap = ImageDecoder.decodeBitmap(source)} catch (e: Exception) {Log.e(TAG, "读Disk缓存异常 $e $item")}}snapShot?.close()return bitmap}fun writeCache(il: ImageLoader, bitmap: Bitmap?, item: MyData?): Any? {var bool = falseif (bitmap != null) {val editor = il.diskCache?.openEditor(item.toString())var bos: BufferedOutputStream? = nulltry {bos = FileOutputStream(editor?.data?.toFile()).buffered(BUF_SIZE)bitmap.compress(Bitmap.CompressFormat.JPEG, 80, bos)bos.flush()bos.close()editor?.commit()Log.d(TAG, "Bitmap写入Disk缓存 $item")bool = true} catch (e: Exception) {Log.e(TAG, "Bitmap写Disk磁盘异常 $e")} finally {try {bos?.close()} catch (e: Exception) {Log.e(TAG, "$e $item")}}}return bool}
}
Android Coil3视频封面抽取封面帧存Disk缓存,Kotlin-CSDN博客文章浏览阅读707次,点赞23次,收藏9次。本文介绍了一个基于Coil3的Android视频封面抽取实现方案。项目通过MediaStore获取设备视频列表,使用MediaMetadataRetriever提取视频首帧作为缩略图,并采用二级缓存(内存+磁盘)优化性能。核心功能包括:1)声明读写存储权限;2)RecyclerView网格布局展示;3)协程处理耗时操作;4)自定义ImageLoader配置缓存策略(最大2GB)。相比原生方案,该实现通过Coil的缓存机制提升了缩略图加载效率,同时处理了视频损坏等异常情况。相关技术细节可参考作者CSDN博客中https://blog.csdn.net/zhangphil/article/details/150224812Android快速视频解码抽帧FFmpegMediaMetadataRetriever,Kotlin(2)-CSDN博客文章浏览阅读650次,点赞4次,收藏8次。本文介绍了两种Android视频封面提取方案对比:1)原生MediaMetadataRetriever速度较慢;2)第三方FFmpegMediaMetadataRetriever(FFMMR)实现快速抽帧。详细说明了FFMMR的集成方法(添加依赖和权限),并提供了完整的Kotlin实现代码,包括视频列表读取、缓存管理、协程异步处理等核心功能。通过LruCache缓存缩略图提升性能,记录处理耗时和失败情况。相比前文介绍的原生方案,本文重点突出了FFMMR在解码效率和性能上的优势,为需要快速获取视频帧的场景提供
https://blog.csdn.net/zhangphil/article/details/150061648