简体   繁体   English

如何使用适用于Android 4.3+的MediaCodec API对视频进行转码

[英]How to transcode a video using the MediaCodec api for Android 4.3+

I have been using FFmpeg to do video transcode, it works but its very slow. 我一直在使用FFmpeg进行视频转码,它可以工作,但是速度很慢。

I have also tried the Intel Inde library but does not work on some Android phones I have tested. 我也尝试了Intel Inde库,但在我测试过的某些Android手机上无法使用。

Therefore I want to make use of the MediaCodec Api to transcode video. 因此,我想利用MediaCodec Api对视频进行转码。

I have read some sample codes from BigFlake , which helps me to gain some concept for the Api. 我已经阅读了BigFlake的一些示例代码,这有助于我了解Api的概念。 However I still do not know how I can transcode a video file from one format to another. 但是,我仍然不知道如何将视频文件从一种格式转码为另一种格式。

I do not have experience working with codec, and the flow I think is: 我没有使用编解码器的经验,我认为的流程是:

Decode input video file(frame?) -> feed into encoder -> convert the stream to a .MP4 file. Decode input video file(frame?) -> feed into encoder -> convert the stream to a .MP4 file. Decode input video file(frame?) -> feed into encoder -> convert the stream to a .MP4 file.

Appreciate if anyone can point me to the right direction. 如果有人能指出我正确的方向,我将不胜感激。

You seem to understand the fundamentals already. 您似乎已经了解了基本原理。 Your work flow is correct -- feed the output of the decoder to the input of the encoder. 您的工作流程是正确的-将解码器的输出馈送到编码器的输入。 If you want the result to be a .mp4 file, send AVC output through MediaMuxer . 如果您希望结果为.mp4文件,请通过MediaMuxer发送AVC输出。

The tricky parts have to do with what you're trying to accomplish by transcoding. 棘手的部分与您要通过转码完成的工作有关。 Changing the size or bit rate of the video is straightforward, as you just need to configure the encoder appropriately. 更改视频的大小或比特率很简单,因为您只需要适当地配置编码器即可。 Filtering the video is a bit more complex, as you'll probably want to use OpenGL ES for performance reasons ( DecodeEditEncode test on bigflake demonstrates this). 过滤视频会更加复杂,因为出于性能方面的考虑,您可能希望使用OpenGL ES( bigflake上的DecodeEditEncode 测试演示了这一点)。 Altering the frame rate can get complicated. 更改帧频会变得很复杂。

For best results do everything through Surface (requires API 18+). 为了获得最佳结果,请通过Surface执行所有操作(需要API 18+)。 This assumes that your video decoder supports output to a Surface; 假设您的视频解码器支持输出到Surface; if not, you will likely need to render it yourself. 如果没有,您可能需要自己渲染。

Some additional MediaCodec examples can be found in Grafika . Grafika中可以找到一些其他MediaCodec示例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM