100行代码实现最简单的基于FFMPEG+SDL的视频播放器

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

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

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

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

平台使用VC2010

使用了最新的FFMPEG类库

直接贴代码

[cpp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. int _tmain(int argc, _TCHAR* argv[])  
  2. {  
  3.     AVFormatContext *pFormatCtx;  
  4.     int             i, videoindex;  
  5.     AVCodecContext  *pCodecCtx;  
  6.     AVCodec         *pCodec;  
  7.     char filepath[]="nwn.mp4";  
  8.     av_register_all();  
  9.     avformat_network_init();  
  10.     pFormatCtx = avformat_alloc_context();  
  11.     if(avformat_open_input(&pFormatCtx,filepath,NULL,NULL)!=0){  
  12.         printf("无法打开文件\n");  
  13.         return -1;  
  14.     }  
  15.     if(av_find_stream_info(pFormatCtx)<0)  
  16.     {  
  17.         printf("Couldn't find stream information.\n");  
  18.         return -1;  
  19.     }  
  20.     videoindex=-1;  
  21.     for(i=0; i<pFormatCtx->nb_streams; i++)   
  22.         if(pFormatCtx->streams[i]->codec->codec_type==AVMEDIA_TYPE_VIDEO)  
  23.         {  
  24.             videoindex=i;  
  25.             break;  
  26.         }  
  27.         if(videoindex==-1)  
  28.         {  
  29.             printf("Didn't find a video stream.\n");  
  30.             return -1;  
  31.         }  
  32.         pCodecCtx=pFormatCtx->streams[videoindex]->codec;  
  33.         pCodec=avcodec_find_decoder(pCodecCtx->codec_id);  
  34.         if(pCodec==NULL)  
  35.         {  
  36.             printf("Codec not found.\n");  
  37.             return -1;  
  38.         }  
  39.         if(avcodec_open(pCodecCtx, pCodec)<0)  
  40.         {  
  41.             printf("Could not open codec.\n");  
  42.             return -1;  
  43.         }  
  44.         AVFrame *pFrame,*pFrameYUV;  
  45.         pFrame=avcodec_alloc_frame();  
  46.         pFrameYUV=avcodec_alloc_frame();  
  47.         uint8_t *out_buffer;  
  48.         out_buffer=new uint8_t[avpicture_get_size(PIX_FMT_YUV420P, pCodecCtx->width, pCodecCtx->height)];  
  49.         avpicture_fill((AVPicture *)pFrameYUV, out_buffer, PIX_FMT_YUV420P, pCodecCtx->width, pCodecCtx->height);  
  50. //------------SDL----------------  
  51.         if(SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO | SDL_INIT_TIMER)) {    
  52.             printf( "Could not initialize SDL - %s\n", SDL_GetError());   
  53.             exit(1);  
  54.         }   
  55.         SDL_Surface *screen;   
  56.         screen = SDL_SetVideoMode(pCodecCtx->width, pCodecCtx->height, 0, 0);  
  57.         if(!screen) {  printf("SDL: could not set video mode - exiting\n");    
  58.         exit(1);  
  59.         }  
  60.         SDL_Overlay *bmp;   
  61.         bmp = SDL_CreateYUVOverlay(pCodecCtx->width, pCodecCtx->height,SDL_YV12_OVERLAY, screen);   
  62.         SDL_Rect rect;  
  63. //---------------  
  64.         int ret, got_picture;  
  65.         static struct SwsContext *img_convert_ctx;  
  66.         int y_size = pCodecCtx->width * pCodecCtx->height;  
  67.   
  68.         AVPacket *packet=(AVPacket *)malloc(sizeof(AVPacket));  
  69.         av_new_packet(packet, y_size);  
  70.         //输出一下信息-----------------------------  
  71.         printf("文件信息-----------------------------------------\n");  
  72.         av_dump_format(pFormatCtx,0,filepath,0);  
  73.         printf("-------------------------------------------------\n");  
  74.         //------------------------------  
  75.         while(av_read_frame(pFormatCtx, packet)>=0)  
  76.         {  
  77.             if(packet->stream_index==videoindex)  
  78.             {  
  79.                 ret = avcodec_decode_video2(pCodecCtx, pFrame, &got_picture, packet);  
  80.                 if(ret < 0)  
  81.                 {  
  82.                     printf("解码错误\n");  
  83.                     return -1;  
  84.                 }  
  85.                 if(got_picture)  
  86.                 {  
  87.                     img_convert_ctx = sws_getContext(pCodecCtx->width, pCodecCtx->height, pCodecCtx->pix_fmt, pCodecCtx->width, pCodecCtx->height, PIX_FMT_YUV420P, SWS_BICUBIC, NULL, NULL, NULL);   
  88.                     sws_scale(img_convert_ctx, (const uint8_t* const*)pFrame->data, pFrame->linesize, 0, pCodecCtx->height, pFrameYUV->data, pFrameYUV->linesize);  
  89.   
  90.                     SDL_LockYUVOverlay(bmp);  
  91.                     bmp->pixels[0]=pFrameYUV->data[0];  
  92.                     bmp->pixels[2]=pFrameYUV->data[1];  
  93.                     bmp->pixels[1]=pFrameYUV->data[2];       
  94.                     bmp->pitches[0]=pFrameYUV->linesize[0];  
  95.                     bmp->pitches[2]=pFrameYUV->linesize[1];     
  96.                     bmp->pitches[1]=pFrameYUV->linesize[2];  
  97.                     SDL_UnlockYUVOverlay(bmp);   
  98.                     rect.x = 0;      
  99.                     rect.y = 0;      
  100.                     rect.w = pCodecCtx->width;      
  101.                     rect.h = pCodecCtx->height;      
  102.                     SDL_DisplayYUVOverlay(bmp, &rect);   
  103.                     //延时40ms  
  104.                     SDL_Delay(40);  
  105.                 }  
  106.             }  
  107.             av_free_packet(packet);  
  108.         }  
  109.         delete[] out_buffer;  
  110.         av_free(pFrameYUV);  
  111.         avcodec_close(pCodecCtx);  
  112.         avformat_close_input(&pFormatCtx);  
  113.   
  114.         return 0;  
  115. }  

软件运行截图:

完整工程下载地址:

http://download.csdn.net/detail/leixiaohua1020/5122959


FFMPEG相关学习资料:

SDL GUIDE 中文译本

ffdoc (FFMPEG的最完整教程)
如何用FFmpeg编写一个简单播放器

 

==============================

注:以上程序有一个小BUG,就是sws_getContext()之后,需要调用sws_freeContext()。否则长时间运行的话,会出现内存泄露的状况。

修复了此BUG的版本已经传到SourceForge上了:

https://sourceforge.net/projects/simplestffmpegplayer/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值