写一个ffmpeg播放MP3的demo

时间: 2023-07-08 08:29:16 浏览: 49
以下是一个使用FFmpeg播放MP3文件的示例代码,使用了libavformat和libavcodec库: ```c #include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h> #include <pthread.h> #include <libavformat/avformat.h> #include <libavcodec/avcodec.h> #include <libswresample/swresample.h> #define BUFFER_SIZE 4096 int main(int argc, char *argv[]) { AVFormatContext *format_ctx = NULL; AVCodecContext *codec_ctx = NULL; AVCodecParameters *codec_params = NULL; AVCodec *codec = NULL; AVFrame *frame = NULL; AVPacket *packet = NULL; SwrContext *swr_ctx = NULL; uint8_t *buffer = NULL; int buffer_size = BUFFER_SIZE; int stream_index = -1; int ret; if (argc < 2) { printf("Usage: %s <input_file>\n", argv[0]); return 1; } av_register_all(); avformat_network_init(); format_ctx = avformat_alloc_context(); if (!format_ctx) { printf("Error: Could not allocate format context.\n"); return 1; } ret = avformat_open_input(&format_ctx, argv[1], NULL, NULL); if (ret < 0) { printf("Error: Could not open input file '%s'\n", argv[1]); return 1; } ret = avformat_find_stream_info(format_ctx, NULL); if (ret < 0) { printf("Error: Could not find stream information.\n"); return 1; } av_dump_format(format_ctx, 0, argv[1], 0); for (int i = 0; i < format_ctx->nb_streams; i++) { if (format_ctx->streams[i]->codecpar->codec_type == AVMEDIA_TYPE_AUDIO) { stream_index = i; break; } } if (stream_index == -1) { printf("Error: Could not find audio stream.\n"); return 1; } codec_params = format_ctx->streams[stream_index]->codecpar; codec = avcodec_find_decoder(codec_params->codec_id); if (!codec) { printf("Error: Unsupported codec.\n"); return 1; } codec_ctx = avcodec_alloc_context3(codec); if (!codec_ctx) { printf("Error: Could not allocate codec context.\n"); return 1; } ret = avcodec_parameters_to_context(codec_ctx, codec_params); if (ret < 0) { printf("Error: Could not copy codec parameters to codec context.\n"); return 1; } ret = avcodec_open2(codec_ctx, codec, NULL); if (ret < 0) { printf("Error: Could not open codec.\n"); return 1; } frame = av_frame_alloc(); packet = av_packet_alloc(); if (!frame || !packet) { printf("Error: Could not allocate frame and packet.\n"); return 1; } swr_ctx = swr_alloc(); if (!swr_ctx) { printf("Error: Could not allocate resampling context.\n"); return 1; } av_opt_set_int(swr_ctx, "in_channel_layout", codec_ctx->channel_layout, 0); av_opt_set_int(swr_ctx, "out_channel_layout", AV_CH_LAYOUT_STEREO, 0); av_opt_set_int(swr_ctx, "in_sample_rate", codec_ctx->sample_rate, 0); av_opt_set_int(swr_ctx, "out_sample_rate", 44100, 0); av_opt_set_sample_fmt(swr_ctx, "in_sample_fmt", codec_ctx->sample_fmt, 0); av_opt_set_sample_fmt(swr_ctx, "out_sample_fmt", AV_SAMPLE_FMT_S16, 0); ret = swr_init(swr_ctx); if (ret < 0) { printf("Error: Could not initialize resampling context.\n"); return 1; } buffer = av_malloc(buffer_size); if (!buffer) { printf("Error: Could not allocate buffer.\n"); return 1; } while (av_read_frame(format_ctx, packet) >= 0) { if (packet->stream_index == stream_index) { ret = avcodec_send_packet(codec_ctx, packet); if (ret < 0) { printf("Error: Could not send packet to decoder.\n"); break; } while (ret >= 0) { ret = avcodec_receive_frame(codec_ctx, frame); if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF) break; else if (ret < 0) { printf("Error: Could not receive frame from decoder.\n"); goto end; } int data_size = av_samples_get_buffer_size(NULL, codec_ctx->channels, frame->nb_samples, AV_SAMPLE_FMT_S16, 1); if (data_size > buffer_size) { buffer_size = data_size; buffer = av_realloc(buffer, buffer_size); } swr_convert(swr_ctx, &buffer, frame->nb_samples, (const uint8_t **)frame->data, frame->nb_samples); fwrite(buffer, 1, data_size, stdout); } } av_packet_unref(packet); } end: avformat_close_input(&format_ctx); avcodec_free_context(&codec_ctx); avformat_free_context(format_ctx); av_frame_free(&frame); av_packet_free(&packet); swr_free(&swr_ctx); av_free(buffer); return 0; } ``` 这个示例代码会打开指定的MP3文件, 解码音频流, 并输出16-bit PCM音频数据到stdout。您可以将输出重定向到另一个程序或使用管道将其传递给ffplay等播放器进行播放。请注意,您需要在编译时链接FFmpeg库。

