·您现在的位置: 云翼网络 >> 文章中心 >> 网站建设 >> app软件开发 >> Android开发 >> camera录制视频的缩略图获取原理心得分享

camera录制视频的缩略图获取原理心得分享

作者:佚名      Android开发编辑:admin      更新时间:2022-07-23
1、在thumbnail.java文件里通过调用bitmap = retriever.getFrameAtTime(-1);
这句代码得到bitmap,

2、那么这句代码在MediaMetadataRetriever.java 中调用
getFrameAtTime(timeUs, OPTION_CLOSEST_SYNC)这句代码:
解释一下timeUs,和OPTION_CLOSEST_SYNC这2个变量的含义
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

3.由于timeUs等于-1,那么在stagefrightMetadataRetriver.cpp中通过
extractVideoFrameWithCodecFlags()函数
复制代码 代码如下:
if (frameTimeUs < 0) {
if (!trackMeta->findInt64(kKeyThumbnailTime, &thumbNailTime)
|| thumbNailTime < 0) {
thumbNailTime = 0;
}
options.setSeekTo(thumbNailTime, mode);
}else{
...................
}

取得thumbnailTime,
thumbnailTime是取同步帧中最大一帧数据,即有可能不是视频文件的第一个I帧。

4.extractVideoFrameWithCodecFlags()函数中,接着第3条,然后调用err = decoder->read(&buffer, &options);这句代码,其options->seekMode为SEEK_CLOSEST_SYNC这个值

5.如果video codec是mpeg4,则调用MPEG4Extractor.cpp中的read()的函数,
根据前面thumnailtime,找到此时间点的vidoe frame index,然后通过 video frame index,再找临近的同步帧(即I帧)

6.SampleTable.cpp中findSyncSampleNear()函数中,找临近同步帧,
视频文件中会存有所有的同步帧,这个同步帧也有可能是这个同步帧数组中第一个值,也有可能在第5步中得到的video frame index,也有可能位于2个同步帧之间,那么我们通过计算找到这2个同步帧最靠近video frame index的一个同步帧

7.通过上述步骤,找到同步帧,那么根据这个同步帧生成thumbnail的bitmap。