简体   繁体   English

Android MediaCodec:减少mp4视频大小

[英]Android MediaCodec: Reduce mp4 video size

I am looking for a way to "compress" an mp4 video. 我正在寻找一种“压缩”mp4视频的方法。 To achieve this, I want to reduce the resolution of the video and / or reduce FPS. 为此,我想降低视频的分辨率和/或降低FPS。

After long research, I think the way to do it is to use MediaCodec and related APIs as follows : 经过长时间的研究,我认为这样做的方法是使用MediaCodec和相关的API,如下所示:

-> MediaExtractor to extract encoded media data from a mp4 file. - > MediaExtractor从mp4文件中提取编码的媒体数据。 -> MediaCodec (Decoder): Decode each frame for later processing. - > MediaCodec(解码器):解码每个帧以供以后处理。 -> 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. - > MediaCodec(编码器):此时,我猜我们应该在哪里建立MediaFormat参数,例如视频分辨率,这应该有助于我们减少mp4文件的最终大小。 -> MediaMuxer for generate mp4 file (MinSDK 18, this might be a problem, but for now... ok) . - > MediaMuxer用于生成mp4文件(MinSDK 18,这可能是一个问题,但现在......好吧)。

I don't want to use a SurfaceView/TextureView. 我不想使用SurfaceView / TextureView。 Regarding the audio, I don't want to process it to reduce quality. 关于音频,我不想处理它以降低质量。

I've been looking at all the examples here: http://bigflake.com/mediacodec/ 我一直在看这里的所有例子: http//bigflake.com/mediacodec/

I've also seen CTS source code modules. 我也看过CTS源代码模块。

And I've been looking at several projects like these: https://github.com/vecio/MediaCodecDemo/blob/master/src/io/vec/demo/mediacodec/DecodeActivity.java 我一直在研究以下几个项目: https//github.com/vecio/MediaCodecDemo/blob/master/src/io/vec/demo/mediacodec/DecodeActivity.java

And of course I have read dozens of posts on stackoverflow. 当然,我已经在stackoverflow上阅读了几十篇帖子。

The problem is that I have not yet found any code example that works well for this porpuse. 问题是我还没有找到任何适合这个porpuse的代码示例。 I tried to take various pieces of code and put them together on a project, but does not really work. 我尝试将各种代码片段放在一起放在一个项目中,但实际上并没有用。

Thanks. 谢谢。

The closest thing to what you want is probably the DecodeEditEncodeTest on bigflake. 最接近你想要的东西可能是bigflake上的DecodeEditEncodeTest。 You don't need SurfaceView or TextureView , since you're not trying to display the video, but you will need to use a SurfaceTexture . 您不需要SurfaceViewTextureView ,因为您不是要显示视频,但是您需要使用SurfaceTexture

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. 缩放由GPU执行,因为它将GL纹理(从SurfaceTexture )渲染到编码器的输入表面上。 You'll have to decide whether GLES linear filtering provides the level of quality you need. 您必须决定GLES线性过滤是否提供您所需的质量水平。

(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.) (您可以在ExtractMpegFramesTest中看到类似的东西,它在进行提取时将所有内容扩展到640x480。在这种情况下,它将帧转换为PNG而不是将它们送入编码器,因此它不适用于您的情况。)

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. 如果输入是“frame1 frame2 ... pause ... frame4 frame5”,并且你丢弃了frame2,你最终会在frame1上暂停,这可能看起来很奇怪。 You can use the presentation time stamps to figure out the timing. 您可以使用演示时间戳来计算时间。

Surface input and MediaMuxer (required for saving H.264 as .mp4) require API 18+. Surface输入和MediaMuxer (将H.264保存为.mp4所需)需要API 18+。 For older versions of Android you'll probably want to use a third-party library like ffmpeg . 对于旧版Android,您可能希望使用第三方库,如ffmpeg

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

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