看了雷神的 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)复制到工作目录下就可以了。