获取Android中视频文件的关键帧(同步帧)时间戳列表,可以通过以下步骤实现:
import android.media.MediaExtractor;
import android.media.MediaFormat;
public List<Long> getKeyFrameTimeStamps(String videoPath) {
List<Long> keyFrameTimeStamps = new ArrayList<>();
MediaExtractor mediaExtractor = new MediaExtractor();
try {
mediaExtractor.setDataSource(videoPath);
int numTracks = mediaExtractor.getTrackCount();
for (int i = 0; i < numTracks; i++) {
MediaFormat format = mediaExtractor.getTrackFormat(i);
String mime = format.getString(MediaFormat.KEY_MIME);
if (mime.startsWith("video/")) {
mediaExtractor.selectTrack(i);
int frameIndex = 0;
while (true) {
MediaCodec.BufferInfo info = new MediaCodec.BufferInfo();
ByteBuffer buffer = mediaExtractor.getSampleBuffer(frameIndex);
if (buffer == null) {
break;
}
mediaExtractor.getSampleTrackIndex();
mediaExtractor.getSampleTime();
mediaExtractor.getSampleFlags();
if ((info.flags & MediaCodec.BUFFER_FLAG_KEY_FRAME) != 0) {
keyFrameTimeStamps.add(info.presentationTimeUs);
}
frameIndex++;
}
break;
}
}
} catch (IOException e) {
e.printStackTrace();
} finally {
mediaExtractor.release();
}
return keyFrameTimeStamps;
}
String videoPath = "/path/to/video/file.mp4";
List<Long> keyFrameTimeStamps = getKeyFrameTimeStamps(videoPath);
for (Long timeStamp : keyFrameTimeStamps) {
// 处理每个关键帧时间戳
}
通过以上步骤,您可以获取Android中视频文件的关键帧(同步帧)时间戳列表。
领取专属 10元无门槛券
手把手带您无忧上云