在存储 PCM 音频数据时,可以通过以下方式获取 AVFrame 中音频数据的长度(以样本数为单位):
int getAudioSampleCount(const AVFrame* frame) {
int sampleCount = frame->nb_samples;
return sampleCount;
}
其中,frame
是一个指向 AVFrame 结构体的指针。这个函数将返回音频数据的长度(以样本数为单位)。请注意,每个样本可能包含多个通道的采样值。
如果需要获取字节级别上的音频数据大小,请使用 av_samples_get_buffer_size
函数来计算缓冲区大小。具体示例如下:
int getAudioDataSize(const AVFrame* frame) {
int dataSize = av_samples_get_buffer_size(NULL, frame->channels, frame->nb_samples,
static_cast<AVSampleFormat>(frame->format), 0);
return dataSize;
}
这个函数会根据帧中的通道数、样本数和采样格式来计算音频数据占用的字节数。
内容由零声教学AI助手提供,问题来源于学员提问