Kir*_*l K 0 video android video-processing grafika android-mediacodec
我的目标是拼接多个视频文件中的视频片段。片段由任意开始时间和结束时间定义。最初我想使用像 mp4parser 这样的库来做到这一点,但它只能在同步(IFRAME)点切割流,而我需要更高的精度。
我的场景是从文件中提取编码流 -> 解码 -> 编码 -> 将结果混合到 mp4 文件中。目前,代码通常可以工作,但生成的视频是白噪声。在 Nexus-S 和 Galaxy-S3 上进行了测试。我的代码是几个示例的组合:
我想简化示例,因为我不需要在中间处理帧。我尝试将缓冲区从解码器输出馈送到编码器输入,而中间没有 Surface。整个过程的工作原理是代码运行完成并生成可播放的视频文件。然而文件的内容是白噪声。
这是将帧从解码器提供给编码器的代码片段。有什么问题以及如何使其发挥作用?
...
} else { // decoderStatus >= 0
if (VERBOSE) Log.d(TAG, "surface decoder given buffer "
+ decoderStatus + " (size=" + info.size + ")");
// The ByteBuffers are null references, but we still get a nonzero
// size for the decoded data.
boolean doRender = (info.size != 0);
// As soon as we call releaseOutputBuffer, the buffer will be forwarded
// to SurfaceTexture to convert to a texture. The API doesn't
// guarantee that the texture will be available before the call
// returns, so we need to wait for the onFrameAvailable callback to
// fire. If we don't wait, we risk rendering from the previous frame.
// decoder.releaseOutputBuffer(decoderStatus, doRender);
if (doRender) {
// This waits for the image and renders it after it arrives.
// if (VERBOSE) Log.d(TAG, "awaiting frame");
// outputSurface.awaitNewImage();
// outputSurface.drawImage();
// // Send it to the encoder.
// inputSurface.setPresentationTime(info.presentationTimeUs * 1000);
// if (VERBOSE) Log.d(TAG, "swapBuffers");
// inputSurface.swapBuffers();
encoderStatus = encoder.dequeueInputBuffer(-1);
if (encoderStatus >= 0) {
encoderInputBuffers[encoderStatus].clear();
decoderOutputBuffers[decoderStatus].position(info.offset);
decoderOutputBuffers[decoderStatus].limit(info.offset + info.size);
encoderInputBuffers[encoderStatus].put(decoderOutputBuffers[decoderStatus]);
encoder.queueInputBuffer(encoderStatus, 0, info.size, info.presentationTimeUs*1000, 0);
}
}
decoder.releaseOutputBuffer(decoderStatus, false);
...
Run Code Online (Sandbox Code Playgroud)
使用 Surface 比 ByteBuffer 好得多。它更快也更便携。表面是缓冲区队列,而不仅仅是像素数据的帧缓冲区;解码后的视频帧通过句柄传递。如果您使用 ByteBuffers,则必须复制视频数据几次,这会减慢您的速度。
创建 MediaCodec 编码器,获取输入表面,并将其传递给解码器作为其输出表面。
如果您需要使用 API 16/17,那么您就只能使用 ByteBuffers。如果您四处搜索,您可以找到针对古怪的高通格式的逆向工程转换器,但请记住,直到 API 18 才进行 CTS 测试,因此无法保证。
| 归档时间: |
|
| 查看次数: |
2120 次 |
| 最近记录: |