VS2015配置ffmpeg到运行成功 雷霄骅大佬 的基于FFMPEG+SDL2.0的视频播放器

由于项目需要,开始入门FFMPEG,记录一下入门中遇到的问题和解决的方法。

一、VS2015配置FFMPEG

配置可以参考这篇文章,简单的三步。https://blog.youkuaiyun.com/hustlx/article/details/51014307

配置注意不要配置错平台就行了,即下面两个图要对应,否则会出现一堆找不到文件的错误。

 最后一步的这段代码,插入stdafx.h中就行了,或者就放在main函数前面

extern "C"
{
#include "include\libavcodec\avcodec.h"
#include "include\libavformat\avformat.h"
#include "include\libavutil\channel_layout.h"
#include "include\libavutil\common.h"
#include "include\libavutil\imgutils.h"
#include "include\libswscale\swscale.h" 
#include "include\libavutil\imgutils.h"    
#include "include\libavutil\opt.h"       
#include "include\libavutil\mathematics.h"    
#include "include\libavutil\samplefmt.h" 
};
#pragma comment(lib, "avcodec.lib")
#pragma comment(lib, "avformat.lib")
#pragma comment(lib, "avdevice.lib")
#pragma comment(lib, "avfilter.lib")
#pragma comment(lib, "avutil.lib")
#pragma comment(lib, "postproc.lib")
#pragma comment(lib, "swresample.lib")
#pragma comment(lib, "swscale.lib")

 配置完成后运行一下下面这段代码,可以检测是否配置成功

int main( int argc , char* argv [])
{ 
printf ( " % s", avcodec_configuration ()); 
system("pause");
return 0; 
} 

 显示这一堆东西的话,应该就是成功了:--disable-static --enable-shared --enable-gpl --enable-version3 --disable-w32threads --enable-avisynth --enable-bzlib --enable-fontconfig --enable-frei0r --enable-gnutls --enable-iconv --enable-libass --enable-libbluray --enable-libcaca --enable-libfreetype --enable-libgsm --enable-libilbc --enable-libmodplug --enable-libmp3lame --enable-libopencore-amrnb --enable-libopencore-amrwb --enable-libopenjpeg --enable-libopus --enable-librtmp --enable-libschroedinger --enable-libsoxr --enable-libspeex --enable-libtheora --enable-libtwolame --enable-libvidstab --enable-libvo-aacenc --enable-libvo-amrwbenc --enable-libvorbis --enable-libvpx --enable-libwavpack --enable-libx264 --enable-libx265 --enable-libxavs --enable-libxvid --enable-decklink --enable-zlib  

二、配置SDL

步骤基本上和上面一样,到SDL官网https://www.libsdl.org/下载SDL2-devel-2.0.10-VC.zip就行了

 然后配置方式和上面基本相同,也是把include和lib文件夹里的东西copy到工程目录对应的目录就行了。

需要注意的一点就是SDL的lib文件里的SDL2.ddl文件不要放到工程的lib里,直接放到工程目录就行了,否则会报错说找不到SDL2.ddl。

三、运行雷霄骅大佬的博客里面的代码

链接https://blog.youkuaiyun.com/leixiaohua1020/article/details/38868499

下面记录一下代码中需要修改的地方:

1.出现avcodec_decode_video2()、av_free_packet()、av_register_all()等函数声明已否决

参考这篇文章可以解决 https://blog.youkuaiyun.com/qq_24854861/article/details/79757120

然后还有一个av_register_all()函数声明被否决,参考https://www.jianshu.com/p/ebb219ec1c0f这篇文章,可以发现新版本的FFMPEG已经不需要在最前面调用这个函数了。

2.出现LNK1561:必须定义入口点

明明有main函数,还提示这个错误。是因为在SDL_main.h中

解决方法,在链接》高级》入口点输入“WinMainCRTStartup”就行了。

3. 运行有问题,白屏或者绿屏

由于第一个问题,换掉了一些函数,通过查看这些函数的定义,发现返回值的意义是有变化的,比如原来的是返回非0成功,但是改过后是返回0成功,所以做如下修改

 

改过后就能够使用了。

下面贴出修改过的代码,希望能够帮助到刚入门的同学。最后再次感谢雷霄骅大佬,大家可以去关注一波,里面非常多的干货。

#include "stdafx.h"

#define OUTPUT_YUV420P 0

// 本程序实现了视频文件的解码和显示(支持HEVC,H.264,MPEG2等)。

