视频缩略图获取分析

目前来说,存在很多实现方式。

下面描述其中两种实现方式

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以上,所以只有一个包时可以很快的处理,这样子可以达到快速显示指定时间缩略图的效果。不依赖服务器很难实现快速显示的效果

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • rljs by sennchi Timeline of History Part One The Cognitiv...
    sennchi阅读 7,458评论 0 10
  • pyspark.sql模块 模块上下文 Spark SQL和DataFrames的重要类: pyspark.sql...
    mpro阅读 9,516评论 0 13
  • 超高速音视频编码器用法: ffmpeg [options] [[infile options] -i infile...
    吉凶以情迁阅读 4,677评论 0 4
  • 喝自己喜欢的茶,做自己想做的事,爱自己喜爱的人,莫问是对是错,是缘是劫。直到那一天,我们仓促老去,纵使不能...
    小美好986阅读 399评论 0 6
  • 2016年9月1日,推倒了,重新再来过。 反正现在在人生的最低谷了,穷,病, 丑。没什么可惜的。 现在改进的方法越...
    就叫张士超吧阅读 316评论 0 0