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

相关内容

热门资讯

分享实测!顺欣茶楼辅助视频(辅... 分享实测!顺欣茶楼辅助视频(辅助挂)果然是有挂(有挂解惑辅助技巧)1、全新机制【顺欣茶楼辅助视频ai...
普及知识!微乐小程序有脚本吗(... 普及知识!微乐小程序有脚本吗(辅助挂)切实是真的有挂(有挂秘笈辅助技巧)1、让任何用户在无需微乐小程...
免费测试版!天道辅助器使用教程... 免费测试版!天道辅助器使用教程(辅助挂)本来是有挂(新版有挂辅助教程)1、天道辅助器使用教程脚本辅助...
信息共享!新财神辅助器(辅助挂... 信息共享!新财神辅助器(辅助挂)都是是真的有挂(有挂实锤辅助神器)暗藏猫腻,小编详细说明新财神辅助器...
科技通报!家乡大贰脚本(辅助挂... 科技通报!家乡大贰脚本(辅助挂)其实是真的有挂(有挂教学辅助攻略)1、让任何用户在无需家乡大贰脚本安...
玩家必看教程!潮友会app下载... 玩家必看教程!潮友会app下载安卓(辅助挂)一贯是有挂(有挂辅助辅助教程)1、首先打开潮友会app下...
详细说明!茶馆游戏辅助(辅助挂... 详细说明!茶馆游戏辅助(辅助挂)切实是真的有挂(详细教程辅助攻略)1、详细说明!茶馆游戏辅助(辅助挂...
必备科技!菠萝神辅助器app(... 必备科技!菠萝神辅助器app(辅助挂)一直是真的有挂(了解有挂辅助攻略)1、超多福利:超高返利,海量...
我来分享!威信茶馆解码器(辅助... 我来分享!威信茶馆解码器(辅助挂)确实是有挂(存在有挂辅助器)1、许多玩家不知道威信茶馆解码器辅助怎...
教程攻略!吉祥填大坑小程序辅助... 教程攻略!吉祥填大坑小程序辅助(辅助挂)确实是真的有挂(有挂存在辅助方法)1、下载好吉祥填大坑小程序...