使用MediaCodec和MediaExtractor解码和编码视频 [英] Use MediaCodec and MediaExtractor to decode and code video

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

问题描述

我需要将视频解码为位图序列,以便能够对其进行修改,然后将其压缩回android中的视频文件.

我计划通过使用getFrameAtTime并将其保存到图像序列中来对其进行管理.然后,我可以按顺序修改图像并将其编码回电影中.但是我有两个问题:

  • 首先,在我阅读本文时,getFrameAtTime用于创建缩略图,并且不能保证返回正确的帧.这使视频变得迟钝.
  • 第二,保存图像并读回它需要很长时间.

我读到正确的解码方法是使用MediaExtractor,这很好,但是我只有示例将其直接呈现为surfaceView.有什么办法可以将outputBuffer转换为位图?

我需要它才能使其在api级别为16及更高的级别上工作.

解决方案

您可以在 bigflake 网站上找到有用的示例的集合.

特别是ExtractMpegFramesTest演示了如何将.mp4文件解码为Bitmap,而DecodeEditEncodeTest解码并重新编码H.264流,并使用GLES着色器修改了帧.

许多示例使用API​​ 18中引入的功能,例如Surface输入MediaCodec(避免了许多颜色格式问题)和MediaMuxer(允许您转换原始H).从MediaCodec出来的264个基本流变成一个.mp4文件).某些设备允许您将视频提取为ByteBuffer中的YUV数据,对其进行修改并重新编码,而其他设备则允许提取为专有的YUV颜色格式,而API 16版本的MediaCodec可能会拒绝这种颜色. >

我建议使用API​​ 18(Android 4.3"Jellybean" MR2)或更高版本进行编码.

I need to decode a video into a sequence of bitmaps, such that I am able to modify them, and then compress them back to a video file in android.

I plan to manage this by using getFrameAtTime and saving it to an image sequence. Then I can modify images in the sequence and code it back to a movie. But I have two problem with this:

  • First, as I read it, the getFrameAtTime is for creating thumbnails and will not guarantee returning the correct frame. This makes the video laggy.
  • Secondly, saving the images and reading it back takes a long time.

I read that the proper way of doing the decode is with MediaExtractor, this is fine, but I only have examples to render it directly to a surfaceView. Is there any way for me to convert the outputBuffer to a bitmap?

I would need it to get it working with an api level of 16 and above.

解决方案

You can find a collection of useful examples on the bigflake site.

In particular, the ExtractMpegFramesTest demonstrates how to decode a .mp4 file to Bitmap, and the DecodeEditEncodeTest decodes and re-encodes an H.264 stream, modifying the frames with a GLES shader.

Many of the examples use features introduced in API 18, such as Surface input to MediaCodec (which avoids a number of color-format issues), and MediaMuxer (which allows you to convert the raw H.264 elementary stream coming out of MediaCodec into a .mp4 file). Some devices will allow you to extract video to YUV data in ByteBuffer, modify it, and re-encode it, but other devices extract to proprietary YUV color formats that may be rejected by the API 16 version of MediaCodec.

I'd recommend coding for API 18 (Android 4.3 "Jellybean" MR2) or later.

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

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