-1

When I use mediacodec audio merging into the video, I just want to capture audio a fragment, from 10 s of the audio, when I call android. Media. MediaExtractor# seekTo, introduced into 10 s parameters, the final stop MediaMuxer will be an error, the sample code:

 private void startAudioCodec(MediaCodec decoder, MediaCodec encoder, MediaExtractor extractor, long firstSampleTime, long startPosition, long duration) {
    MediaCodec.BufferInfo info = new MediaCodec.BufferInfo();
    MediaCodec.BufferInfo outputInfo = new MediaCodec.BufferInfo();
    boolean done = false;
    boolean inputDone = false;
    boolean decodeDone = false;
    extractor.seekTo(firstSampleTime + startPosition, SEEK_TO_PREVIOUS_SYNC);
    int decodeinput = 0;
    int encodeinput = 0;
    int encodeoutput = 0;
    long lastEncodeOutputTimeStamp = -1;
    int channelCount = 1;
    info.presentationTimeUs = 0;


    while (!done) {
        if (!inputDone) {
            int inputIndex = decoder.dequeueInputBuffer(TIMEOUT_USEC);
            if (inputIndex >= 0) {
                ByteBuffer inputBuffer = decoder.getInputBuffer(inputIndex);
                inputBuffer.clear();
                int readSampleData = extractor.readSampleData(inputBuffer, 0);
                long dur = extractor.getSampleTime() - firstSampleTime - startPosition;
                if ((dur < duration) && readSampleData > 0) {
                    decoder.queueInputBuffer(inputIndex, 0, readSampleData, extractor.getSampleTime()-startPosition, 0);//pts need to subtract the value of startPosition
                    decodeinput++;
                    // System.out.println("videoCliper audio decodeinput" + decodeinput + " dataSize" + readSampleData + " sampeTime" + extractor.getSampleTime());
                    extractor.advance();
                } else {
                    decoder.queueInputBuffer(inputIndex, 0, 0, 0, MediaCodec.BUFFER_FLAG_END_OF_STREAM);
                    System.out.println("videoCliper audio decodeInput end");
                    inputDone = true;
                }
            }
        }

the error log: 2020-03-28 14:49:22.549 32612-2052/com.srwl.mytx E/MPEG4Writer: The number of recorded samples is 0 2020-03-28 14:49:22.550 32612-2053/com.srwl.mytx I/MPEG4Writer: Received total/0-length (999/0) buffers and encoded 998 frames. - Video 2020-03-28 14:49:22.550 32612-2052/com.srwl.mytx E/MPEG4Writer: Dumping Audio track's last 10 frames timestamp and frame type 2020-03-28 14:49:22.550 32612-2052/com.srwl.mytx W/MPEG4Writer: 0-duration samples found: 1 2020-03-28 14:49:22.550 32612-2052/com.srwl.mytx W/MPEG4Writer: 0-duration samples found: 1 2020-03-28 14:49:22.550 32612-2052/com.srwl.mytx I/MPEG4Writer: Received total/0-length (0/0) buffers and encoded 0 frames. - Audio 2020-03-28 14:49:22.550 32612-2052/com.srwl.mytx I/MPEG4Writer: Audio track drift time: 0 us 2020-03-28 14:49:22.558 32612-2006/com.srwl.mytx E/AndroidRuntime: FATAL EXCEPTION: pool-7-thread-2 Process: com.srwl.mytx, PID: 32612 java.lang.IllegalStateException: Failed to stop the muxer at android.media.MediaMuxer.nativeStop(Native Method)

I hope someone can tell me what went wrong. Thank you

miken32
  • 42,008
  • 16
  • 111
  • 154

1 Answers1

0

The problem has already been settled.

like this :

  outputInfo.presentationTimeUs -= startPosition;
                    if (outputInfo.presentationTimeUs > lastEncodeOutputTimeStamp) {
                        encodeoutput++;
                        System.out.println("videoCliper audio encodeOutput" + encodeoutput + " dataSize" + outputInfo.size + " sampeTime" + outputInfo.presentationTimeUs);
                        mMediaMuxer.writeSampleData(muxAudioTrack, encodedData, outputInfo);
                        lastEncodeOutputTimeStamp = outputInfo.presentationTimeUs;
                    }

the pts write to mediamuxer need subtracted from the start time