使用MediaCodec对本地mp4视频进行帧预览, 区间选择,
2018-10-10 本文已影响174人
vb12
image.png
实现说明
- 使用的播放器为原生MediaPlayer, 所以在帧选择时可能不太流畅, 在高于O版本的手机上会好一点, 这是因为如下的一个新接口:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
player.seekTo(startMillSec, SEEK_CLOSEST)
} else {
player.seekTo(startMillSec.toInt())
}
最开始的时候用的是Exoplayer2, 效果竟然还没有MediaPlayer好, 有点不可思议.
公司项目用的是腾讯视频sdk, 效果好很多, 但是需要一大堆依赖. 猜测它应该是使用的ffmpeg实现的.
如果要实现最流畅的预览效果, 需要重新生成一个关键帧丰富的视频文件. 这是一个可以折中放弃的选项.
-
慢速, 快速播放. 目前MediaPlayer, exoPlayer2已经支持.
-
缩略图使用的TextureView截图实现, 虽然笨拙且慢, 但是目前在不依赖ffmpeg情况下, 这是最保险的. 因为对于关键帧确实的视频extractor也无法抓取到正确的图像.
具体可以参看: https://www.jianshu.com/p/0f6578362e58 -
其实最费劲的是怎么计算帧选择时, 以及裁剪区间时的时间点. 可费劲了. 这也是我决定放到github上的原因, 万一以后还会用到呢. 不想再折腾一次了.
-
支持裁剪. 具体代码:
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
public static boolean genVideoUsingMuxer(Context context, String srcPath, String dstPath,
int startMs, int endMs, boolean useAudio, boolean
useVideo)
throws IOException {
boolean success = true;
// Set up MediaExtractor to read from the source.
MediaExtractor extractor = new MediaExtractor();
extractor.setDataSource(srcPath);
int trackCount = extractor.getTrackCount();
// Set up MediaMuxer for the destination.
MediaMuxer muxer;
muxer = new MediaMuxer(dstPath, MediaMuxer.OutputFormat.MUXER_OUTPUT_MPEG_4);
// Set up the tracks and retrieve the max buffer size for selected
// tracks.
HashMap<Integer, Integer> indexMap = new HashMap<>(trackCount);
int bufferSize = -1;
for (int i = 0; i < trackCount; i++) {
MediaFormat format = extractor.getTrackFormat(i);
String mime = format.getString(MediaFormat.KEY_MIME);
boolean selectCurrentTrack = false;
if (mime.startsWith("audio/") && useAudio) {
selectCurrentTrack = true;
} else if (mime.startsWith("video/") && useVideo) {
selectCurrentTrack = true;
}
if (selectCurrentTrack) {
extractor.selectTrack(i);
int dstIndex = muxer.addTrack(format);
indexMap.put(i, dstIndex);
if (format.containsKey(MediaFormat.KEY_MAX_INPUT_SIZE)) {
int newSize = format.getInteger(MediaFormat.KEY_MAX_INPUT_SIZE);
bufferSize = newSize > bufferSize ? newSize : bufferSize;
}
}
}
if (bufferSize < 0) {
bufferSize = 1080*1920*30; // todo
}
// Set up the orientation and starting time for extractor.
MediaMetadataRetriever retrieverSrc = new MediaMetadataRetriever();
retrieverSrc.setDataSource(srcPath);
String degreesString = retrieverSrc.extractMetadata(
MediaMetadataRetriever.METADATA_KEY_VIDEO_ROTATION);
if (degreesString != null) {
int degrees = Integer.parseInt(degreesString);
if (degrees >= 0) {
muxer.setOrientationHint(degrees);
}
}
if (startMs > 0) {
extractor.seekTo(startMs * 1000, MediaExtractor.SEEK_TO_PREVIOUS_SYNC);
}
// Copy the samples from MediaExtractor to MediaMuxer. We will loop
// for copying each sample and stop when we get to the end of the source
// file or exceed the end time of the trimming.
int offset = 0;
int trackIndex = -1;
ByteBuffer dstBuf = ByteBuffer.allocate(bufferSize);
MediaCodec.BufferInfo bufferInfo = new MediaCodec.BufferInfo();
try {
muxer.start();
while (true) {
bufferInfo.offset = offset;
bufferInfo.size = extractor.readSampleData(dstBuf, offset);
if (bufferInfo.size < 0) {
Log.d(TAG, "Saw input EOS.");
bufferInfo.size = 0;
break;
} else {
bufferInfo.presentationTimeUs = extractor.getSampleTime();
if (endMs > 0 && bufferInfo.presentationTimeUs > (endMs * 1000)) {
Log.d(TAG, "The current sample is over the trim end time.");
break;
} else {
bufferInfo.flags = extractor.getSampleFlags();
trackIndex = extractor.getSampleTrackIndex();
muxer.writeSampleData(indexMap.get(trackIndex), dstBuf,
bufferInfo);
extractor.advance();
}
}
}
muxer.stop();
//deleting the old file
// File file = new File(srcPath);
// file.delete();
} catch (Exception e) {
// Swallow the exception due to malformed source.
Log.w(TAG, "The source video file is malformed");
success = false;
} finally {
muxer.release();
}
return success;
}