简体   繁体   English

使用MediaCodec和MediaExtractor解码和编码视频

[英]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: 但我有两个问题:

  • First, as I read it, the getFrameAtTime is for creating thumbnails and will not guarantee returning the correct frame. 首先,在我阅读它时, getFrameAtTime用于创建缩略图,并且不保证返回正确的帧。 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 . 我读到使用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文件解码为BitmapDecodeEditEncodeTest对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.

相关问题 Android MediaCodec和MediaExtractor用于播放视频和音频 - Android MediaCodec & MediaExtractor For video and audio playing 使用MediaExtractor和MediaCodec在视频播放中跳过帧 - Skip frames in video playback with MediaExtractor and MediaCodec 使用 MediaCodec 和 MediaExtractor 播放视频和音频 - Play Video and audio using MediaCodec and MediaExtractor 如何使用MediaCodec,MediaExtractor或MediaMetadataRetriever确定视频文件的帧率? - How to determine a video file's framerate with MediaCodec, MediaExtractor or MediaMetadataRetriever? Android中的视频清理是否需要MediaCodec-MediaExtractor组合? - Is the MediaCodec-MediaExtractor combo necessary for video scrubbing in Android? 如何在没有MediaExtractor的情况下为H264使用MediaCodec - How to use MediaCodec without MediaExtractor for H264 了解MediaCodec和MediaExtractor - Understanding of MediaCodec and MediaExtractor 使用没有MediaExtractor的MediaCodec解码mp4 / h.264,预期的访问单元格式 - Decode mp4/h.264 using MediaCodec without MediaExtractor, expected access unit format android 中的 MediaCodec、MediaExtractor 和 MediaMuxer 是什么? - What is MediaCodec, MediaExtractor and MediaMuxer in android? MediaCodec.Callback句柄来自MediaExtractor的IllegalStateException - MediaCodec.Callback handle IllegalStateException from MediaExtractor
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM