在Android中使用 bitmap转视频 而且不能使用第三方框架,需要控制每一个bitmap显示的时长
时间: 2024-06-09 21:06:20 浏览: 16
要实现将一组 Bitmap 转换为视频的功能,你可以使用 Android 提供的 MediaCodec 和 MediaMuxer 类来完成。这两个类都是 Android SDK 提供的标准类,不需要引入额外的第三方框架。
具体的实现步骤如下:
1. 创建一个 MediaCodec 编码器和一个 MediaMuxer 复用器,并配置它们的参数。
2. 将每个 Bitmap 转换为 YUV 格式的数据,并编码成 H.264 格式的视频帧。
3. 将编码后的视频帧写入到 MediaMuxer 中。
4. 重复步骤 2 和 3,直到所有的 Bitmap 转换完成。
5. 关闭 MediaMuxer 和 MediaCodec,完成视频的生成。
在这个过程中,你需要控制每个 Bitmap 显示的时长,可以在每个 Bitmap 转换为视频帧的时候,通过设置 PTS(Presentation Time Stamp)来控制。
下面是一个简单的示例代码,可以参考一下:
```java
public void createVideoFromBitmaps(List<Bitmap> bitmaps, String outputPath, int frameRate) throws Exception {
// 创建 MediaCodec 编码器
MediaCodec encoder = MediaCodec.createEncoderByType("video/avc");
MediaFormat format = MediaFormat.createVideoFormat("video/avc", bitmaps.get(0).getWidth(), bitmaps.get(0).getHeight());
format.setInteger(MediaFormat.KEY_FRAME_RATE, frameRate);
format.setInteger(MediaFormat.KEY_BIT_RATE, 2000000);
format.setInteger(MediaFormat.KEY_I_FRAME_INTERVAL, 5);
encoder.configure(format, null, null, MediaCodec.CONFIGURE_FLAG_ENCODE);
encoder.start();
// 创建 MediaMuxer 复用器
MediaMuxer muxer = new MediaMuxer(outputPath, MediaMuxer.OutputFormat.MUXER_OUTPUT_MPEG_4);
// 将每个 Bitmap 转换为视频帧
for (int i = 0; i < bitmaps.size(); i++) {
Bitmap bitmap = bitmaps.get(i);
byte[] yuvData = convertBitmapToYUV(bitmap);
ByteBuffer inputBuffer = getInputBuffer(encoder);
inputBuffer.clear();
inputBuffer.put(yuvData);
long pts = i * 1000000L / frameRate;
encoder.queueInputBuffer(0, 0, yuvData.length, pts, 0);
MediaCodec.BufferInfo bufferInfo = new MediaCodec.BufferInfo();
int outputBufferIndex = encoder.dequeueOutputBuffer(bufferInfo, 0);
while (outputBufferIndex >= 0) {
ByteBuffer outputBuffer = encoder.getOutputBuffer(outputBufferIndex);
muxer.writeSampleData(0, outputBuffer, bufferInfo);
encoder.releaseOutputBuffer(outputBufferIndex, false);
outputBufferIndex = encoder.dequeueOutputBuffer(bufferInfo, 0);
}
}
// 关闭 MediaMuxer 和 MediaCodec
encoder.stop();
encoder.release();
muxer.stop();
muxer.release();
}
private byte[] convertBitmapToYUV(Bitmap bitmap) {
int width = bitmap.getWidth();
int height = bitmap.getHeight();
int[] argb = new int[width * height];
bitmap.getPixels(argb, 0, width, 0, 0, width, height);
byte[] yuv = new byte[width * height * 3 / 2];
encodeYUV420SP(yuv, argb, width, height);
return yuv;
}
private void encodeYUV420SP(byte[] yuv420sp, int[] argb, int width, int height) {
final int frameSize = width * height;
int yIndex = 0;
int uvIndex = frameSize;
int a, R, G, B, Y, U, V;
int index = 0;
for (int j = 0; j < height; j++) {
for (int i = 0; i < width; i++) {
a = (argb[index] & 0xff000000) >> 24; // a is not used obviously
R = (argb[index] & 0xff0000) >> 16;
G = (argb[index] & 0xff00) >> 8;
B = (argb[index] & 0xff);
// well known RGB to YUV algorithm
Y = ((66 * R + 129 * G + 25 * B + 128) >> 8) + 16;
U = ((-38 * R - 74 * G + 112 * B + 128) >> 8) + 128;
V = ((112 * R - 94 * G - 18 * B + 128) >> 8) + 128;
// Y
yuv420sp[yIndex++] = (byte) ((Y < 0) ? 0 : ((Y > 255) ? 255 : Y));
if (j % 2 == 0 && index % 2 == 0) {
// U
yuv420sp[uvIndex++] = (byte) ((U < 0) ? 0 : ((U > 255) ? 255 : U));
// V
yuv420sp[uvIndex++] = (byte) ((V < 0) ? 0 : ((V > 255) ? 255 : V));
}
index++;
}
}
}
private ByteBuffer getInputBuffer(MediaCodec encoder) {
ByteBuffer[] inputBuffers = encoder.getInputBuffers();
for (ByteBuffer buffer : inputBuffers) {
if (buffer != null) {
return buffer;
}
}
return null;
}
```
注意,这只是一个简单的示例代码,实际上在实现中还需要处理一些异常情况和边界条件,以保证代码的健壮性和可靠性。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)