[英]Use MediaCodec and MediaExtractor to decode and code video
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. 我需要将视频解码为一系列位图,以便我能够修改它们,然后将它们压缩回android中的视频文件。
I plan to manage this by using getFrameAtTime
and saving it to an image sequence. 我计划通过使用
getFrameAtTime
并将其保存到图像序列来管理它。 Then I can modify images in the sequence and code it back to a movie. 然后我可以修改序列中的图像并将其编码回电影。 But I have two problem with this:
但我有两个问题:
getFrameAtTime
is for creating thumbnails and will not guarantee returning the correct frame. getFrameAtTime
用于创建缩略图,并且不保证返回正确的帧。 This makes the video laggy. 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
. 我读到使用MediaExtractor进行解码的正确方法,这很好,但我只有示例将它直接渲染到
surfaceView
。 Is there any way for me to convert the outputBuffer
to a bitmap? 有什么方法可以将
outputBuffer
转换为位图吗?
I would need it to get it working with an api level of 16 and above. 我需要它来使用16级及以上的api级别。
You can find a collection of useful examples on the bigflake site. 您可以在bigflake网站上找到一些有用的示例。
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. 特别是,
ExtractMpegFramesTest
演示了如何将.mp4文件解码为Bitmap
, DecodeEditEncodeTest
对H.264流进行解码和重新编码,使用GLES着色器修改帧。
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). 许多示例使用API 18中引入的功能,例如
MediaCodec
Surface
输入(避免了许多颜色格式问题)和MediaMuxer
(允许您将来自MediaCodec
的原始H.264基本流转换为.mp4文件)。 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
. 某些设备允许您将视频提取到
ByteBuffer
YUV数据,对其进行修改并对其进行重新编码,但其他设备会提取为专有的YUV颜色格式,这些格式可能会被API 16版本的MediaCodec
拒绝。
I'd recommend coding for API 18 (Android 4.3 "Jellybean" MR2) or later. 我建议编写API 18(Android 4.3“Jellybean”MR2)或更高版本。
There are many people saying that the method onFrameAvailable()
is never called. 有很多人说从不调用
onFrameAvailable()
方法。 Well, the listener should be in a different thread than the main thread. 好吧, 监听器应该在与主线程不同的线程中。 To set the listener do this: (where this is the class listener that implements
SurfaceTexture.IOnFrameAvailableListener
): 要设置监听器,请执行以下操作:(这是实现
SurfaceTexture.IOnFrameAvailableListener
的类监听SurfaceTexture.IOnFrameAvailableListener
):
mSurfaceTexture.SetOnFrameAvailableListener(this);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.