gpt4 book ai didi

android - 使用 MediaCodec 进行转码会产生绿色且困惑的视频

转载 作者:行者123 更新时间:2023-11-30 00:09:24 29 4
gpt4 key购买 nike

我正在尝试通过使用 MediaCodec 对视频进行解码和重新编码来减小视频文件的大小。但是,muxer 生成的新视频文件都是 messed up .你能告诉我我做错了什么吗?

我的代码是从这个 CTS 修改而来的有两个主要区别:

  1. 我在带有回调的异步模式下使用编解码器。
  2. 我没有使用辅助类,而是将 encoder.createInputSurface() 生成的表面直接传递给 decoder.configure()。

我尝试通过将 surfaceView.getHolder().getSurface() 传递给 decoder.configure() 将视频解码为 SurfaceView,并且视频播放正确。我认为这可能是编码器/复用器部分的问题。

这是我的代码:

解码器回调:

private class VideoDecodeCallBack extends MediaCodec.Callback {

@Override
public void onInputBufferAvailable(MediaCodec codec, int index) {
ByteBuffer buffer = codec.getInputBuffer(index);
if (extractorDone) {
return;
}

int size = videoExtractor.readSampleData(buffer, 0);
long pts = videoExtractor.getSampleTime();
if (size >= 0) {
codec.queueInputBuffer(index, 0, size, pts, videoExtractor.getSampleFlags());
}
extractorDone = !videoExtractor.advance();
if (extractorDone) {
codec.queueInputBuffer(index, 0, 0, 0, MediaCodec.BUFFER_FLAG_END_OF_STREAM);
}
}

@Override
public void onOutputBufferAvailable(
MediaCodec codec, int index, MediaCodec.BufferInfo info) {
if ((info.flags & MediaCodec.BUFFER_FLAG_CODEC_CONFIG) != 0) {
codec.releaseOutputBuffer(index, false);
return;
}

boolean render = info.size != 0;
codec.releaseOutputBuffer(index, render);

if ((info.flags & MediaCodec.BUFFER_FLAG_END_OF_STREAM) != 0) {
videoEncoder.signalEndOfInputStream();
}

}

@Override
public void onError(
MediaCodec codec, MediaCodec.CodecException e) {

}

@Override
public void onOutputFormatChanged(
MediaCodec codec, MediaFormat format) {
}

编码器回调:

private class VideoEncodeCallBack extends MediaCodec.Callback {

@Override
public void onInputBufferAvailable(MediaCodec codec, int index) {
}

@Override
public void onOutputBufferAvailable(
MediaCodec codec, int index, MediaCodec.BufferInfo info) {
ByteBuffer buffer = codec.getOutputBuffer(index);

if ((info.flags & MediaCodec.BUFFER_FLAG_CODEC_CONFIG) != 0) {
codec.releaseOutputBuffer(index, false);
return;
}

if (info.size != 0) {
muxer.writeSampleData(outputVideoTrack, buffer, info);
}

if ((info.flags & MediaCodec.BUFFER_FLAG_END_OF_STREAM) != 0) {
muxer.stop();
muxer.release();
}

codec.releaseOutputBuffer(index, false);
}

@Override
public void onError(
MediaCodec codec, MediaCodec.CodecException e) {

}

@Override
public void onOutputFormatChanged(
MediaCodec codec, MediaFormat format) {
outputVideoTrack = muxer.addTrack(format);
muxer.start();
}
}

我如何配置编解码器:

public void configure(String input, String output) throws IOException {
// Extractors
videoExtractor = new MediaExtractor();
videoExtractor.setDataSource(input);

int videoTrack = getAndSelectVideoTrackIndex(videoExtractor);

MediaFormat videoFormat = videoExtractor.getTrackFormat(videoTrack);
String mime = videoFormat.getString(MediaFormat.KEY_MIME);

// Codecs
videoDecoder = MediaCodec.createDecoderByType(mime);
videoEncoder = MediaCodec.createEncoderByType(mime);

videoDecoder.setCallback(new VideoDecodeCallBack(), videoDecodeThreadHandler);
videoEncoder.setCallback(new VideoEncodeCallBack(), videoEncodeThreadHandler);

videoEncoder.configure(createOutputFormat(), null, null, MediaCodec.CONFIGURE_FLAG_ENCODE);
Surface surface = videoEncoder.createInputSurface();
videoDecoder.configure(videoFormat, surface, null, 0);

// Muxer
muxer = new MediaMuxer(output, OutputFormat.MUXER_OUTPUT_MPEG_4);
}

所需的输出格式:

private static MediaFormat createOutputFormat() {
MediaFormat result = MediaFormat.createVideoFormat("video/avc", 540, 960);
result.setInteger(MediaFormat.KEY_COLOR_FORMAT, MediaCodecInfo.CodecCapabilities.COLOR_FormatSurface);
result.setInteger(MediaFormat.KEY_BIT_RATE, 1300000);
result.setInteger(MediaFormat.KEY_FRAME_RATE, 30);
result.setInteger(MediaFormat.KEY_I_FRAME_INTERVAL, 1);
return result;
}

完整代码为here

最佳答案

我也有这个问题。我的猜测是,当解码器直接渲染到编码器的输入表面时,Android 无法重新缩放帧。

解决方案可以是使用 openGL 进行重新缩放。

这个项目就是这样做的。 https://github.com/hoolrory/AndroidVideoSamples/blob/master/CommonVideoLibrary/src/com/roryhool/commonvideolibrary/VideoResampler.java

(我没有运行这个项目,但我已经实现了类似的东西,所以它应该可以工作)

关于android - 使用 MediaCodec 进行转码会产生绿色且困惑的视频,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48400290/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com