下面是一个使用FFmpeg库解码MP3文件的示例代码:
时间: 2023-03-19 12:24:18 浏览: 103
以下是一个使用FFmpeg库解码MP3文件的示例代码:
```c++
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
#include <inttypes.h>
#include <errno.h>
#include <math.h>
#include <unistd.h>
#include <fcntl.h>
#include <libavcodec/avcodec.h>
#include <libavformat/avformat.h>
#include <libswresample/swresample.h>
#define INBUF_SIZE 4096
int main(int argc, char **argv)
{
AVFormatContext *fmt_ctx = NULL;
AVCodecContext *dec_ctx = NULL;
AVCodec *dec = NULL;
AVPacket pkt;
AVFrame *frame = NULL;
int stream_index = 0, ret = 0, got_frame = 0;
uint8_t inbuf[INBUF_SIZE + AV_INPUT_BUFFER_PADDING_SIZE];
uint8_t *data = NULL;
size_t data_size = 0;
if (argc <= 1) {
fprintf(stderr, "Usage: %s <input file>\n", argv[0]);
exit(0);
}
av_register_all();
if ((ret = avformat_open_input(&fmt_ctx, argv[1], NULL, NULL)) < 0) {
fprintf(stderr, "Could not open input file '%s' (error '%s')\n",
argv[1], av_err2str(ret));
exit(1);
}
if ((ret = avformat_find_stream_info(fmt_ctx, NULL)) < 0) {
fprintf(stderr, "Could not find stream information (error '%s')\n",
av_err2str(ret));
exit(1);
}
av_dump_format(fmt_ctx, 0, argv[1], 0);
if ((stream_index = av_find_best_stream(fmt_ctx, AVMEDIA_TYPE_AUDIO, -1, -1, NULL, 0)) < 0) {
fprintf(stderr, "Could not find audio stream in the input file\n");
exit(1);
}
dec_ctx = avcodec_alloc_context3(NULL);
if (!dec_ctx) {
fprintf(stderr, "Failed to allocate codec context\n");
exit(1);
}
if ((ret = avcodec_parameters_to_context(dec_ctx, fmt_ctx->streams[stream_index]->codecpar)) < 0) {
fprintf(stderr, "Failed to copy codec parameters to codec context (error '%s')\n",
av_err2str(ret));
exit(1);
}
if (!(dec = avcodec_find_decoder(dec_ctx->codec_id))) {
fprintf(stderr, "Failed to find codec\n");
exit(1);
}
if ((ret = avcodec_open2(dec_ctx, dec, NULL)) < 0) {
fprintf(stderr, "Failed to open codec (error '%s')\n", av_err2str(ret));
exit(1);
}
av_init_packet(&pkt);
pkt.data = inbuf;
pkt.size = 0;
frame = av_frame_alloc();
if (!frame) {
fprintf(stderr, "Failed to allocate frame\n");
exit(1);
}
while (av_read_frame(fmt_ctx, &pkt) >= 0) {
if (pkt.stream_index == stream_index) {
ret = avcodec_decode_audio4(dec_ctx, frame, &got_frame, &pkt);
if (ret < 0) {
fprintf(stderr, "Error decoding audio frame (error '%s')\n", av_err2str(ret));
exit(1);
}
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.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)