Android MediaCodec:减小 mp4 视频大小 [英] Android MediaCodec: Reduce mp4 video size

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

问题描述

我正在寻找一种压缩"mp4 视频的方法.为此,我想降低视频的分辨率和/或降低 FPS.

I am looking for a way to "compress" an mp4 video. To achieve this, I want to reduce the resolution of the video and / or reduce FPS.

经过长时间的研究,我认为这样做的方法是使用MediaCodec和相关API如下:

After long research, I think the way to do it is to use MediaCodec and related APIs as follows :

-> MediaExtractor 从 mp4 文件中提取编码的媒体数据.-> MediaCodec (Decoder):解码每一帧以备后用.-> MediaCodec (Encoder):此时,我想这就是我们应该建立视频分辨率等MediaFormat参数的地方,这应该有助于我们减小mp4文件的最终大小.-> MediaMuxer 用于生成 mp4 文件(MinSDK 18,这可能是一个问题,但现在...好的).

-> MediaExtractor to extract encoded media data from a mp4 file. -> MediaCodec (Decoder): Decode each frame for later processing. -> MediaCodec (Encoder): At this point, I guess that's where we should establish MediaFormat parameters such as video resolution, which should help us to reduce the final size of the mp4 file. -> MediaMuxer for generate mp4 file (MinSDK 18, this might be a problem, but for now... ok) .

我不想使用 SurfaceView/TextureView.关于音频,我不想对其进行处理以降低质量.

I don't want to use a SurfaceView/TextureView. Regarding the audio, I don't want to process it to reduce quality.

我一直在查看这里的所有示例:http://bigflake.com/mediacodec/

I've been looking at all the examples here: http://bigflake.com/mediacodec/

我也看过 CTS 源代码模块.

I've also seen CTS source code modules.

我一直在研究几个这样的项目:https://github.com/vecio/MediaCodecDemo/blob/master/src/io/vec/demo/mediacodec/DecodeActivity.java

And I've been looking at several projects like these: https://github.com/vecio/MediaCodecDemo/blob/master/src/io/vec/demo/mediacodec/DecodeActivity.java

当然,我已经阅读了许多关于 stackoverflow 的帖子.

And of course I have read dozens of posts on stackoverflow.

问题是我还没有找到任何适合这个海豚的代码示例.我试图将不同的代码片段放在一个项目中,但并没有真正奏效.

The problem is that I have not yet found any code example that works well for this porpuse. I tried to take various pieces of code and put them together on a project, but does not really work.

谢谢.

推荐答案

最接近你想要的可能是 DecodeEditEncodeTest 在 bigflake 上.您不需要 SurfaceViewTextureView,因为您不想显示视频,但您需要使用 SurfaceTexture.

The closest thing to what you want is probably the DecodeEditEncodeTest on bigflake. You don't need SurfaceView or TextureView, since you're not trying to display the video, but you will need to use a SurfaceTexture.

测试代码生成视频,然后解码/编辑/编码,然后测试编码输出.您需要解码-编辑-编码部分.创建编码器时,给它任何你想要的大小.它创建的输入表面将调整大小以匹配.缩放由 GPU 执行,因为它将 GL 纹理(来自 SurfaceTexture)渲染到编码器的输入表面上.您必须决定 GLES 线性过滤是否提供您需要的质量水平.

The test code generates a video, then decodes / edits / encodes, then tests the encoded output. You want the decode-edit-encode part. When creating the encoder, give it whatever size you want. The input surface it creates will be sized to match. The scaling is performed by the GPU as it renders the GL texture (from the SurfaceTexture) onto the encoder's input surface. You'll have to decide whether GLES linear filtering provides the level of quality you need.

(您可以在 ExtractMpegFramesTest 中看到类似的内容,它在进行提取时将所有内容缩放到 640x480.在这种情况下,它将帧转换为 PNG,而不是将它们送入编码器,因此它不适用于您的情况.)

(You can see something similar in ExtractMpegFramesTest, which scales everything to 640x480 as it's doing the extraction. In this case it's converting the frames to PNG rather than feeding them into an encoder, so it's not as applicable to your situation.)

丢帧在理论上很容易:只是不要将其提交给编码器.实际上,如果输入具有不规则间隔的帧,则会有点困难.如果输入是frame1 frame2 ... pause ... frame4 frame5",而你放下 frame2,你最终会在 frame1 上暂停,这可能看起来很奇怪.您可以使用演示时间戳来确定时间.

Dropping frames is theoretically easy: just don't submit it to the encoder. In practice it's a bit harder if the input has irregularly-spaced frames. If the input is "frame1 frame2 ... pause ... frame4 frame5", and you drop frame2, you'll end up paused on frame1, which might look weird. You can use the presentation time stamps to figure out the timing.

Surface 输入和 MediaMuxer(需要将 H.264 保存为 .mp4)需要 API 18+.对于旧版本的 Android,您可能需要使用第三方库,例如 ffmpeg.

Surface input and MediaMuxer (required for saving H.264 as .mp4) require API 18+. For older versions of Android you'll probably want to use a third-party library like ffmpeg.

这篇关于Android MediaCodec:减小 mp4 视频大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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