SDL+ffmpeg 最新可直接运行 音频播放器

播放视频文件内的音频需要进行重采样操作,代码中会引入重采样结构体SwrContext。

重采样结构体能够改变原先音频的采样率、声道数等参数,令各种音频能够按照我们设定的参数进行输出。这样做的原因是不同视频文件内的音频参数通常区别较大,如果分别处理工作量太大,不如将其统一成相同的格式。

重采样结构体,就是改变音频的采样率、sample format、声道数等参数,使之按照我们期望的参数输出,当然是原有的音频参数不满足我们的需求,比如在FFMPEG解码音频的时候,不同的音源有不同的格式,采样率等,在解码后的数据中的这些参数也会不一致,如果我们接下来需要使用解码后的音频数据做其他操作,而这些参数的不一致导致会有很多额外工作,此时直接对其进行重采样,获取我们制定的音频参数,这样就会方便很多

#include <stdio.h>
#include <tchar.h>
#include <iostream>
 
 
extern "C"
{
#include "SDL.h" //头文件不仅要在项目中鼠标点击配置,在代码中也要引入
#include "include/libavformat/avformat.h"
#include "include/libavformat/avformat.h"
#include "include/libswscale/swscale.h"
#include "include/libavdevice/avdevice.h"
#include "include/libavcodec/avcodec.h"
#include "include/libswresample/swresample.h"
};
using namespace std;
 
#define MAX_AUDIO_FRAME_SIZE 19200
 
static  Uint8  *audio_chunk;  //音频块
static  Uint32  audio_len;    //音频剩下的长度
static  Uint8  *audio_pos;    //音频当前的位置
 
//回调函数的作用是填充音频缓冲区,当音频设备需要更多数据的时候会调用该回调函数
//userdata一般不使用,stream是音频缓冲区,len是音频缓冲区大小
void  fill_audio(void *udata, Uint8 *stream, int len) {
	//将stream置0,SDL2必须有的操作
	SDL_memset(stream, 0, len);
	if (audio_len == 0)		//如果没有剩余数据
		return;
	len = (len > audio_len ? audio_len : len);	//尽可能得到更多的数据
	//混音处理
	SDL_MixAudio(stream, audio_pos, len, SDL_MIX_MAXVOLUME);
	audio_pos += len;   //更新音频当前的位置
	audio_len -= len;   //更新音频剩下的长度
}
 
