目前来说,存在很多实现方式。
下面描述其中两种实现方式
1.利用MediaMetadataRetriever实现
利用接口:
/**
* Call this method after setDataSource(). This method finds a
* representative frame close to the given time position if possible,
* and returns it as a bitmap. This is useful for generating a thumbnail
* for an input data source. Call this method if one does not care
* how the frame is found as long as it is close to the given time;
* otherwise, please call {@link #getFrameAtTime(long, int)}.
*
* @param timeUs The time position where the frame will be retrieved.
* When retrieving the frame at the given time position, there is no
* guarentee that the data source has a frame located at the position.
* When this happens, a frame nearby will be returned. If timeUs is
* negative, time position and option will ignored, and any frame
* that the implementation considers as representative may be returned.
*
* @return A Bitmap containing a representative video frame, which
* can be null, if such a frame cannot be retrieved.
*
* @see #getFrameAtTime(long, int)
*/
public Bitmap getFrameAtTime(long timeUs) {
return getFrameAtTime(timeUs,OPTION_CLOSEST_SYNC);
}
其中函数有选项如下:
/**
* This option is used with {@link #getFrameAtTime(long, int)} to retrieve
* a sync (or key) frame associated with a data source that is located
* closest to (in time) or at the given time.
*
* @see #getFrameAtTime(long, int)
*/
public static final int OPTION_CLOSEST_SYNC =0x02;
获取时间点附近关键帧的截图
/**
* This option is used with {@link #getFrameAtTime(long, int)} to retrieve
* a frame (not necessarily a key frame) associated with a data source that
* is located closest to or at the given time.
*
* @see #getFrameAtTime(long, int)
*/
public static final int OPTION_CLOSEST =0x03;
获取指定时间(非关键帧也行)的截图,但是概率性获取为NULL
获取指定时间的Bitmap
优点:
A.截取关键帧接口耗时500ms
B.接口使用简单
C.直接返回Bitmap,方便使用
缺点:
A. M3U8等部分格式无法获取截图
B. 选项为关键帧时只能获取关键帧的截图,无法准确按照时间获取there is no guarentee that the data source has a frame located at the position.When this happens, a frame nearby will be returned
C.使用非关键帧截图选项时,概率性获取不到截图,截图时间需要1~3s
2.利用ffmpeg实现
直接使用ffmpeg库,利用命令:
ffmpeg -ss 起始时间 -i 源文件 -f image2 -r 频率 -t 总共处理时长 %4d.jpg
获取指定时间的截图并保存为本地文件
优点:
A.可以按照时间准确获取截图
B.功能强大,可以为以后功能打下基础
C.M3U8格式可以获取截图
缺点:
A.功能复杂性提高
B.截图大概需要1s时间(耗时点在ffmpeg中transcode接口大概耗时800ms),主要性能限制实在网络,本地linux截图时间可以达到几十ms级别
C.需要保存文件进行使用,耗费时间
以上是两种实现方式的介绍
linux本地ffmpeg测试本地视频时,大概耗时在100ms以内,因此可以猜测主要耗时点实在网络这边
产品分析:目前可行的快速显示缩略图的作法是:服务端另外放置低帧率的码流,以便于拉取缩略图,由于帧率低,数据量小,处理的视频数据包也少,一般对于ffmpeg一个包大概耗时50ms,一般视频可能有10多个包也就500ms以上,所以只有一个包时可以很快的处理,这样子可以达到快速显示指定时间缩略图的效果。不依赖服务器很难实现快速显示的效果