简体   繁体   English

如何使用 Android MediaCodec 将视频分成更小的部分?

[英]How to split a video into smaller parts with Android MediaCodec?

In my app I would like to split a selected video from the gallery into smaller pieces of the same length.在我的应用程序中,我想将图库中的选定视频拆分为相同长度的较小片段。 For example, if I want to split a 25 second video into pieces of up to 10 seconds, I would get 3 videos (10 sec, 10 sec, 5 sec).例如,如果我想将一个 25 秒的视频分成最多 10 秒的片段,我会得到 3 个视频(10 秒、10 秒、5 秒)。

So far I found this answer on Stack Overflow that allows me to just choose the beginning and end of a video.到目前为止,我在 Stack Overflow 上找到了这个答案,它允许我只选择视频的开头和结尾。

How can I use MediaCodec to achieve my goal?我如何使用 MediaCodec 来实现我的目标?

To produce segments of exact duration you would need to re-encode the video and possibly the audio track using pairs of decoders and encoders (instances of MediaCodec ) along with a MediaExtractor and a separate MediaMuxer for each output segment.要生成精确持续时间的片段,您需要使用成对的解码器和编码器( MediaCodec的实例)以及一个MediaExtractor和一个单独的MediaMuxer为每个 output 片段重新编码视频和可能的音轨。

Each time you reach the end of a segment (looking at the presentation time of the frame) you would send an end-of-stream signal to the encoder and then restart it.每次到达片段的末尾(查看帧的呈现时间)时,您都会向编码器发送一个流结束信号,然后重新启动它。

For audio you would also need to split some of the decoded buffers at the segment boundaries.对于音频,您还需要在段边界处拆分一些解码缓冲区。

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

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