目的
将pcm数据保存到文件中,方便使用pcm音频播放器来进行播放。
代码
void writeAVFramePcmToFile(AVFrame *inFrame,QFile *outPcmFilePtr)
{
if(NULL == inFrame)
{
qDebug()<<"robin:InFrame is NULL";
return;
}
int t_data_size = av_samples_get_buffer_size(
NULL, inFrame->channels,
inFrame->nb_samples,
(AVSampleFormat)inFrame->format,
0);
if(t_data_size < 0)
{
qDebug()<<"robin:av_samples_get_buffer_size return:"<<t_data_size<<"||so return.";
return;
}
if(av_sample_fmt_is_planar((AVSampleFormat)inFrame->format))
{//如果是平面的
uint8_t *buf = (uint8_t *)malloc(t_data_size);
interleave(inFrame->data, buf,
inFrame->channels, (AVSampleFormat)inFrame->format, t_data_size);
outPcmFilePtr->write((const char *)buf,t_data_size);
outPcmFilePtr->flush();
free(buf);
}
else
{
outPcmFilePtr->write((const char *)inFrame->data[0],t_data_size);
outPcmFilePtr->flush();
}
}
总结
关键点就是:pcm数据是否是平面类型
(年纪大了,总感觉这个问题以前写过博客,算了,再写一遍吧)