首页 > 其他分享 >H.264:FFMpeg 实现简单的播放器

H.264:FFMpeg 实现简单的播放器

时间:2023-02-17 10:57:19浏览次数:37  
标签:播放器 H.264 pCodecCtx FFMpeg height bmp pFrameYUV SDL pFormatCtx

FFMPEG工程浩大,可以参考的书籍又不是很多,因此很多刚学习FFMPEG的人常常感觉到无从下手。我刚接触FFMPEG的时候也感觉不知从何学起。

因此我把自己做项目过程中实现的一个非常简单的视频播放器(大约100行代码)源代码传上来,以作备忘,同时方便新手学习FFMPEG。

该播放器虽然简单,但是几乎包含了使用FFMPEG播放一个视频所有必备的API,并且使用SDL显示解码出来的视频。

并且支持流媒体等多种视频输入,处于简单考虑,没有音频部分,同时视频播放采用直接延时40ms的方式

平台使用VC2010

使用了最新的FFMPEG类库

直接贴代码

 int _tmain(int argc, _TCHAR* argv[])
 {
       AVFormatContext	*pFormatCtx;
       int i, videoindex;
       AVCodecContext	*pCodecCtx;
       AVCodec			*pCodec;
       char filepath[]="nwn.mp4";
       av_register_all();
       avformat_network_init();
       pFormatCtx = avformat_alloc_context();
       if(avformat_open_input(&pFormatCtx,filepath,NULL,NULL)!=0){
       	printf("无法打开文件\n");
       	return -1;
       }
       if(av_find_stream_info(pFormatCtx)<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)
       	{
       		videoindex=i;
       		break;
       	}
       	if(videoindex==-1)
       	{
       		printf("Didn't find a video stream.\n");
       		return -1;
       	}
       	pCodecCtx=pFormatCtx->streams[videoindex]->codec;
       	pCodec=avcodec_find_decoder(pCodecCtx->codec_id);
       	if(pCodec==NULL)
       	{
       		printf("Codec not found.\n");
       		return -1;
       	}
       	if(avcodec_open(pCodecCtx, pCodec)<0)
       	{
       		printf("Could not open codec.\n");
       		return -1;
       	}
       	AVFrame	*pFrame,*pFrameYUV;
       	pFrame=avcodec_alloc_frame();
       	pFrameYUV=avcodec_alloc_frame();
       	uint8_t *out_buffer;
       	out_buffer=new uint8_t[avpicture_get_size(PIX_FMT_YUV420P, pCodecCtx->width, pCodecCtx->height)];
       	avpicture_fill((AVPicture *)pFrameYUV, out_buffer, PIX_FMT_YUV420P, pCodecCtx->width, pCodecCtx->height);
       //------------SDL----------------
       	if(SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO | SDL_INIT_TIMER)) {  
       		printf( "Could not initialize SDL - %s\n", SDL_GetError()); 
       		exit(1);
       	} 
       	SDL_Surface *screen; 
       	screen = SDL_SetVideoMode(pCodecCtx->width, pCodecCtx->height, 0, 0);
       	if(!screen) {  printf("SDL: could not set video mode - exiting\n");  
       	exit(1);
       	}
       	SDL_Overlay *bmp; 
       	bmp = SDL_CreateYUVOverlay(pCodecCtx->width, pCodecCtx->height,SDL_YV12_OVERLAY, screen); 
       	SDL_Rect rect;
       //---------------
       	int ret, got_picture;
       	static struct SwsContext *img_convert_ctx;
       	int y_size = pCodecCtx->width * pCodecCtx->height;
       
       	AVPacket *packet=(AVPacket *)malloc(sizeof(AVPacket));
       	av_new_packet(packet, y_size);
       	//输出一下信息-----------------------------
       	printf("文件信息-----------------------------------------\n");
       	av_dump_format(pFormatCtx,0,filepath,0);
       	printf("-------------------------------------------------\n");
       	//------------------------------
       	while(av_read_frame(pFormatCtx, packet)>=0)
       	{
       		if(packet->stream_index==videoindex)
       		{
       			ret = avcodec_decode_video2(pCodecCtx, pFrame, &got_picture, packet);
       			if(ret < 0)
       			{
       				printf("解码错误\n");
       				return -1;
       			}
       			if(got_picture)
       			{
       				img_convert_ctx = sws_getContext(pCodecCtx->width, pCodecCtx->height, pCodecCtx->pix_fmt, pCodecCtx->width, pCodecCtx->height, PIX_FMT_YUV420P,   SWS_BICUBIC, NULL, NULL, NULL); 
       				sws_scale(img_convert_ctx, (const uint8_t* const*)pFrame->data, pFrame->linesize, 0, pCodecCtx->height, pFrameYUV->data, pFrameYUV->linesize);
       
       				SDL_LockYUVOverlay(bmp);
       				bmp->pixels[0]=pFrameYUV->data[0];
       				bmp->pixels[2]=pFrameYUV->data[1];
       				bmp->pixels[1]=pFrameYUV->data[2];     
       				bmp->pitches[0]=pFrameYUV->linesize[0];
       				bmp->pitches[2]=pFrameYUV->linesize[1];   
       				bmp->pitches[1]=pFrameYUV->linesize[2];
       				SDL_UnlockYUVOverlay(bmp); 
       				rect.x = 0;    
       				rect.y = 0;    
       				rect.w = pCodecCtx->width;    
       				rect.h = pCodecCtx->height;    
       				SDL_DisplayYUVOverlay(bmp, &rect); 
       				//延时40ms
       				SDL_Delay(40);
       			}
       		}
       		av_free_packet(packet);
       	}
       	delete[] out_buffer;
       	av_free(pFrameYUV);
       	avcodec_close(pCodecCtx);
       	avformat_close_input(&pFormatCtx);
       
       	return 0;
       }
 