相关推荐

最新推荐

recommend-type

使用 FFmpeg 命令拼接mp3音频文件异常问题及解决方法

主要介绍了使用 FFmpeg 命令拼接mp3音频文件异常问题及解决方法,本文给大家介绍的非常详细,对大家的工作或学习具有一定的参考借鉴价值,需要的朋友可以参考下
recommend-type

如何用FFmpeg编写一个简单播放器详细步骤介绍

通过一步一步的介绍怎么去调用ffmpeg和sdl的库,来编写一个简单的视频播放器,使自己了解整个过程。
recommend-type

python调用系统ffmpeg实现视频截图、http发送

主要为大家详细介绍了python调用系统ffmpeg实现视频截图、http发送,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
recommend-type

pre_o_1csdn63m9a1bs0e1rr51niuu33e.a

pre_o_1csdn63m9a1bs0e1rr51niuu33e.a
recommend-type

matlab建立计算力学课程的笔记和文件.zip

matlab建立计算力学课程的笔记和文件.zip
recommend-type

zigbee-cluster-library-specification

最新的zigbee-cluster-library-specification说明文档。
recommend-type

管理建模和仿真的文件

管理Boualem Benatallah引用此版本:布阿利姆·贝纳塔拉。管理建模和仿真。约瑟夫-傅立叶大学-格勒诺布尔第一大学,1996年。法语。NNT:电话:00345357HAL ID:电话:00345357https://theses.hal.science/tel-003453572008年12月9日提交HAL是一个多学科的开放存取档案馆,用于存放和传播科学研究论文,无论它们是否被公开。论文可以来自法国或国外的教学和研究机构,也可以来自公共或私人研究中心。L’archive ouverte pluridisciplinaire
recommend-type

实现实时数据湖架构:Kafka与Hive集成

![实现实时数据湖架构:Kafka与Hive集成](https://img-blog.csdnimg.cn/img_convert/10eb2e6972b3b6086286fc64c0b3ee41.jpeg) # 1. 实时数据湖架构概述** 实时数据湖是一种现代数据管理架构,它允许企业以低延迟的方式收集、存储和处理大量数据。与传统数据仓库不同,实时数据湖不依赖于预先定义的模式,而是采用灵活的架构,可以处理各种数据类型和格式。这种架构为企业提供了以下优势: - **实时洞察:**实时数据湖允许企业访问最新的数据,从而做出更明智的决策。 - **数据民主化:**实时数据湖使各种利益相关者都可
recommend-type

SPDK_NVMF_DISCOVERY_NQN是什么 有什么作用

SPDK_NVMF_DISCOVERY_NQN 是 SPDK (Storage Performance Development Kit) 中用于查询 NVMf (Non-Volatile Memory express over Fabrics) 存储设备名称的协议。NVMf 是一种基于网络的存储协议,可用于连接远程非易失性内存存储器。 SPDK_NVMF_DISCOVERY_NQN 的作用是让存储应用程序能够通过 SPDK 查询 NVMf 存储设备的名称,以便能够访问这些存储设备。通过查询 NVMf 存储设备名称,存储应用程序可以获取必要的信息,例如存储设备的IP地址、端口号、名称等,以便能
recommend-type

JSBSim Reference Manual

JSBSim参考手册,其中包含JSBSim简介,JSBSim配置文件xml的编写语法,编程手册以及一些应用实例等。其中有部分内容还没有写完,估计有生之年很难看到完整版了,但是内容还是很有参考价值的。