0

I am getting data from Camera and I am trying to convert NV21 data to .H264 format. I am done this with MediaCodec but When I saved .H264 format it doesn't have audio and only playing Video at VLC media player. I want to play Video with its audio.Can I do this with using .H264 format or Do I use other converting format? How can do this? I shared below my code.

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);
}

private void initCodec() {
    try {
        fos = new FileOutputStream(mVideoFile);
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }
    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, 15);
    mediaFormat.setInteger(MediaFormat.KEY_COLOR_FORMAT,
            MediaCodecInfo.CodecCapabilities.COLOR_FormatYUV420Planar);
    mediaFormat.setInteger(MediaFormat.KEY_I_FRAME_INTERVAL, 5);
    mMediaCodec.configure(mediaFormat,
            null,
            null,
            MediaCodec.CONFIGURE_FLAG_ENCODE);
    mMediaCodec.start();

}
Diego
  • 937
  • 8
  • 24
  • Hi my friend,I saw your post,I have some problem with you.When I played .H264 byte format I can't take audio,my aim is playing video with its audio simultaneously.I shared my code at above,how did you solve this problem. Brendon Tsai – Diego Dec 19 '18 at 07:07
  • H264 never contains any audio, it's a video compression standard. You'll probably have to take a few steps back and look on your incoming data. Is there a audio channel at all? If so, have a look at MediaMuxer. – ChrisBe Dec 19 '18 at 11:18
  • How can I understand my byte array has a audio channel? – Diego Dec 19 '18 at 12:50
  • See [this question](https://stackoverflow.com/questions/19826809/encoding-aac-audio-using-audiorecord-and-mediacodec-on-android). – greeble31 Dec 19 '18 at 20:39
  • You have to mux your audio track and your video track together using MediaMuxer to produce a .mp4 file (.mp4 will be the container wich contains your video and audio tracks that must be synchronized), h264 is a video format which contains a frame sequence,no audio is included.... you can find many example on the internet or github – E.Abdel Dec 19 '18 at 21:58
  • Can you give example code,I cannot find video and audio tracking and combinig to save – Diego Dec 20 '18 at 13:42

0 Answers0