标签:播放器,H.264,pCodecCtx,FFMpeg,height,bmp,pFrameYUV,SDL,pFormatCtx
From: https://www.cnblogs.com/kn-zheng/p/17129352.html

相关文章

  • .NET 中创建录音机和播放器应用
    前言在本博客中,你将了解如何在.NETMAUI中开发录音机和播放器。音频播放器将录制和播放音频文件。此应用程序可以在Android和iOS上部署和使用。预览以下是该录音机和......
  • 实践:基于腾讯云播放器SDK,带您体验播放多场景下的 COS 视频文件
    一.实践步骤1.准备您的腾讯云COS视频文件链接,您需要:1.1创建一个存储桶;1.2上传对象;1.3在对象信息详情里复制对象地址;注意:    目前腾讯云......
  • Android使用FFmpeg的API库
    Java可以通过JNI调用原生库中的函数,原生库完全是由原生的CPU指令堆叠起来的,所以运行速度很快。大部分的原生库都是用C/C++编译出来的。因此,Android里面也能通过JNI......
  • FFmpeg打开输入文件
    本文介绍如何使用FFmpeg的API函数 ​​avformat_open_input​​​,​​avformat_find_stream_info​​ 来打开跟获取一个输入文件的信息(AVFormatContext)。本文的代码......
  • react-native音乐播放器
    android效果[video(video-q8EJW2y5-1592291410395)(type-bilibili)(url-https://player.bilibili.com/player.html?aid=583605396)(image-https://img-blog.csdnimg.cn/img......
  • uni-app写小程序音乐播放器
    uni-app开发,学习成本低开发小程序还是比较好用的uni-app框架,这个框架现阶段还是有不少问题的,项目中遇到的几点1,v-show不能正常使用->绑定一个display:none的样式根据条件判......
  • H.265网页播放器EasyPlayer添加sei数据导致视频花屏该如何解决?
    EasyPlayer属于TSINGSEE青犀视频研发的性能稳定、播放流畅的H.265视频流媒体播放器,可支持的视频流格式有RTSP、RTMP、HLS、FLV、WebRTC等,支持高清画面秒开、视频播放流畅,具......
  • H.265流媒体EasyPlayer播放器添加sei数据导致视频花屏该如何解决?
    EasyPlayer属于TSINGSEE青犀视频研发的性能稳定、播放流畅的H.265视频流媒体播放器,可支持的视频流格式有RTSP、RTMP、HLS、FLV、WebRTC等,支持高清画面秒开、视频播放流畅,具......
  • ffmpeg均匀分割视频命令
    ffmpeg-y-iinput.wav-fsegment-segment_time5output-%d.wav-segment_time分割视频片段大小-segmenet_list<file_name>同时生成名为file_name的文件,记录分割......
  • ffmpeg计算vmaf
    命令如下:ffmpeg.exe-ie:/videodata/dst.ts-ie:/videodata/src-20s.ts-lavfilibvmaf="model_path=C\\:/\Users/\pc/\Desktop/\FFMetrics.1.3.1/\FFMetrics/\vmaf-mo......