int main(int argc, char *argv[])
{
	AVFormatContext	*pFormatCtx;
	int				i, videoindex;
	AVCodecContext	*pCodecCtx;
	AVCodec			*pCodec;
	AVFrame	*pFrame, *pFrameYUV;
	unsigned char *out_buffer;
	AVPacket *packet;
	int y_size;
	int ret, got_picture;
	struct SwsContext *img_convert_ctx;

	char filepath[] = "video_h265.h265";
	//SDL---------------------------
	int screen_w = 0, screen_h = 0;
	SDL_Window  *screen;
	SDL_Renderer* sdlRenderer;
	SDL_Texture* sdlTexture;
	SDL_Rect sdlRect;

	FILE *fp_yuv;

	//av_register_all();
	avformat_network_init();
	pFormatCtx = avformat_alloc_context();  //申请AVFormatContext类型变量并初始化默认参数
	//打开一个文件并解析。可解析的内容包括:视频流、音频流、视频流参数、音频流参数、视频帧索引
	if (avformat_open_input(&pFormatCtx, filepath, NULL, NULL) != 0) {
		printf("Couldn't open input stream.\n");
		return -1;
	}
	if (avformat_find_stream_info(pFormatCtx, NULL)<0) {
		printf("Couldn't find stream information.\n");
		return -1;
	}
	videoindex = -1;
	//遍历文件中的视频流,应该是看一下传入的视频是否能够被解析
	for (i = 0; i<pFormatCtx->nb_streams; i++)
		//if (pFormatCtx->streams[i]->codec->codec_type == AVMEDIA_TYPE_VIDEO) {
		if (pFormatCtx->streams[i]->codecpar->codec_type == AVMEDIA_TYPE_VIDEO){
			videoindex = i;
			break;
		}
	if (videoindex == -1) {
		printf("Didn't find a video stream.\n");
		return -1;
	}

	//pCodecCtx = pFormatCtx->streams[videoindex]->codec;
	pCodecCtx = avcodec_alloc_context3(NULL);  //申请AVCodecContext类型变量并初始化默认参数
	avcodec_parameters_to_context(pCodecCtx, pFormatCtx->streams[videoindex]->codecpar);
	//printf("%u",pCodecCtx->codec_id);//AV_CODEC_ID_HEVC
	
	pCodec = avcodec_find_decoder(pCodecCtx->codec_id);//获取解码器
	if (pCodec == NULL) {
		printf("Codec not found.\n");
		return -1;
	}
	if (avcodec_open2(pCodecCtx, pCodec, NULL)<0) {
		printf("Could not open codec.\n");
		return -1;
	}

	pFrame = av_frame_alloc();				//AVFrame存放从AVPacket中解码出来的原始数据
	pFrameYUV = av_frame_alloc();
	out_buffer = (unsigned char *)av_malloc(av_image_get_buffer_size(AV_PIX_FMT_YUV420P, pCodecCtx->width, pCodecCtx->height, 1));
	av_image_fill_arrays(pFrameYUV->data, pFrameYUV->linesize, out_buffer,
		AV_PIX_FMT_YUV420P, pCodecCtx->width, pCodecCtx->height, 1);

	packet = (AVPacket *)av_malloc(sizeof(AVPacket));
	//Output Info-----------------------------
	printf("--------------- File Information ----------------\n");
	av_dump_format(pFormatCtx, 0, filepath, 0);
	printf("-------------------------------------------------\n");
	img_convert_ctx = sws_getContext(pCodecCtx->width, pCodecCtx->height, pCodecCtx->pix_fmt,
		pCodecCtx->width/2, pCodecCtx->height/2, AV_PIX_FMT_YUV420P, SWS_BICUBIC, NULL, NULL, NULL);

#if OUTPUT_YUV420P 
	fp_yuv = fopen("output.yuv", "wb+");
#endif  

	if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO | SDL_INIT_TIMER)) {
		printf("Could not initialize SDL - %s\n", SDL_GetError());
		return -1;
	}

	screen_w = pCodecCtx->width;
	screen_h = pCodecCtx->height;
	//SDL 2.0 Support for multiple windows
	screen = SDL_CreateWindow("Simplest ffmpeg player's Window", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED,
		screen_w/2, screen_h/2,
		SDL_WINDOW_OPENGL);

	if (!screen) {
		printf("SDL: could not create window - exiting:%s\n", SDL_GetError());
		return -1;
	}

	sdlRenderer = SDL_CreateRenderer(screen, -1, 0);
	//IYUV: Y + U + V  (3 planes)
	//YV12: Y + V + U  (3 planes)
	sdlTexture = SDL_CreateTexture(sdlRenderer, SDL_PIXELFORMAT_IYUV, SDL_TEXTUREACCESS_STREAMING, pCodecCtx->width, pCodecCtx->height);

	sdlRect.x = 0;
	sdlRect.y = 0;
	sdlRect.w = screen_w;
	sdlRect.h = screen_h;

	//SDL End----------------------
	while (av_read_frame(pFormatCtx, packet) >= 0) {
		if (packet->stream_index == videoindex) {
			//ret = avcodec_decode_video2(pCodecCtx, pFrame, &got_picture, packet);//错误时返回负值,否则返回已使用的字节数,如果无法解压缩任何帧,则返回零。
																					//got_picture 如果无法解压缩任何帧,则为零,否则为非零。
			ret = avcodec_send_packet(pCodecCtx, packet);		//成功返回0
			got_picture = avcodec_receive_frame(pCodecCtx, pFrame); //got_picture = 0 success, a frame was returned

			if (ret != 0) {
				printf("Decode Error.\n");
				return -1;
			}
			if (got_picture == 0) {
				sws_scale(img_convert_ctx, (const unsigned char* const*)pFrame->data, pFrame->linesize, 0, pCodecCtx->height,
					pFrameYUV->data, pFrameYUV->linesize);

#if OUTPUT_YUV420P
				y_size = pCodecCtx->width*pCodecCtx->height;
				fwrite(pFrameYUV->data[0], 1, y_size, fp_yuv);    //Y 
				fwrite(pFrameYUV->data[1], 1, y_size / 4, fp_yuv);  //U
				fwrite(pFrameYUV->data[2], 1, y_size / 4, fp_yuv);  //V
#endif
			//SDL---------------------------
#if 0
				SDL_UpdateTexture(sdlTexture, NULL, pFrameYUV->data[0], pFrameYUV->linesize[0]);
#else
				SDL_UpdateYUVTexture(sdlTexture, &sdlRect,
					pFrameYUV->data[0], pFrameYUV->linesize[0],
					pFrameYUV->data[1], pFrameYUV->linesize[1],
					pFrameYUV->data[2], pFrameYUV->linesize[2]);
#endif	

				SDL_RenderClear(sdlRenderer);
				SDL_RenderCopy(sdlRenderer, sdlTexture, NULL, &sdlRect);
				SDL_RenderPresent(sdlRenderer);
				//SDL End-----------------------
				//Delay 40ms
				SDL_Delay(10);
			}
		}
		//av_free_packet(packet);
		av_packet_unref(packet);
	}
	//flush decoder
	//FIX: Flush Frames remained in Codec
	while (1) {
		//ret = avcodec_decode_video2(pCodecCtx, pFrame, &got_picture, packet);
		ret = avcodec_send_packet(pCodecCtx, packet);
		got_picture = avcodec_receive_frame(pCodecCtx, pFrame); //got_picture = 0 success, a frame was returned

		if (ret != 0)
			break;
		if (got_picture == 0)
			break;
		sws_scale(img_convert_ctx, (const unsigned char* const*)pFrame->data, pFrame->linesize, 0, pCodecCtx->height,
			pFrameYUV->data, pFrameYUV->linesize);
#if OUTPUT_YUV420P
		int y_size = pCodecCtx->width*pCodecCtx->height;
		fwrite(pFrameYUV->data[0], 1, y_size, fp_yuv);    //Y 
		fwrite(pFrameYUV->data[1], 1, y_size / 4, fp_yuv);  //U
		fwrite(pFrameYUV->data[2], 1, y_size / 4, fp_yuv);  //V
#endif
		//SDL---------------------------
		SDL_UpdateTexture(sdlTexture, &sdlRect, pFrameYUV->data[0], pFrameYUV->linesize[0]);
		SDL_RenderClear(sdlRenderer);
		SDL_RenderCopy(sdlRenderer, sdlTexture, NULL, &sdlRect);
		SDL_RenderPresent(sdlRenderer);
		//SDL End-----------------------
		//Delay 40ms
		SDL_Delay(40);
	}

	sws_freeContext(img_convert_ctx);

