目前來說末购,存在很多實(shí)現(xiàn)方式。
下面描述其中兩種實(shí)現(xiàn)方式
1.利用MediaMetadataRetriever實(shí)現(xiàn)
利用接口:
/**
* 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);
}
其中函數(shù)有選項(xiàng)如下:
/**
* 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;
獲取時(shí)間點(diǎn)附近關(guān)鍵幀的截圖
/**
* 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;
獲取指定時(shí)間(非關(guān)鍵幀也行)的截圖笋庄,但是概率性獲取為NULL
獲取指定時(shí)間的Bitmap
優(yōu)點(diǎn):
≌嚼ぁA.截取關(guān)鍵幀接口耗時(shí)500ms
B.接口使用簡單
C.直接返回Bitmap参淫,方便使用
缺點(diǎn):
A. M3U8等部分格式無法獲取截圖
B. 選項(xiàng)為關(guān)鍵幀時(shí)只能獲取關(guān)鍵幀的截圖翁授,無法準(zhǔn)確按照時(shí)間獲取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.使用非關(guān)鍵幀截圖選項(xiàng)時(shí)拣播,概率性獲取不到截圖晾咪,截圖時(shí)間需要1~3s
2.利用ffmpeg實(shí)現(xiàn)
直接使用ffmpeg庫收擦,利用命令:
ffmpeg -ss 起始時(shí)間 -i 源文件?-f image2 -r? 頻率 -t 總共處理時(shí)長 %4d.jpg
獲取指定時(shí)間的截圖并保存為本地文件
優(yōu)點(diǎn):
A.可以按照時(shí)間準(zhǔn)確獲取截圖
B.功能強(qiáng)大,可以為以后功能打下基礎(chǔ)
? ? C.M3U8格式可以獲取截圖
缺點(diǎn):
? ? A.功能復(fù)雜性提高
? ? B.截圖大概需要1s時(shí)間(耗時(shí)點(diǎn)在ffmpeg中transcode接口大概耗時(shí)800ms)谍倦,主要性能限制實(shí)在網(wǎng)絡(luò)塞赂,本地linux截圖時(shí)間可以達(dá)到幾十ms級(jí)別
C.需要保存文件進(jìn)行使用,耗費(fèi)時(shí)間
以上是兩種實(shí)現(xiàn)方式的介紹
linux本地ffmpeg測試本地視頻時(shí)昼蛀,大概耗時(shí)在100ms以內(nèi)宴猾,因此可以猜測主要耗時(shí)點(diǎn)實(shí)在網(wǎng)絡(luò)這邊
產(chǎn)品分析:目前可行的快速顯示縮略圖的作法是:服務(wù)端另外放置低幀率的碼流,以便于拉取縮略圖叼旋,由于幀率低仇哆,數(shù)據(jù)量小,處理的視頻數(shù)據(jù)包也少夫植,一般對(duì)于ffmpeg一個(gè)包大概耗時(shí)50ms,一般視頻可能有10多個(gè)包也就500ms以上,所以只有一個(gè)包時(shí)可以很快的處理,這樣子可以達(dá)到快速顯示指定時(shí)間縮略圖的效果讹剔。不依賴服務(wù)器很難實(shí)現(xiàn)快速顯示的效果