基础知识预备
- 物体震动引起空气震动,震动波形在空气中传播到耳朵的时候,造成耳膜的震动,从而接受到声音信号,自然界中的声音是基于时间的连续信号,属于模拟信号,声音的三要素,音色由由波形决定,音高由振幅决定,音高由频率决定,但是模拟信号传输不方便,需要把它转化为数字信号,根据奈奎斯特采样定律,只要采样频率高于原信号最高频率的两倍就能把数字信号还原为模拟信号,从而不会造成太多的失真,人类的耳朵能感知到的频率范围为
2khz~20khz
,所以一般使用的采样频率为44.1khz
- 模拟信号到数字信号的过程经过了
量化
,采样
,和调制
,人类对某些部分的声音不敏感,所以去掉这些部分的声音能有效减少音频文件的大小而不会对音频质量有大的影响,经过处理的压缩文件....
//注册所有组件
av_register_all();
//打开文件封装格式,需要先获取文件封装格式上下文
AVFormatContext * avFormat_context = avformat_alloc_context();
const char * cinFilePath = [inputFilePath UTF8String];
if (avformat_open_input(&avFormat_context,cinFilePath , NULL, NULL) != 0) {
NSLog(@"打开文件失败");
return;
}
//找到对应的流(音频流, 视频流, 字幕流)
if(avformat_find_stream_info(avFormat_context, NULL) < 0){
NSLog(@"查找失败");
return;
}
int av_stream_index = -1;
//查找音频解码器
for (int i = 0; i < avFormat_context->nb_streams; i++) {
if (avFormat_context->streams[i]->codecpar->codec_type == AVMEDIA_TYPE_AUDIO) {
av_stream_index = i;
}
}
//音频解码器上下文
AVCodecContext * av_codec_context = avFormat_context->streams[av_stream_index]->codec;
//获取解码器
AVCodec * av_codec = avcodec_find_decoder(av_codec_context->codec_id);
//打开解码器
if (avcodec_open2(av_codec_context, av_codec, NULL) != 0) {
NSLog(@"打开解码器失败");
return ;
}
NSLog(@"---解码器名称:%s",av_codec->name);
//循环读取数据
AVPacket * avPacket = (AVPacket*)av_malloc(sizeof(AVPacket));
AVFrame * avFrame = av_frame_alloc();
//初始化p音频采样数据上下文
SwrContext * swrContext = swr_alloc();
//设置默认配置
int64_t in_ch_layout = av_get_default_channel_layout(av_codec_context->channels);
//设置采样精度
swr_alloc_set_opts(swrContext, AV_CH_LAYOUT_STEREO, AV_SAMPLE_FMT_S16, av_codec_context->sample_rate, in_ch_layout, av_codec_context->sample_fmt, av_codec_context->sample_rate, 0, NULL);
swr_init(swrContext);
int MAX_AUDIO_SIZE = 44100 * 2;
uint8_t * out_buffer = (uint8_t*)av_malloc(MAX_AUDIO_SIZE);
//获取缓冲区事假大小
int out_nb_channels = av_get_channel_layout_nb_channels(AV_CH_LAYOUT_STEREO);
//打开文件
const char * outfile = [outFilepath UTF8String];
FILE * file_pcm = fopen(outfile, "wb+");
if (file_pcm == NULL) {
NSLog(@"打开文件失败");
return;
}
int current_index = 0;
while (av_read_frame(avFormat_context, avPacket) >= 0) {
//判断这帧数据的类型
if (avPacket->stream_index == av_stream_index) {
avcodec_send_packet(av_codec_context, avPacket);
int ret = avcodec_receive_frame(av_codec_context, avFrame);
if (ret == 0) {//转码成功
swr_convert(swrContext, &out_buffer, MAX_AUDIO_SIZE, (const uint8_t **)avFrame->data, avFrame->nb_samples);
//获取缓冲区实际大小
int buffer_size = av_samples_get_buffer_size(NULL, out_nb_channels, avFrame->nb_samples, av_codec_context->sample_fmt, 1);
fwrite(out_buffer, 1, buffer_size, file_pcm);
// float progress = current_index ;
current_index++;
NSLog(@"当前解码到了第%d帧",current_index);
}
}
}
completionBlock(nil, true);
//释放资源
av_packet_free(&avPacket);
fclose(file_pcm);
av_frame_free(&avFrame);
free(out_buffer);
avcodec_close(av_codec_context);
// avcodec_close(vidio_codec_context);
avformat_free_context(avFormat_context);

运行代码,解码成功
网友评论