#if OUTPUT_YUV420P 
	fclose(fp_yuv);
#endif 

	SDL_Quit();

	av_frame_free(&pFrameYUV);
	av_frame_free(&pFrame);
	avcodec_close(pCodecCtx);
	avformat_close_input(&pFormatCtx);

	system("pause");

	return 0;
}

 stdafx.h


// stdafx.h : 标准系统包含文件的包含文件,
// 或是经常使用但不常更改的
// 特定于项目的包含文件

#pragma once

#include <stdio.h>


extern "C"
{
#include "include\libavcodec\avcodec.h"
#include "include\libavformat\avformat.h"
#include "include\libavutil\channel_layout.h"
#include "include\libavutil\common.h"
#include "include\libavutil\imgutils.h"
#include "include\libswscale\swscale.h" 
#include "include\libavutil\imgutils.h"    
#include "include\libavutil\opt.h"       
#include "include\libavutil\mathematics.h"    
#include "include\libavutil\samplefmt.h"
#include "SDL\SDL.h"
};
#pragma comment(lib, "avcodec.lib")
#pragma comment(lib, "avformat.lib")
#pragma comment(lib, "avdevice.lib")
#pragma comment(lib, "avfilter.lib")
#pragma comment(lib, "avutil.lib")
#pragma comment(lib, "postproc.lib")
#pragma comment(lib, "swresample.lib")
#pragma comment(lib, "swscale.lib")

#pragma comment(lib, "SDL2.lib")
#pragma comment(lib, "SDL2main.lib")

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值