在VS2017下FFmpeg+SDL编写最简单的视频播放器
创始人
2025-01-10 18:07:48
0

1.下载ShiftMediaProject/FFmpeg

2.下载SDL2

3.新建VC++控制台应用

3.配置include和lib

 4.把FFmpeg和SDL的dll 复制到工程Debug目录下,并设置调试命令

5.复制一下mp4视频到工程Debug目录下(复制一份到*.vcxproj同一目录,用于调试)

6.编写代码

#include  #include  #include   extern "C" { #include "libavcodec\avcodec.h" #include "libavformat\avformat.h" #include "libavutil\avutil.h" #include "SDL.h" }  SDL_Window *window; SDL_Texture* texture; SDL_Renderer* renderer; SDL_Rect rect; int width; int height;  void display(AVFrame* frame); void iniSdl();  #undef main int main() { 	int ret = -1; 	AVFormatContext* ctx = avformat_alloc_context();  	ret = avformat_open_input(&ctx, "my.mp4", NULL, NULL); 	ret = avformat_find_stream_info(ctx, NULL);  	int videoindex = -1; 	for (int i = 0; i < ctx->nb_streams; i++) 	{ 		if (ctx->streams[i]->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) 		{ 			videoindex = i; 			break; 		} 	}  	const AVCodec* codec = avcodec_find_decoder(ctx->streams[videoindex]->codecpar->codec_id); 	AVCodecContext* avcc = avcodec_alloc_context3(codec); 	ret = avcodec_parameters_to_context(avcc, ctx->streams[videoindex]->codecpar); 	ret = avcodec_open2(avcc, codec, NULL);  	width = avcc->width; 	height = avcc->height; 	rect.x = 0; 	rect.y = 0; 	rect.h = height; 	rect.w = width; 	iniSdl();  	AVFrame* frame = av_frame_alloc(); 	AVPacket* packet = av_packet_alloc(); 	while (true) 	{ 		ret = av_read_frame(ctx, packet); 		if (ret >=0) 		{ 			if (packet->stream_index == videoindex) 			{ 				ret = avcodec_send_packet(avcc, packet); 				while (ret >= 0) 				{ 					ret = avcodec_receive_frame(avcc, frame); 					if (ret >= 0) 					{ 						display(frame); 						av_frame_unref(frame); 					} 					else 					{ 						av_frame_unref(frame); 						break; 					} 				} 			}			 		} 		else 		{ 			av_packet_unref(packet); 			break; 		} 	}  	SDL_DestroyWindow(window); 	std::cout << "finish\n"; }  void display(AVFrame* frame) { 	SDL_UpdateYUVTexture(texture, &rect, frame->data[0], frame->linesize[0], frame->data[1], frame->linesize[1], frame->data[2], frame->linesize[2]); 	SDL_RenderClear(renderer); 	SDL_RenderCopy(renderer, texture, NULL, NULL); 	SDL_RenderPresent(renderer); }  void iniSdl() { 	SDL_Init(SDL_INIT_VIDEO); 	window = SDL_CreateWindow("test", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, width, height, SDL_WINDOW_RESIZABLE); 	renderer = SDL_CreateRenderer(window, -1, 0); 	texture = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_IYUV, SDL_TEXTUREACCESS_STREAMING, width, height); }

相关内容

热门资讯

4分钟辅助!天天手游辅助工具,... 4分钟辅助!天天手游辅助工具,好像存在有辅助脚本(有挂秘诀)1、起透看视 天天手游辅助工具辅助软件价...
7分钟辅助!微乐小程序自建房辅... 7分钟辅助!微乐小程序自建房辅助,都是有辅助方法(有人有挂)1、玩家可以在微乐小程序自建房辅助线上大...
2分钟辅助!约局吧辅助器,本来... 2分钟辅助!约局吧辅助器,本来真的有辅助插件(的确有挂)1、约局吧辅助器破解器简单,约局吧辅助器机器...
三分钟辅助!微乐自建房辅助工具... 三分钟辅助!微乐自建房辅助工具,确实有辅助挂(存在有挂)1、很好的工具软件,可以解锁游戏的微乐自建房...
8分钟辅助!巴郎新疆开挂,一直... 8分钟辅助!巴郎新疆开挂,一直有辅助工具(有挂详情)1、巴郎新疆开挂透视辅助软件激活码多个强度级别选...
五分钟辅助!温州茶苑手机辅助器... 五分钟辅助!温州茶苑手机辅助器,好像真的有辅助软件(有挂技巧)1、每一步都需要思考,不同水平的挑战温...
两分钟辅助!白金岛手游的作弊码... 两分钟辅助!白金岛手游的作弊码,其实真的是有辅助挂(有挂细节)1、白金岛手游的作弊码免费辅助多个强度...
八分钟辅助!极速暗宝辅助,一贯... 八分钟辅助!极速暗宝辅助,一贯真的有辅助技巧(有挂神器)1、许多玩家不知道极速暗宝辅助辅助怎么退出观...
一分钟辅助!创乐源辅助软件,果... 一分钟辅助!创乐源辅助软件,果然有辅助工具(有挂讲解)1、一分钟辅助!创乐源辅助软件,果然有辅助工具...
8分钟辅助!微乐自建房辅助多少... 8分钟辅助!微乐自建房辅助多少钱一个月,真是真的是有辅助工具(有挂讲解)小薇(辅助器软件下载)致您一...