阿里巴巴怎么建设网站,遵义网约车平台,建设银行东营分行网站,聊城做企业网站FFmpeg入门#xff1a;最简单的音频播放器
欢迎大家来到FFmpeg入门的第二章#xff0c;今天只做一个最简单的FFmpeg音频播放器#xff1b;同样#xff0c;话不多说#xff0c;先上流程图
流程图 以上流程和视频播放器的解码过程基本上是一致的#xff1b; 不同点在于 S…FFmpeg入门最简单的音频播放器
欢迎大家来到FFmpeg入门的第二章今天只做一个最简单的FFmpeg音频播放器同样话不多说先上流程图
流程图 以上流程和视频播放器的解码过程基本上是一致的 不同点在于 SDL的渲染方式。下面我会重点说一下这个部分
SDL音频渲染
音频渲染的方式和视频不太一样的我们对于音频的播放速度其实是根据采样率定义的音频的采样率视频的帧率在初始化的时候SDL播放器就指定了这个参数因此不需要向视频播放器那样手动去延迟来保持帧率。
如下是SDL音频播放器的初始化。
SDL_AudioSpec wanted_spec;
wanted_spec.freq out_sample_rate; // 采样率
wanted_spec.format AUDIO_S16SYS; // 采样格式 16bit
wanted_spec.channels out_channels; // 通道数
wanted_spec.silence 0;
wanted_spec.samples out_nb_samples; // 单帧处理的采样点
wanted_spec.callback fill_audio; // 回调函数
wanted_spec.userdata pCodecCtx; // 回调函数的参数其原理就是SDL音频播放器会不断从其缓冲区取出数据读取播放因此我们只需要不断向其缓冲区中写入数据即可。详见代码
// 设置读取的音频数据
audio_info.audio_len out_buffer_size;
audio_info.audio_pos (Uint8 *) out_buffer;但是有个点注意一下就是在写入SDL播放器的缓冲区之前我们要确保之前的数据已经被SDL播放器消化完了不然会导致音频数据被覆盖而没有读出来详见代码
// 等待SDL播放完成
while(audio_info.audio_len 0)SDL_Delay(0.5);源代码
接下来看看源代码吧 tutorial03.h
//
// tutorial03.h
// learning
//
// Created by chenhuaiyi on 2025/2/16.
//#ifndef tutorial03_h
#define tutorial03_h/**头文件*/
#include stdio.h
// ffmpeg
#include libavcodec/avcodec.h
#include libswresample/swresample.h
#include libavformat/avformat.h
#include libswscale/swscale.h
#include libavutil/imgutils.h
#include libavutil/time.h
// SDL
#include SDL.h
#include SDL_thread.h/**宏定义*/
#define USE_SDL 1/**数据类型定义*/
typedef struct {Uint32 audio_len;Uint8 *audio_pos;
} AudioInfo;/**全局变量*/
extern AudioInfo audio_info;#endif /* tutorial03_h */
tutorial03.c
/**
// tutorial03.c
// learning
//
// Created by chenhuaiyi on 2025/2/16.*/#include tutorial03.hAudioInfo audio_info;/* udata: 传入的参数* stream: SDL音频缓冲区* len: SDL音频缓冲区大小* 回调函数*/
void fill_audio(void *udata, Uint8 *stream, int len){SDL_memset(stream, 0, len); // 必须重置不然全是电音!!!if(audio_info.audio_len0){ // 有音频数据时才调用return;}len (lenaudio_info.audio_len ? audio_info.audio_len : len); // 最多填充缓冲区大小的数据SDL_MixAudio(stream, audio_info.audio_pos, len, SDL_MIX_MAXVOLUME);audio_info.audio_pos len;audio_info.audio_len - len;
}int main(int argc, char* argv[])
{AVFormatContext* pFormatCtx avformat_alloc_context();int i, audioStream;AVCodecContext* pCodecCtx avcodec_alloc_context3(NULL);const AVCodec* pCodec;AVPacket packet;if(argc 2) {fprintf(stderr, Usage: test file\n);exit(1);}avformat_network_init();// 1. 打开视频文件获取格式上下文if(avformat_open_input(pFormatCtx, argv[1], NULL, NULL)!0){printf(Couldnt open input stream.\n);return -1;}// 2. 对文件探测流信息if(avformat_find_stream_info(pFormatCtx, NULL) 0){printf(Couldnt find stream information.\n);return -1;}// 打印信息av_dump_format(pFormatCtx, 0, argv[1], 0);// 3. 找到对应的音频流audioStream-1;for(i0; i pFormatCtx-nb_streams; i) {if(pFormatCtx-streams[i]-codecpar-codec_typeAVMEDIA_TYPE_AUDIO){audioStreami;break;}}if(audioStream-1){printf(Didnt find a audio stream.\n);return -1;}// 4. 将音频流编码参数写入上下文AVCodecParameters* pCodecParam pFormatCtx-streams[audioStream]-codecpar;avcodec_parameters_to_context(pCodecCtx, pCodecParam);avcodec_parameters_free(pCodecParam);// 5. 查找流的编码器pCodec avcodec_find_decoder(pCodecCtx-codec_id);if(pCodecNULL){printf(Codec not found.\n);return -1;}// 6. 打开流的编解码器if(avcodec_open2(pCodecCtx, pCodec,NULL)0){printf(Could not open codec.\n);return -1;}// 输出用到的信息AVChannelLayout out_channel_layout AV_CHANNEL_LAYOUT_STEREO; // 通道 layoutint out_nb_samples pCodecCtx-frame_size; // 编解码器每个帧需要处理或者输出的采样点的大小 AAC:1024 MP3:1152enum AVSampleFormat out_sample_fmt AV_SAMPLE_FMT_S16; // 采样格式int out_sample_rate 44100; // 采样率int out_channels out_channel_layout.nb_channels; // 通道数// 获取需要使用的缓冲区大小 - 通道数单通道样本数位深 1024(单帧处理的采样点)*2(双通道)*2(16bit对应2字节)int out_buffer_size av_samples_get_buffer_size(NULL, out_channels,out_nb_samples, out_sample_fmt, 1);// 分配缓冲区空间uint8_t* out_buffer NULL;av_samples_alloc(out_buffer, NULL, out_channels, out_nb_samples, out_sample_fmt, 1);AVFrame* pFrame av_frame_alloc();// SDL 初始化
#if USE_SDLif(SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO | SDL_INIT_TIMER)) {printf( Could not initialize SDL - %s\n, SDL_GetError());return -1;}SDL_AudioSpec wanted_spec;wanted_spec.freq out_sample_rate; // 采样率wanted_spec.format AUDIO_S16SYS; // 采样格式 16bitwanted_spec.channels out_channels; // 通道数wanted_spec.silence 0;wanted_spec.samples out_nb_samples; // 单帧处理的采样点wanted_spec.callback fill_audio; // 回调函数wanted_spec.userdata pCodecCtx; // 回调函数的参数// 打开音频播放器if (SDL_OpenAudio(wanted_spec, NULL)0){printf(cant open audio.\n);return -1;}#endifint ret 0;int index 0;// 上下文格式转换SwrContext *swr_ctx NULL;swr_alloc_set_opts2(swr_ctx,out_channel_layout, // 输出layoutout_sample_fmt, // 输出格式out_sample_rate, // 输出采样率pCodecCtx-ch_layout, // 输入layoutpCodecCtx-sample_fmt, // 输入格式pCodecCtx-sample_rate, // 输入采样率0, NULL);swr_init(swr_ctx);// 开始播放SDL_PauseAudio(0);AVRational time_base pFormatCtx-streams[audioStream]-time_base;int64_t av_start_time av_gettime(); // 播放开始时间戳// 循环1: 从文件中读取packetwhile(av_read_frame(pFormatCtx, packet)0){if(packet.stream_indexaudioStream){// 将packet写入编解码器ret avcodec_send_packet(pCodecCtx, packet);if ( ret 0 ) {printf(send packet error\n);return -1;}while (!avcodec_receive_frame(pCodecCtx, pFrame)) {// 格式转化swr_convert(swr_ctx, out_buffer, out_buffer_size,(const uint8_t **)pFrame-data, pFrame-nb_samples);index;printf(第%d帧 | pts:%lld | 帧大小(采样点):%d | 实际播放点%.2fs | 预期播放点%.2fs\n,index,packet.pts,packet.size,(double)(av_gettime() - av_start_time)/AV_TIME_BASE,pFrame-pts * av_q2d(time_base));#if USE_SDL// 设置读取的音频数据audio_info.audio_len out_buffer_size;audio_info.audio_pos (Uint8 *) out_buffer;// 等待SDL播放完成while(audio_info.audio_len 0)SDL_Delay(0.5);
#endif}av_packet_unref(packet);}}// 打印参数printf(格式: %s\n, pFormatCtx-iformat-name);printf(时长: %lld us\n, pFormatCtx-duration);printf(音频持续时长为 %.2f音频帧总数为 %d\n, (double)(av_gettime()-av_start_time)/AV_TIME_BASE, index);printf(码率: %lld\n, pFormatCtx-bit_rate);printf(编码器: %s (%s)\n, pCodecCtx-codec-long_name, avcodec_get_name(pCodecCtx-codec_id));printf(通道数: %d\n, pCodecCtx-ch_layout.nb_channels);printf(采样率: %d \n, pCodecCtx-sample_rate);printf(单通道每帧的采样点数目: %d\n, pCodecCtx-frame_size);printf(pts单位(ms*1000): %.2f\n, av_q2d(pFormatCtx-streams[audioStream]-time_base) * AV_TIME_BASE);// 释放空间swr_free(swr_ctx);
#if USE_SDLSDL_CloseAudio();SDL_Quit();
#endifav_free(out_buffer);av_free(pFrame);avcodec_free_context(pCodecCtx);avformat_close_input(pFormatCtx);return 0;
}