获取具有FFMPEG样品的总数

Obtain the total number of samples with FFMpeg

本文关键字:FFMPEG 获取      更新时间:2023-10-16

当前我的应用程序基于while-realloc循环读取音频文件:

// Pseudocode
float data* = nullptr;
int size = 0;
AVFrame* frame;
while(readFrame(formatContext, frame))
{
    data = realloc(data, size + frame.nSamples);
    size += frame.nSamples;
    /* Read frame samples into data */
}

有没有办法在开始时获得流中的样本总数?我希望能够用new[]而不是malloc创建数组。

供参考,此处回答:FFMPEG:如何估计音频流中的样本数?

我在代码中使用了以下内容:

int total_samples = (int) ((format_context->duration / (float) AV_TIME_BASE) * SAMPLE_RATE * NUMBER_CHANNELS);

注意:我的测试表明,此计算很可能大于实际的样本数量,因此请确保您在代码中补偿了该样本。我将所有剩余的" Unset"样本设置为零。