FFMPEG解码+SDL2播放视频

FFMPEG解码+SDL2播放

本项目通过FFmpeg对音视频进行解码,得到原始数据格式AVFrame,再通过SDL2在窗口渲染图像数据进行播放。

配置环境:vs 2022 + FFmpeg5.0 + SDL2 2.28

项目源码:https://github.com/say-Hai/FFmpeg-videoPlayDemo/tree/PlaySDL2

附录1:FFMPEG解码调用API流程

附录2:SDL2库播放调用API流程

关键代码:

c++ 复制代码
///初始化ffmpeg相关结构体
int init_ffmpeg(FFmpeg_V_Param* p_ffmpeg_param, const char* filePath)
{
	//AVFormatContext初始化
	p_ffmpeg_param->pFormatCtx = avformat_alloc_context();
	//解码器
	const AVCodec* pcodec = NULL;
	//1.初始化网络
	//av_register_all()已被弃用
	avformat_network_init();
	//2.open输入流
	int ret = avformat_open_input(&(p_ffmpeg_param->pFormatCtx), filePath, NULL, NULL);
	if (ret < 0)
	{
		output_log(LOG_ERROR, "avformat_open_input error");
		return -1;
	}
	//3.读取媒体的数据包以获取具体的流信息,如媒体存入的编码格式。
	ret = avformat_find_stream_info(p_ffmpeg_param->pFormatCtx, NULL);
	if (ret < 0)
	{
		output_log(LOG_ERROR, "avformat_find_stream_info error");
		return -1;
	}
	//4.遍历 FFmpeg 中 AVFormatContext 的所有媒体流
	//get video pCodecParms, codec and frame rate
	//nb_streams 表示多媒体文件或流中包含的媒体流的数量
	for (int i = 0; i < p_ffmpeg_param->pFormatCtx->nb_streams; i++)
	{
		//4.1AVStream:存储音频流或视频流的结构体
		AVStream* pStream = p_ffmpeg_param->pFormatCtx->streams[i];
		if (pStream->codecpar->codec_type == AVMEDIA_TYPE_VIDEO)
		{
			//4.2查找匹配解码器ID的已注的解码器
			pcodec = avcodec_find_decoder(pStream->codecpar->codec_id);
			//4.3 分配并初始化 AVCodecContext 结构体(参数为编解码器)
			p_ffmpeg_param->pCodecCtx = avcodec_alloc_context3(pcodec);
			avcodec_parameters_to_context(p_ffmpeg_param->pCodecCtx, pStream->codecpar);
			//4.4计算视频的帧率
			g_frame_rate = pStream->avg_frame_rate.num / pStream->avg_frame_rate.den;
			//流的索引
			p_ffmpeg_param->video_index = i;
		}
	}
	if (!p_ffmpeg_param->pCodecCtx)
	{
		output_log(LOG_ERROR, "could not find video codecCtx");
		return -1;
	}
	//5 通过给定的AVCodec来初始化一个视音频编解码器的 AVCodecContext
	ret = avcodec_open2(p_ffmpeg_param->pCodecCtx, pcodec, NULL);
	if (ret < 0)
	{
		output_log(LOG_ERROR, "avcodec_open2 error");
		return -1;
	}
	//6初始化一个缩放上下文 (SwsContext),以便进行视频像素格式的转换或尺寸缩放。(这里是转成YUV420P)
	p_ffmpeg_param->pSwsCtx = sws_getContext(
		p_ffmpeg_param->pCodecCtx->width, p_ffmpeg_param->pCodecCtx->height, p_ffmpeg_param->pCodecCtx->pix_fmt,
		p_ffmpeg_param->pCodecCtx->width, p_ffmpeg_param->pCodecCtx->height, AV_PIX_FMT_YUV420P,
		SWS_BICUBIC, NULL, NULL, NULL);

	/*
	 *打印将媒体文件的格式和流信息
	av_dump_format(p_ffmpeg_param->pFormatCtx, p_ffmpeg_param->video_index, filePath, 0);
	*/
	return 0;
	//ffmpeg初始化全部完成
}
相关推荐
听风吹等浪起1 小时前
第19章:基于efficientNet实现的视频内容识别系统
人工智能·深度学习·音视频
科技小E1 小时前
视频设备轨迹回放平台EasyCVR打造水库大坝智慧安防视频监控智能分析方案
大数据·网络·人工智能·音视频·安防监控
李煜鑫1 小时前
关于视频的一些算法内容,不包含代码等
算法·音视频·语音识别
布拉德很帅2 小时前
android系统使用FFmpeng集成OpenSL音频录制和播放
音视频
硅谷秋水2 小时前
MAPLE:编码从自我为中心的视频中学习的灵巧机器人操作先验
人工智能·机器学习·计算机视觉·机器人·音视频
TSINGSEE5 小时前
跨平台嵌入式音视频开发指南:EasyRTC音视频通话的多场景适配与AI扩展能力
人工智能·音视频·webrtc·智能家居
科技小E14 小时前
5G时代,视频分析设备平台EasyCVR实现通信基站远程安全便捷管控
大数据·网络·人工智能·音视频·安防监控
小白教程16 小时前
如何处理Python爬取视频时的反爬机制?
开发语言·python·音视频·python爬虫
Luke Ewin16 小时前
一个基于OpenAI Whisper开发的音视频字幕文件生成工具
人工智能·whisper·音视频·语音识别·asr·语音转写·视频字幕生成
WDeLiang19 小时前
音频基础概念
音视频