1

我正在尝试将音频添加到由以下开源项目创建的视频中

具体到https://github.com/madisp/trails/blob/master/app/src/main/java/com/madisp/trails/CaptureService.java

我需要从 MIC 获取音频并将其作为音轨写入编码文件。目前用 Muxer 编码的文件只有视频轨道。

我可以从 MIC 获取音频,没有任何问题

int nChannels = 1;
int minBufferSize = AudioRecord.getMinBufferSize(44100, AudioFormat.CHANNEL_IN_MONO, AudioFormat.ENCODING_PCM_16BIT) * 2;
AudioRecord aRecorder = new AudioRecord(MediaRecorder.AudioSource.MIC, 44100, AudioFormat.CHANNEL_IN_MONO, AudioFormat.ENCODING_PCM_16BIT, minBufferSize);

short[] buffer = new short[44100 * nChannels];
aRecorder.startRecording();
int readSize = 0;

while (recording) {
    readSize = aRecorder.read(buffer, 0, minBufferSize);
    if (readSize < 0) {
        break;
    } else if (readSize > 0) {
        // do stuff with buffer
    }
}
aRecorder.stop();
aRecorder.release();

但我不知道如何将其合并到(https://github.com/madisp/trails/blob/master/app/src/main/java/com/madisp/trails/CaptureService.java

while (running) {
    int index = avc.dequeueOutputBuffer(info, 10000);
    if (index == MediaCodec.INFO_OUTPUT_FORMAT_CHANGED) {
        if (track != -1) {
            throw new RuntimeException("format changed twice");
        }
        track = muxer.addTrack(avc.getOutputFormat());
        muxer.start();
    } else if (index >= 0) {
        if ((info.flags & MediaCodec.BUFFER_FLAG_CODEC_CONFIG) != 0) {
            // ignore codec config
            info.size = 0;
        }
        if (track != -1) {
            ByteBuffer out = avc.getOutputBuffer(index);
            out.position(info.offset);
            out.limit(info.offset + info.size);
            muxer.writeSampleData(track, out, info);
            avc.releaseOutputBuffer(index, false);
        }
    }
}

是的,明白我实际上是在要求您编写代码,但我没有这方面的专业知识

任何帮助表示赞赏

谢谢

4

1 回答 1

2

首先,使用byte[]而不是short[]用于与AudioRecord- 一起使用的缓冲区,这将简化一些事情。

然后,为了对接收到的缓冲区进行编码,这样的事情应该可以工作(未经测试):

while (recording) {
    readSize = aRecorder.read(buffer, 0, minBufferSize);
    if (readSize < 0) {
        break;
    } else if (readSize > 0) {
        boolean done = false;
        while (!done) {
            int index = avc.dequeueInputBuffer(10000);
            if (index >= 0) { // In case we didn't get any input buffer, it may be blocked by all output buffers being full, thus try to drain them below if we didn't get any
                ByteBuffer in = avc.getIndexBuffer(index);
                in.clear();
                in.put(buffer, 0, readSize);
                avc.queueInputBuffer(index, 0, readSize, System.nanoTime()/1000, 0);
                done = true; // Done passing the input to the codec, but still check for available output below
            }
            index = avc.dequeueOutputBuffer(info, 10000);
            if (index == MediaCodec.INFO_OUTPUT_FORMAT_CHANGED) {
                if (track != -1) {
                    throw new RuntimeException("format changed twice");
                }
                track = muxer.addTrack(avc.getOutputFormat());
                muxer.start();
            } else if (index >= 0) {
                if ((info.flags & MediaCodec.BUFFER_FLAG_CODEC_CONFIG) != 0) {
                    // ignore codec config
                    info.size = 0;
                }
                if (track != -1 && info.size > 0) {
                    ByteBuffer out = avc.getOutputBuffer(index);
                    out.position(info.offset);
                    out.limit(info.offset + info.size);
                    muxer.writeSampleData(track, out, info);
                    avc.releaseOutputBuffer(index, false);
                }
            }
        }
    }
}

我认为普通的 SW AAC 编码器应该可以将任意数量的音频字节传递给它,但如果编码器很挑剔,你需要将记录的数据以 1024 个样本块的形式传递(单声道为 2048 个字节) , 4096 字节的立体声)。

于 2014-12-26T22:35:11.340 回答