ffmpeg推送AAC音频处理ADTS

来源:互联网 发布:java将双引号替换空格 编辑:程序博客网 时间:2024/06/05 11:48

原来取rtmp流如:rtmp://live.hkstv.hk.lxdns.com/live/hks

或者录播保存的文件,它们的aac音频包都没有ADTS头部,

但客户提供的一个http流:http://cntv.hls.cdn.myqcloud.com/asp/hls/850/0303000a/3/default/1ee473b960054ae29256751f50033d34/850.m3u8

它的每个aac音频包有adts头,

不去掉头直接调用ffmpeg的av_interleaved_write_frame会失败的,

简单去掉7字节adts头,接口调用成功了,但fms地址播放没有声音,用vlc播放器有断续的声音。

用ffmpeg可执行程序推送要对音频加上-bsf:a  aac_adtstoasc才能推送,但视频还是有问题的。

原因是音频输出流中的codec的extra_data为空,要分配2字节的空间,将音频采样率和音频通道数设置进去。

out_stream2->codec->extradata = (uint8_t*)av_malloc(2);
out_stream2->codec->extradata_size = 2;
unsigned char dsi1[2];
unsigned int sampling_frequency_index = (unsigned int)get_sr_index((unsigned int)out_stream2->codec->sample_rate);
make_dsi(sampling_frequency_index, (unsigned int)out_stream2->codec->channels, dsi1 );  
memcpy(out_stream2->codec->extradata, dsi1, 2);

这个get_sr_index就是根据采样率返回下标,与adts的sampling_frequency_index定义相同,如48000返回3


void make_dsi(unsigned int sampling_frequency_index, 
 unsigned int channel_configuration, 
 unsigned char* dsi )

unsigned int object_type = 2; // AAC LC by default 
dsi[0] = (object_type<<3) | (sampling_frequency_index>>1); 
dsi[1] = ((sampling_frequency_index&1)<<7) | (channel_configuration<<3);
}