在VS2017下FFmpeg+SDL编写最简单的视频播放器

1.下载ShiftMediaProject/FFmpeg

2.下载SDL2

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

3.配置include和lib

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

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

6.编写代码

复制代码
#include <stdio.h>
#include <stdlib.h>
#include <iostream>

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);
}
相关推荐
charlie1145141914 分钟前
如何快速在 VS2026 上使用 C++ 模块 — 完整上手指南
开发语言·c++·笔记·学习·现代c++
报错小能手1 小时前
STL_unordered_map
开发语言·c++·哈希算法
历程里程碑1 小时前
C++ 9 stack_queue:数据结构的核心奥秘
java·开发语言·数据结构·c++·windows·笔记·算法
仰泳的熊猫1 小时前
1108 Finding Average
数据结构·c++·算法·pat考试
学而知不足~2 小时前
字幕转码杂记
ffmpeg
AA陈超2 小时前
虚幻引擎5 GAS开发俯视角RPG游戏 P07-18.生成火球术
c++·游戏·ue5·游戏引擎·虚幻
wxin_VXbishe2 小时前
springboot居家养老管理系统-计算机毕业设计源码55953
java·c++·spring boot·python·spring·django·php
ULTRA??2 小时前
归并排序算法实现,kotlin,c++,python
c++·python·kotlin
deng-c-f3 小时前
C/C++内置库函数(5):值/引用传递、移动构造、以及常用的构造技巧
开发语言·c++
qq_310658513 小时前
mediasoup源码走读(十)——producer
服务器·c++·音视频