1、使用avformat_open_input函数打开音视频文件
2、使用avformat_find_stream_info函数获取解码器信息。
3、使用av_dump_format设置打印信息
4、使用av_init_packet初始化AVPacket。
5、使用av_find_best_stream查找对应音视频流的流下标。
6、使用av_read_frame读取音视频帧,判断AVPacket的stream_index是否等于音频下标。
7、如果是音频帧,则先写AAC存储格式ADTS头,然后写音频帧数据。
8、调用avformat_close_input关闭输入文件。
ADTS头部:
int adts_header ( char * const p_adts_header, const int data_length, const int profile, const int samplerate, const int channels)
{ int sampling_frequency_index = 3 ; int adtsLen = data_length + 7 ; int frequencies_size = sizeof ( sampling_frequencies) / sizeof ( sampling_frequencies[ 0 ] ) ; int i = 0 ; for ( i = 0 ; i < frequencies_size; i++ ) { if ( sampling_frequencies[ i] == samplerate) { sampling_frequency_index = i; break ; } } if ( i >= frequencies_size) { printf ( "unsupport samplerate:%d\n" , samplerate) ; return - 1 ; } p_adts_header[ 0 ] = 0xff ; p_adts_header[ 1 ] = 0xf0 ; p_adts_header[ 1 ] |= ( 0 << 3 ) ; p_adts_header[ 1 ] |= ( 0 << 1 ) ; p_adts_header[ 1 ] |= 1 ; p_adts_header[ 2 ] = ( profile) << 6 ; p_adts_header[ 2 ] |= ( sampling_frequency_index & 0x0f ) << 2 ; p_adts_header[ 2 ] |= ( 0 << 1 ) ; p_adts_header[ 2 ] |= ( channels & 0x04 ) >> 2 ; p_adts_header[ 3 ] = ( channels & 0x03 ) << 6 ; p_adts_header[ 3 ] |= ( 0 << 5 ) ; p_adts_header[ 3 ] |= ( 0 << 4 ) ; p_adts_header[ 3 ] |= ( 0 << 3 ) ; p_adts_header[ 3 ] |= ( 0 << 2 ) ; p_adts_header[ 3 ] |= ( ( adtsLen & 0x1800 ) >> 11 ) ; p_adts_header[ 4 ] = ( uint8_t ) ( ( adtsLen & 0x7f8 ) >> 3 ) ; p_adts_header[ 5 ] = ( uint8_t ) ( ( adtsLen & 0x7 ) << 5 ) ; p_adts_header[ 5 ] |= 0x1f ; p_adts_header[ 6 ] = 0xfc ; return 0 ;
}