如何使用 MediaCodec 将位图编码为视频? [英] How to encode Bitmaps into a video using MediaCodec?

查看:52
本文介绍了如何使用 MediaCodec 将位图编码为视频?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将我拥有的一组位图编码为 h264.这可以通过 MediaEncoder 实现吗?为了做到这一点,我编写了一些代码,但无法在我尝试过的任何媒体播放器中播放输出.以下是我主要从 Stackoverflow 上找到的其他来源借用的一些代码.

I would like to encode a set of Bitmaps that I have into an h264. Is this possible via MediaEncoder? I have written some code in order to do it, but the output cannot be played in any media player I have tried. Here's some of the code that I primarily borrowed from other sources that I found on Stackoverflow.

mMediaCodec = MediaCodec.createEncoderByType("video/avc");
mMediaFormat = MediaFormat.createVideoFormat("video/avc", 320, 240);
mMediaFormat.setInteger(MediaFormat.KEY_BIT_RATE, 125000);
mMediaFormat.setInteger(MediaFormat.KEY_FRAME_RATE, 15);
mMediaFormat.setInteger(MediaFormat.KEY_COLOR_FORMAT, MediaCodecInfo.CodecCapabilities.COLOR_FormatYUV420Planar);
mMediaFormat.setInteger(MediaFormat.KEY_I_FRAME_INTERVAL, 5);
mMediaCodec.configure(mMediaFormat, null, null, MediaCodec.CONFIGURE_FLAG_ENCODE);
mMediaCodec.start();
mInputBuffers = mMediaCodec.getInputBuffers();

ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
image.compress(Bitmap.CompressFormat.PNG, 100, byteArrayOutputStream); // image is the bitmap
byte[] input = byteArrayOutputStream.toByteArray();

int inputBufferIndex = mMediaCodec.dequeueInputBuffer(-1);
if (inputBufferIndex >= 0) {
    ByteBuffer inputBuffer = mInputBuffers[inputBufferIndex];
    inputBuffer.clear();
    inputBuffer.put(input);
    mMediaCodec.queueInputBuffer(inputBufferIndex, 0, input.length, 0, 0);
}

我应该调整什么?

推荐答案

MediaCodec 的输出是原始 H.264 基本流.我发现 Linux 的 Totem 媒体播放器可以播放它们.

The output of MediaCodec is a raw H.264 elementary stream. I've found that the Totem media player for Linux is able to play them.

您真正想做的是将输出转换为 .mp4 文件.(更新:)Android 4.3 (API 18) 引入了 MediaMuxer 类,它提供了一种将原始数据(加上可选的音频流)转换为 .mp4 文件的方法.

What you really want to do is convert the output to a .mp4 file. (Update:) Android 4.3 (API 18) introduced the MediaMuxer class, which provides a way to convert the raw data (plus an optional audio stream) to a .mp4 file.

ByteBuffer 中的数据布局从 Android 4.3 开始取决于设备.(实际上,并非所有设备都支持 COLOR_FormatYUV420Planar —— 有些更喜欢半平面变体.)我无法在不了解您的设备的情况下告诉您确切的布局,但我可以告诉您它需要未压缩数据,因此传递压缩的 PNG 数据将不起作用.

The layout of data in the ByteBuffer is, as of Android 4.3, device-dependent. (In fact, not all devices support COLOR_FormatYUV420Planar -- some prefer a semi-planar variant.) I can't tell you the exact layout without knowing your device, but I can tell you that it wants uncompressed data, so passing in compressed PNG data is not going to work.

(更新:)Android 4.3 还允许 Surface 输入到 MediaCodec 编码器,因此可以记录任何可以使用 OpenGL ES 渲染的内容.有关示例,请参阅 EncodeAndMuxTest 示例此处.

(Update:) Android 4.3 also allows Surface input to MediaCodec encoders, so anything you can render with OpenGL ES can be recorded. For an example, see the EncodeAndMuxTest sample here.

这篇关于如何使用 MediaCodec 将位图编码为视频?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