android - Nv21字节数据在android中保存为彩色H264格式

标签 android encoding h.264 android-mediacodec

我正在从 NV21 字节数据获取视频以使用下面的代码,我将其保存为 .H264 格式扩展名,但我的视频背景没有颜色。 a frame of video ,我想做成彩色的,我保存的是H264格式的NV21字节数据,请问这个问题怎么解决?

    private void initCodec() {
        try {
            mMediaCodec = MediaCodec.createEncoderByType("video/avc");
        } catch (IOException e) {
            e.printStackTrace();
        }
        MediaFormat mediaFormat = MediaFormat.createVideoFormat("video/avc",
                1280,
                720);
        mediaFormat.setInteger(MediaFormat.KEY_BIT_RATE, 125000);
        mediaFormat.setInteger(MediaFormat.KEY_FRAME_RATE, 360); //video second
        mediaFormat.setInteger(MediaFormat.KEY_COLOR_FORMAT,
                MediaCodecInfo.CodecCapabilities.COLOR_FormatYUV420Planar);
/*        mDestData = new byte[1280 * 720
                * ImageFormat.getBitsPerPixel(ImageFormat.YV12) / 8];*/

        mediaFormat.setInteger(MediaFormat.KEY_I_FRAME_INTERVAL, 5);
        mMediaCodec.configure(mediaFormat,
                null,
                null,
                MediaCodec.CONFIGURE_FLAG_ENCODE);
        mMediaCodec.start();
}

编码NV21字节数据

    //Video format H264
private synchronized void encode(byte[] data) {

    ByteBuffer[]  inputBuffers = mMediaCodec.getInputBuffers();
    ByteBuffer[]  outputBuffers = mMediaCodec.getOutputBuffers();

    int inputBufferIndex = mMediaCodec.dequeueInputBuffer(-1);
    if (inputBufferIndex >= 0) {
        ByteBuffer inputBuffer = inputBuffers[inputBufferIndex];
        inputBuffer.capacity();
        inputBuffer.clear();
        inputBuffer.put(data);
        mMediaCodec.queueInputBuffer(inputBufferIndex, 0, data.length, 0, 0);
    } else {
        return;
    }

    MediaCodec.BufferInfo bufferInfo = new MediaCodec.BufferInfo();
    int outputBufferIndex = mMediaCodec.dequeueOutputBuffer(bufferInfo, 0);
    Log.i(TAG, "outputBufferIndex-->" + outputBufferIndex);
    do {
        if (outputBufferIndex >= 0) {
            ByteBuffer outBuffer = outputBuffers[outputBufferIndex];
            System.out.println("buffer info-->" + bufferInfo.offset + "--"
                    + bufferInfo.size + "--" + bufferInfo.flags + "--"
                    + bufferInfo.presentationTimeUs);
            byte[] outData = new byte[bufferInfo.size];
            outBuffer.get(outData);
            try {
                if (bufferInfo.offset != 0) {
                    fos.write(outData, bufferInfo.offset, outData.length
                            - bufferInfo.offset);
                } else {
                    fos.write(outData, 0, outData.length);
                }
                fos.flush();
                Log.i(TAG, "out data -- > " + outData.length);
                mMediaCodec.releaseOutputBuffer(outputBufferIndex, false);
                outputBufferIndex = mMediaCodec.dequeueOutputBuffer(bufferInfo,
                        0);
            } catch (IOException e) {
                e.printStackTrace();
            }
        } else if (outputBufferIndex == MediaCodec.INFO_OUTPUT_BUFFERS_CHANGED) {
            outputBuffers = mMediaCodec.getOutputBuffers();
        } else if (outputBufferIndex == MediaCodec.INFO_OUTPUT_FORMAT_CHANGED) {
            MediaFormat format = mMediaCodec.getOutputFormat();
        }
    } while (outputBufferIndex >= 0);
}

我正在使用这种转换格式,它比旧版本更好,但它不提供真实的相机 View

public static byte[] YV12toYUV420Planar(byte[] input, byte[] output, int width, int height) {

    final int frameSize = (width * height);
    final int qFrameSize = frameSize/4;

    System.arraycopy(input, 0, output, 0, frameSize); // Y
    System.arraycopy(input, frameSize, output, frameSize + qFrameSize, qFrameSize); // Cr (V)
    System.arraycopy(input, frameSize + qFrameSize, output, frameSize, qFrameSize); // Cb (U)

    return output;
}

终于打电话了

   private CameraProxy.CameraDataCallBack callBack = new CameraProxy.CameraDataCallBack() {

        @Override
        public void onDataBack(byte[] data, long length) {
            // TODO Auto-generated method stub
            Log.i(TAG, "length . " + length);

            photo = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/", "photo"+i+".jpg");
            byte[]outpt = new byte[data.length];

            byte[] datam = YV12toYUV420Planar(data,outpt,1280,720);

            encode(datam);
}

最佳答案

这个功能解决了我的问题

public static byte[] NV21toYUV420Planar(byte[] input, byte[] output, int width, int height) {
    final int frameSize = width * height;
    final int qFrameSize = frameSize/4;

    System.arraycopy(input, 0, output, 0, frameSize); // Y

    byte v, u;

    for (int i = 0; i < qFrameSize; i++) {
        v = input[frameSize + i*2];
        u = input[frameSize + i*2 + 1];

        output[frameSize + i + qFrameSize] = v;
        output[frameSize + i] = u;
    }

    return output;
}

关于android - Nv21字节数据在android中保存为彩色H264格式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53932501/

相关文章:

android - ArrayAdapter.createViewFromResource() 上的 NullPointerException

android - android.view.InflateException膨胀类com.android.internal.widget.ActionBarContextView发生错误

encoding - 从十进制转换为十六进制

android - 如何在关闭另一个 DialogFragment 后刷新 DialogFragment

android - 如何在 Android Manifest Application 中添加多个 `tools:replace`?

c# - (简单)带有特殊字符的 CSV 下载让我抓狂

eclipse - 在 Apache Tomcat 上运行 jsp 网页的编码问题

ios - 解码 H264 VideoToolkit API 失败,在 VTDecompressionSessionCreate 中出现错误 -8971

c++ - 使用 FFmpeg 在 mp4 容器中寻找 h264 编解码器的视频帧。数据包点数始终为0

c# - 如何在 Media Foundation (Windows 10) 中使用硬件(nvenc) 编码