写一个基于ffmpeg采集音频后进行编码成aac格式的程序代码
时间: 2023-03-13 09:19:51 浏览: 99
使用ffmpeg采集和编码音频的程序代码:// 初始化ffmpeg资源
av_register_all();// 分配输入上下文
AVFormatContext *pFormatCtx = avformat_alloc_context();// 打开音频文件
if (avformat_open_input(&pFormatCtx, szFilePath, NULL, NULL) != 0) {
printf("Couldn't open input stream.\n");
return -1;
}// 查找音频流
if (avformat_find_stream_info(pFormatCtx, NULL) < 0) {
printf("Couldn't find stream information.\n");
return -1;
}// 获取音频流索引
int i;
int audioStream = -1;
for (i = 0; i < pFormatCtx->nb_streams; i++) {
if (pFormatCtx->streams[i]->codec->codec_type == AVMEDIA_TYPE_AUDIO) {
audioStream = i;
break;
}
}// 获取音频流参数
AVCodecContext *pCodecCtx = pFormatCtx->streams[audioStream]->codec;// 查找解码器
AVCodec *pCodec = avcodec_find_decoder(pCodecCtx->codec_id);
if (pCodec == NULL) {
printf("Codec not found.\n");
return -1;
}// 打开解码器
if (avcodec_open2(pCodecCtx, pCodec, NULL) < 0) {
printf("Could not open codec.\n");
return -1;
}// 分配缓冲区
AVPacket *packet = (AVPacket *)av_malloc(sizeof(AVPacket));
AVFrame *pFrame = av_frame_alloc();// 分配编码输出缓冲
int out_buffer_size = av_samples_get_buffer_size(NULL, pCodecCtx->channels,
pCodecCtx->frame_size, pCodecCtx->sample_fmt, 0);
uint8_t *out_buffer = (uint8_t *)av_malloc(out_buffer_size);// 分配编码上下文
AVCodec *aacCodec = avcodec_find_encoder(AV_CODEC_ID_AAC);
AVCodecContext *aacCodecCtx = avcodec_alloc_context3(aacCodec);// 设置编码参数
aacCodecCtx->bit_rate = 64000;
aacCodecCtx->sample_fmt = AV_SAMPLE_FMT_FLTP;
aacCodecCtx->sample_rate = pCodecCtx->sample_rate;
aacCodecCtx->channel_layout = pCodecCtx->channel_layout;
aacCodecCtx->channels = pCodecCtx->channels;// 打开编码器
if (avcodec_open2(aacCodecCtx, aacCodec, NULL) < 0) {
printf("Could not open AAC codec.\n");
return -1;
}// 开始解码
int frameFinished = 0;
while (av_read_frame(pFormatCtx, packet) >= 0) {
if (packet->stream_index == audioStream) {
// 解码音频帧
avcodec_decode_audio4(pCodecCtx, pFrame, &frameFinished, packet); // 编码aac
if (frameFinished) {
int out_size = avcodec_encode_audio2(aacCodecCtx, out_buffer, out_buffer_size, pFrame);
}
} // 释放资源
av_free_packet(packet);
av_frame_free(&pFrame);
av_free(out_buffer);
avcodec_close(aacCodecCtx);
avcodec_close(pCodecCtx);
avformat_close_input(&pFormatCtx);
}
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)