fffmpeg音频解码C例程
时间: 2023-08-25 11:16:12 浏览: 50
您好!以下是一个使用FFmpeg进行音频解码的C语言例程:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <libavcodec/avcodec.h>
#include <libavformat/avformat.h>
#include <libavutil/frame.h>
int main(int argc, char *argv[]) {
AVFormatContext *formatContext = NULL;
AVCodecContext *codecContext = NULL;
AVCodec *codec = NULL;
AVFrame *frame = NULL;
AVPacket packet;
int audioStreamIndex, ret;
// 初始化FFmpeg
av_register_all();
// 打开输入文件
if (avformat_open_input(&formatContext, argv[1], NULL, NULL) != 0) {
fprintf(stderr, "无法打开输入文件\n");
return -1;
}
// 查找音频流信息
if (avformat_find_stream_info(formatContext, NULL) < 0) {
fprintf(stderr, "无法获取流信息\n");
avformat_close_input(&formatContext);
return -1;
}
// 查找音频流
audioStreamIndex = -1;
for (int i = 0; i < formatContext->nb_streams; i++) {
if (formatContext->streams[i]->codecpar->codec_type == AVMEDIA_TYPE_AUDIO) {
audioStreamIndex = i;
break;
}
}
if (audioStreamIndex == -1) {
fprintf(stderr, "未找到音频流\n");
avformat_close_input(&formatContext);
return -1;
}
// 获取解码器参数
codecContext = avcodec_alloc_context3(NULL);
if (!codecContext) {
fprintf(stderr, "无法分配解码器上下文\n");
avformat_close_input(&formatContext);
return -1;
}
if (avcodec_parameters_to_context(codecContext, formatContext->streams[audioStreamIndex]->codecpar) < 0) {
fprintf(stderr, "无法获取解码器参数\n");
avcodec_free_context(&codecContext);
avformat_close_input(&formatContext);
return -1;
}
// 查找解码器
codec = avcodec_find_decoder(codecContext->codec_id);
if (codec == NULL) {
fprintf(stderr, "未找到解码器\n");
avcodec_free_context(&codecContext);
avformat_close_input(&formatContext);
return -1;
}
// 打开解码器
if (avcodec_open2(codecContext, codec, NULL) < 0) {
fprintf(stderr, "无法打开解码器\n");
avcodec_free_context(&codecContext);
avformat_close_input(&formatContext);
return -1;
}
// 分配帧内存
frame = av_frame_alloc();
if (!frame) {
fprintf(stderr, "无法分配帧内存\n");
avcodec_free_context(&codecContext);
avformat_close_input(&formatContext);
return -1;
}
// 读取音频数据
while (av_read_frame(formatContext, &packet) >= 0) {
if (packet.stream_index == audioStreamIndex) {
ret = avcodec_send_packet(codecContext, &packet);
if (ret < 0) {
fprintf(stderr, "解码错误: %d\n", ret);
break;
}
while (ret >= 0) {
ret = avcodec_receive_frame(codecContext, frame);
if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF) {
break;
} else if (ret < 0) {
fprintf(stderr, "解码错误: %d\n", ret);
break;
}
// 在这里处理解码后的音频帧数据
// frame->data[0], frame->data[1], ... 分别是各个声道的音频数据
// frame->nb_samples 是每个声道的采样数
}
av_packet_unref(&packet);
}
}
// 释放资源
av_frame_free(&frame);
avcodec_free_context(&codecContext);
avformat_close_input(&formatContext);
return 0;
}
```
请注意,在使用此例程之前,您需要确保已正确安装了FFmpeg库,并将其正确链接到您的项目中。此外,您需要将音频文件的路径作为命令行参数传递给该程序。
希望这能帮助到您!如果您还有其他问题,请随时提问。
相关推荐
![](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)