基于SDL2.0和ffmpeg(2017,9,4版本)的简单视频播放器

参考文章 1:http://blog.csdn.net/leixiaohua1020/article/details/8652605 
2:http://blog.csdn.net/leixiaohua1020/article/details/40525591
#ifdef _WIN32  
//Windows  
#include 
   
   
    
    
extern "C"  
{  
#include "libavcodec/avcodec.h"  
#include "libavformat/avformat.h"  
#include "libswscale/swscale.h"  
#include "SDL.h"  
};  
#else  
//Linux...  
#ifdef __cplusplus  
extern "C"  
{  
#endif  
#include 
    
    
     
       
#include 
     
     
      
        
#include 
      
      
       
         
#include 
       
       
         #ifdef __cplusplus }; #endif #endif //Refresh #define SFM_REFRESH_EVENT (SDL_USEREVENT + 1) int thread_exit=0; //Thread int sfp_refresh_thread(void *opaque) { SDL_Event event; while (thread_exit==0) { event.type = SFM_REFRESH_EVENT; SDL_PushEvent(&event); //Wait 40 ms SDL_Delay(40); } return 0; } int SCREENW = 640; int SCREENH = 480; int main(int argc, char* argv[]) { AVFormatContext *pFormatCtx; int i, videoindex; AVCodecContext *pCodecCtx; AVCodec *pCodec; AVFrame *pFrame,*pFrameYUV; AVPacket *packet; struct SwsContext *img_convert_ctx; //SDL int ret, got_picture; int screen_w=0,screen_h=0; //SDL_Surface *screen; //SDL_Overlay *bmp; std::cout< 
        
          nb_streams); i++) 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 = avcodec_alloc_context3(NULL); avcodec_parameters_to_context(pCodecCtx,pFormatCtx->streams[videoindex]->codecpar); 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(); pFrameYUV=av_frame_alloc(); //uint8_t *out_buffer=(uint8_t *)av_malloc(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()); return -1; } SDL_Window *screen; screen = SDL_CreateWindow("palyer",SDL_WINDOWPOS_UNDEFINED,SDL_WINDOWPOS_UNDEFINED,SCREENW,SCREENH,SDL_WINDOW_OPENGL|SDL_WINDOW_RESIZABLE); if(!screen) { printf("SDL: could not create window - exiting:%s\n",SDL_GetError()); return -1; } SDL_Renderer* sdlRenderer = SDL_CreateRenderer(screen, -1, 0); screen_w = pCodecCtx->width; screen_h = pCodecCtx->height; unsigned char *buffer = new unsigned char[screen_w*screen_h*3/2]; //screen = SDL_SetVideoMode(screen_w, screen_h, 0,0); SDL_Texture* sdlTexture = SDL_CreateTexture(sdlRenderer,SDL_PIXELFORMAT_IYUV, SDL_TEXTUREACCESS_STREAMING,screen_w,screen_h); rect.x = 0; rect.y = 0; rect.w = screen_w; rect.h = screen_h; packet=(AVPacket *)av_malloc(sizeof(AVPacket)); 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, pCodecCtx->height, AV_PIX_FMT_YUV420P, SWS_BICUBIC, NULL, NULL, NULL); //-------------- video_tid = SDL_CreateThread(sfp_refresh_thread,NULL,NULL); // //Event Loop for (;;) { //Wait SDL_WaitEvent(&myEvent); if(myEvent.type==SFM_REFRESH_EVENT){ //------------------------------ if(av_read_frame(pFormatCtx, packet)>=0){ if(packet->stream_index==videoindex){ //ret = avcodec_decode_video2(pCodecCtx, pFrame, &got_picture, packet); ret = avcodec_send_packet(pCodecCtx,packet); if(ret < 0){ printf("Decode Error.\n"); return -1; } ret = avcodec_receive_frame(pCodecCtx,pFrame); if(ret== AVERROR(EAGAIN) || ret==AVERROR_EOF) continue; else if(ret<0) { exit(-1); } if(true){ for(int i=0;i 
         
           data[0] + j + i*pFrame->linesize[0]); } for(int i=0;i 
          
            data[1] + j + i*pFrame->linesize[1]); buffer[(5*screen_w*screen_h/4)+j+(i*screen_w/2)] = *(pFrame->data[2] + j + i*pFrame->linesize[2]); } } SDL_UpdateTexture( sdlTexture, NULL, buffer, screen_w); rect.x = 0; rect.y = 0; rect.w = screen_w; rect.h = screen_h; SDL_RenderClear( sdlRenderer ); SDL_RenderCopy( sdlRenderer, sdlTexture, NULL, &rect); SDL_RenderPresent( sdlRenderer ); //Delay 40ms SDL_Delay(40); } } av_packet_unref(packet); }else{ //Exit Thread thread_exit=1; break; } }else if(myEvent.type==SDL_QUIT){ break; } } delete[] buffer; SDL_Quit(); av_free(pFrameYUV); avcodec_close(pCodecCtx); avformat_close_input(&pFormatCtx); return 0; } 
           
          
         
       
      
      
     
     
    
    
   
   



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要下载一个基于FFmpeg SDL的最简单视频播放器,可以按照以下步骤进行: 1. 首先,需要下载和安装FFmpeg软件包。FFmpeg是一个开源的跨平台多媒体框架,可以用于处理音频和视频文件。可以上FFmpeg官网(https://www.ffmpeg.org/)找到相应的下载链接,并根据操作系统选择正确的版本进行下载和安装。 2. 下载SDL库。SDL是一个跨平台的开发库,可以用于创建多媒体应用程序。可以在SDL官网(https://www.libsdl.org/)上找到相应的下载链接,并选择适合自己操作系统的版本进行下载和安装。 3. 使用编程语言(如C/C++)编写一个基于FFmpegSDL视频播放器。可以使用任何喜欢的集成开发环境(IDE),如Visual Studio、Dev-C++等。根据自己的需求,可以封装FFmpegSDL的相关函数,以方便播放视频文件。 4. 在编程中,需要包含FFmpegSDL所需的头文件,并链接FFmpegSDL的库文件。可以在编译选项中添加"-lffmpeg"和"-lsdl"等参数。 5. 编写代码来打开视频文件,读取视频流,将每一帧解码和渲染到屏幕上并进行播放。可以使用FFmpeg提供的函数来进行解码和渲染,使用SDL提供的函数来显示图像并进行窗口管理。 6. 编译和运行程序,即可实现最简单的基于FFmpeg SDL视频播放器。可以通过命令行输入视频文件的路径进行播放。 需要注意的是,基于FFmpeg SDL视频播放器可以根据个人需求来进行功能的扩展,如添加播放控制(播放、暂停、停止等)、全屏显示、音量调节等功能。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值