#undef main
int main(int argc, char* argv[])
{
	const char *audio_path = "ds.mov";//视频路径
 
	//初始化ffmpeg组件
	AVFormatContext *pFormatCtx = nullptr;
	int i, audioStream = -1,videoStream = -1;
	AVCodecParameters *pCodecParameters = nullptr;
	AVCodecContext *pCodecCtx = nullptr;
	const AVCodec *pCodec = nullptr;
	AVFrame *pFrame = nullptr;
	AVPacket *packet;
	uint8_t *out_buffer;
 
	int64_t in_channel_layout;
 
	//初始化重采样组件
	struct SwrContext *au_convert_ctx;
 
	//打开音频文件 ffmpeg
	if (avformat_open_input(&pFormatCtx, audio_path, nullptr, nullptr) != 0) {
		cout << "can not open the audio!" << endl;
		return -1;
	}
 
	//寻找视频流与音频流 ffmpeg
	audioStream = av_find_best_stream(pFormatCtx, AVMEDIA_TYPE_AUDIO, -1, -1, nullptr, 0);
	if (audioStream == -1) {
		cout << "can not find audio stream!" << endl;
		return -1;
	}
	videoStream = av_find_best_stream(pFormatCtx, AVMEDIA_TYPE_VIDEO, -1, -1, nullptr, 0);
	if (videoStream == -1) {
		cout << "can not open a video stream" << endl;
		return -1;
	}
 
	//寻找解码器 ffmpeg
	pCodecParameters = pFormatCtx->streams[audioStream]->codecpar;
	pCodec = avcodec_find_decoder(pFormatCtx->streams[audioStream]->codecpar->codec_id);
	if (pCodec == nullptr) {
		cout << "can not find a codec" << endl;
		return -1;
	}
 
	//加载解码器参数 ffmpeg
	pCodecCtx = avcodec_alloc_context3(pCodec);
	if (avcodec_parameters_to_context(pCodecCtx, pCodecParameters) != 0) {
		cout << "can not copy codec context" << endl;
		return -1;
	}
 
	//启动解码器 ffmpeg
	if (avcodec_open2(pCodecCtx, pCodec, NULL) < 0) {
		cout << "can not open the decoder!" << endl;
		return -1;
	}
 
	//初始化packet与pframe ffmpeg
	packet = (AVPacket*)av_malloc(sizeof(AVPacket));
	av_packet_alloc();
	pFrame = av_frame_alloc();
 
	//音频参数初始化
	uint64_t out_channel_layout = AV_CH_LAYOUT_STEREO;//输出声道
	int out_nb_samples = 1024; //音频缓冲区的采样个数
	enum AVSampleFormat out_sample_fmt = AV_SAMPLE_FMT_S16;//输出格式S16
	int out_sample_rate = 44100; //pcm采样率
	int out_channels = av_get_channel_layout_nb_channels(out_channel_layout);//声道数量
 
	int out_buffer_size = av_samples_get_buffer_size(NULL, out_channels, out_nb_samples, out_sample_fmt, 1);//计算音频缓冲区大小
	out_buffer = (uint8_t *)av_malloc(MAX_AUDIO_FRAME_SIZE * 2);//初始化音频缓冲区大小
 
	//初始化SDL
	if (SDL_Init(SDL_INIT_AUDIO | SDL_INIT_TIMER)) {
		cout << "can not open the SDL!" << endl;
		return -1;
	}
 
	//设置音频相关参数
	SDL_AudioSpec spec;
	spec.freq = out_sample_rate;  //pcm采样频率
	spec.format = AUDIO_S16SYS;   //音频格式,16bit
	spec.channels = out_channels; //声道数量
	spec.silence = 0;             //设置静音的值   
	spec.samples = out_nb_samples;//音频缓冲区的采样个数
	spec.callback = fill_audio;   //回调函数
	spec.userdata = pCodecCtx;    //回调函数的数据来自解码器
 
	//初始化SDL
	if (SDL_OpenAudio(&spec, nullptr) < 0) {
		cout << "can not open audio" << endl;
		return -1;
	}
 
	//音频重采样操作
	in_channel_layout = av_get_default_channel_layout(pCodecCtx->channels);
	cout << "in_channel_layout: " << in_channel_layout << endl;
	au_convert_ctx = swr_alloc(); //初始化重采样结构体
	au_convert_ctx = swr_alloc_set_opts(au_convert_ctx, out_channel_layout, out_sample_fmt, out_sample_rate, in_channel_layout, pCodecCtx->sample_fmt, pCodecCtx->sample_rate, 0, NULL);//重采样结构体赋值
	swr_init(au_convert_ctx);//将重采样结构体参数加载
 
	SDL_PauseAudio(0);
 
	//循环读取packet
	while (av_read_frame(pFormatCtx, packet) >= 0) {
		if (packet->stream_index == audioStream) {
			//进行解码操作
			avcodec_send_packet(pCodecCtx, packet);
			while (avcodec_receive_frame(pCodecCtx,pFrame) == 0)
			{
				//将输入的音频按照定义的参数进行转换,并输出
				swr_convert(au_convert_ctx, &out_buffer, MAX_AUDIO_FRAME_SIZE, (const uint8_t **)pFrame->data, pFrame->nb_samples);
			}
 
			audio_chunk = (Uint8*)out_buffer; //设置音频缓冲区
			audio_len = out_buffer_size;      //更新音频长度
			audio_pos = audio_chunk;          //更新音频位置
			while (audio_len > 0)
			{
				SDL_Delay(1);//延迟播放
			}
		}
		av_packet_unref(packet);//清空packet内容
	}
	swr_free(&au_convert_ctx);//清空重采样结构体内容
	//回收ffmpeg组件
	if (pFrame) {
		av_frame_free(&pFrame);
		pFrame = nullptr;
	}
 
	if (pCodecCtx) {
		avcodec_close(pCodecCtx);
		pCodecCtx = nullptr;
		pCodec = nullptr;
	}
 
	if (pFormatCtx) {
		avformat_close_input(&pFormatCtx);
		pFormatCtx = nullptr;
	}
	//关闭SDL
	SDL_Quit();
	cout << "succeed!" << endl;
	return 0;
}
 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值