ffmpeg学习笔记之SDL视频播放器

本文介绍了如何基于FFMPEG和SDL1.x创建一个简单的视频播放器。首先建立新项目,包含必要的头文件和库,然后解决编译错误,如入口点未定义。接着,添加代码以读取和解码视频流,但在运行时遇到‘couldntopeninputstream’的问题,这通常是因为缺少视频文件。解决方案是确保视频文件位于工作目录下。
摘要由CSDN通过智能技术生成

看了雷神的 100行代码实现最简单的基于FFMPEG+SDL的视频播放器(SDL1.x) 后手痒难耐,决定将里面的代码重新建一个

首先建立一个空项目,新建一个Mysimplest.cpp的文件。在里面写代码

#include <stdio.h>

extern "C"
{
#include "libavcodec/avcodec.h"
#include "libavformat/avformat.h"
#include "libswresample/swresample.h"
#include "SDL/SDL.h"
}


这是头文件引用 。ffmpeg是用C语言写的,所以要用extern "C"来包含

将原项目中的include,lib文件复制到新项目中,还有相关的dll,如图:

同时在属性->链接器->输入->附加依赖项中输入相关的lib文件:avcodec.lib;avformat.lib;avutil.lib;avdevice.lib;avfilter.lib;postproc.lib;swresample.lib;swscale.lib;SDL.lib;SDLmain.lib;

因为文件是在工作路径下的lib文件夹中,所以还要设置附加库目录:

进行编译,如果出现

如图错误:error LNK1561:必须定义入口点

一般来说,如果我们没有定义main函数的话,也会报这个错,加上main函数声明,

#include <stdio.h>

extern "C"
{
#include "libavcodec/avcodec.h"
#include "libavformat/avformat.h"
#include "libswresample/swresample.h"
#include "SDL/SDL.h"
}


int main(int argc, char * argv[]){
	printf("hello world");

	return 0;
}

再次编译,仍然报同样的错,

因为我们还要再设置一个东西

如图,设置为控制台或者窗口,具体根据项目需要,这里设置为控制台

编译通过。

现在继续加代码

在main函数中加进如下代码,运行报错,显示couldn't open input stream.

	AVFormatContext * pFormatCtx;
	int i, videoindex;
	AVCodecContext * pCodecCtx;
	AVCodec * pCodec;
	AVFrame * pFrame;
	AVPacket * packet;
	struct SwsContext * img_convert_ctx;
	int screen_w, screen_h;
	SDL_Surface * screen;
	SDL_VideoInfo * vi;
	SDL_Overlay *bmp;
	SDL_Rect rect;
	FILE * fp_yuv;
	int ret, got_picture;
	char filepath[] = "bigbuckbunny_480x272.h265";
	av_register_all();
	avformat_network_init();
	pFormatCtx = avformat_alloc_context();
	if (avformat_open_input(&pFormatCtx, filepath, NULL, NULL) != 0){
		printf("Couldn't open input stream.\n");
		return -1;
	}

这是因为我们的新工程中没有这个文件,将相关文件(bigbuckbunny_480x272.h265)复制到工作目录下就可以了。

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值