如何连接code位图到使用媒体codeC视频? [英] How to encode Bitmaps into a video using MediaCodec?
问题描述
我想EN codeA集位图,我已经成为一个H264的。通过MediaEn codeR这可能吗?我已经写了一些code,为了做到这一点,但输出不能在任何媒体播放器我已经试过播放。下面是一些code,我从我发现#1其他来源主要是借来的。
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);
}
我应该怎么调整?
What should I adjust?
推荐答案
媒体codeC
是一个原始的H.264基本流的输出。我发现,图腾媒体播放器的Linux是能够发挥他们。
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
- 一些preFER半平面变种)我不能告诉你,不知道你的设备的精确布局,但我可以告诉你,它想uncom pressed数据,所以传递COM pressed 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还允许表面输入媒体codeC
连接codeRS,所以任何可以渲染与OpenGL ES的可记录。举一个例子,看到恩codeAndMuxTest样品这里。
(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.
这篇关于如何连接code位图到使用媒体codeC视频?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!