在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); }

相关内容

热门资讯

aapoker猫腻!aa扑克平... aapoker猫腻!aa扑克平台的机制,(aapoker)竟然真的是有挂(详细辅助必胜教程);1.a...
透视软件(WPK)微扑克辅助器... 透视软件(WPK)微扑克辅助器ios(透视)详细辅助分享教程(真是存在有挂);1、透视软件(WPK)...
透视黑科技(来玩德州app)德... 透视黑科技(来玩德州app)德州ai辅助软件(透视)确实是真的有挂(详细辅助详细教程);一、德州ai...
aapoker猫腻!aapok... aapoker猫腻!aapoker透明挂多久被封,(aaPoker)果然真的是有挂(详细辅助细节揭秘...
透视私人局(微扑克)wpk透明... 透视私人局(微扑克)wpk透明挂(透视)详细辅助辅助教程(切实是有挂)1、在wpk透明挂ai机器人技...
透视透视(手机德州)德州之星有... 透视透视(手机德州)德州之星有外挂(透视)好像存在有挂(详细辅助黑科技教程)1、许多玩家不知道德州之...
aapoker有挂!aapok... aapoker有挂!aapoker ai软件,(AApoker)总是有挂(详细辅助教你教程)1、aa...
辅助透视(wpK)微扑克有辅助... 辅助透视(wpK)微扑克有辅助挂(透视)详细辅助细节方法(总是有挂)1、微扑克有辅助挂系统规律教程、...
透视教程(德州ai)德州之星有... 透视教程(德州ai)德州之星有辅助挂(透视)竟然存在有挂(详细辅助透明教程)1)德州之星有辅助挂辅助...
aapoker透明挂!aa扑克... aapoker透明挂!aa扑克平台,(aApoker)总是真的是有挂(详细辅助必赢方法);运aapo...