1.下载sdl2,ffmpeg dev

2.qt的配置

win32: LIBS += -L$$PWD/libffmpeg/lib/ -lavcodec -lavdevice -lavfilter -lavformat -lavutil -lpostproc -lswscale
win32: LIBS += -L$$PWD/libSDL/lib/x86/ -lSDL2 -lSDL2mainINCLUDEPATH += $$PWD/libffmpeg/include
DEPENDPATH += $$PWD/libffmpeg/includeINCLUDEPATH += $$PWD/libSDL/include
DEPENDPATH += $$PWD/libSDL/include

3.类接口源码(参考了雷博士的源码)

#ifndef VIDEOPLAYER_H
#define VIDEOPLAYER_H#ifdef _WIN32
//Windows
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 <libavcodec/avcodec.h>
#include <libavformat/avformat.h>
#include <libswscale/swscale.h>
#include <SDL2/SDL.h>
#ifdef __cplusplus
};
#endif
#endifclass VideoPlayer
{
public:VideoPlayer(void* hwnd);~VideoPlayer();//init ffmpeg SDLstatic int StaticInit();//crate videoplayerstatic VideoPlayer * CreateNew(void *hwnd);//int OpenVideo(const char * videopath);//int Play();//int Stop();//int Pause();//int Continue();void test();
public:static bool IsStaticInit;AVFormatContext *pFormatCtx;AVCodecContext  *pCodecCtx;int             videoindex;//--------------SDL---------SDL_Texture* sdlTexture;SDL_Renderer* sdlRenderer;bool             fPause;bool            fEndThread;private:uint8_t *out_buffer;//AVPacket *packet;//------------SDL----------------int screen_w, screen_h;SDL_Window *screen;SDL_Rect sdlRect;SDL_Thread *video_tid;SDL_Event event;char  fVideoPath[512];void * fHwnd;
};#endif // VIDEOPLAYER_H
#include "videoplayer.h"
#include <QDebug>bool VideoPlayer::IsStaticInit = false;int video_refresh_thread(void *opaque)
{VideoPlayer *vplayer = (VideoPlayer*)opaque;if(vplayer == NULL){return 0;}AVPacket * packet = (AVPacket *)av_malloc(sizeof(AVPacket));AVFrame  *pFrame, *pFrameYUV;int ret, got_picture;struct SwsContext *img_convert_ctx;uint8_t *out_buffer;pFrame = av_frame_alloc();pFrameYUV = av_frame_alloc();out_buffer = (uint8_t *)av_malloc(avpicture_get_size(AV_PIX_FMT_YUV420P, vplayer->pCodecCtx->width, vplayer->pCodecCtx->height));avpicture_fill((AVPicture *)pFrameYUV, out_buffer, AV_PIX_FMT_YUV420P, vplayer->pCodecCtx->width, vplayer->pCodecCtx->height);img_convert_ctx = sws_getContext(vplayer->pCodecCtx->width, vplayer->pCodecCtx->height, vplayer->pCodecCtx->pix_fmt, vplayer->pCodecCtx->width,vplayer-> pCodecCtx->height, AV_PIX_FMT_YUV420P, SWS_BICUBIC, NULL, NULL, NULL);while(!vplayer->fEndThread){if(!vplayer->fPause){if (av_read_frame(vplayer->pFormatCtx, packet) >= 0) {if (packet->stream_index == vplayer->videoindex) {ret = avcodec_decode_video2(vplayer->pCodecCtx, pFrame, &got_picture, packet);if (ret < 0) {printf("Decode Error.\n");break;}if (got_picture) {sws_scale(img_convert_ctx, (const uint8_t* const*)pFrame->data, pFrame->linesize, 0, vplayer->pCodecCtx->height, pFrameYUV->data, pFrameYUV->linesize);//SDL---------------------------SDL_UpdateTexture(vplayer->sdlTexture, NULL, pFrameYUV->data[0], pFrameYUV->linesize[0]);SDL_RenderClear(vplayer->sdlRenderer);//SDL_RenderCopy( sdlRenderer, sdlTexture, &sdlRect, &sdlRect );SDL_RenderCopy(vplayer->sdlRenderer, vplayer->sdlTexture, NULL, NULL);SDL_RenderPresent(vplayer->sdlRenderer);//SDL End-----------------------}}av_free_packet(packet);}else{break;}}SDL_Delay(40);}if(vplayer->sdlRenderer)SDL_RenderClear(vplayer->sdlRenderer);sws_freeContext(img_convert_ctx);av_frame_free(&pFrameYUV);av_frame_free(&pFrame);vplayer->fEndThread = false;vplayer->fPause = false;return 0;
}VideoPlayer::VideoPlayer(void *hwnd):fHwnd(hwnd),pFormatCtx(NULL),pCodecCtx(NULL),sdlRenderer(NULL),sdlTexture(NULL),video_tid(NULL),fPause(false),fEndThread(false)
{//03.申请context内存pFormatCtx = avformat_alloc_context();
}VideoPlayer::~VideoPlayer()
{SDL_Quit();Stop();
}int VideoPlayer::StaticInit()
{if(IsStaticInit){return 0;}IsStaticInit = true;//00.初始化SDL 视频、音频、定时器if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO | SDL_INIT_TIMER)) {printf("Could not initialize SDL - %s\n", SDL_GetError());return -1;}//01.ffpeg初始化av_register_all();//02.网络初始化return avformat_network_init();
}int VideoPlayer::OpenVideo(const char *videopath)
{strcpy(fVideoPath, videopath);return 0;
}int VideoPlayer::Play()
{//04.打开视频if (avformat_open_input(&pFormatCtx, fVideoPath, NULL, NULL) != 0) {printf("Couldn't open input stream.\n");return -1;}//05.获取视频信息if (avformat_find_stream_info(pFormatCtx, NULL) < 0) {printf("Couldn't find stream information.\n");return -1;}//06.获取视频流的序号videoindex = -1;for (unsigned int i = 0; i < pFormatCtx->nb_streams; i++)if (pFormatCtx->streams[i]->codec->codec_type == AVMEDIA_TYPE_VIDEO) {videoindex = i;break;}//07.是否存在视频流if (videoindex == -1) {printf("Didn't find a video stream.\n");return -1;}//08.视频编码方式pCodecCtx = pFormatCtx->streams[videoindex]->codec;AVCodec    *pCodec;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;}//09.创建渲染器和纹理sdlRenderer = SDL_CreateRenderer(screen, -1, 0);sdlTexture = SDL_CreateTexture(sdlRenderer, SDL_PIXELFORMAT_IYUV, SDL_TEXTUREACCESS_STREAMING, pCodecCtx->width, pCodecCtx->height);video_tid = SDL_CreateThread(video_refresh_thread, NULL, (void*)this);return 0;
}int VideoPlayer::Stop()
{if(video_tid){fEndThread = true;SDL_WaitThread(video_tid, NULL);video_tid = NULL;fEndThread = false;}if(sdlTexture){SDL_DestroyTexture(sdlTexture);sdlTexture = NULL;}if(sdlRenderer){SDL_RenderClear(sdlRenderer);SDL_DestroyRenderer(sdlRenderer);sdlRenderer = NULL;}if(pCodecCtx){avcodec_close(pCodecCtx);pCodecCtx = NULL;}if(pFormatCtx){avformat_close_input(&pFormatCtx);pFormatCtx = NULL;}return 0;}int VideoPlayer::Pause()
{fPause = true;return 0;
}int VideoPlayer::Continue()
{fPause = false;return 0;
}void VideoPlayer::test()
{SDL_Surface *sur = SDL_GetWindowSurface(screen);;SDL_BlitSurface( SDL_LoadBMP("d:/123.bmp") , NULL ,sur ,NULL);SDL_UpdateWindowSurface(screen);//更新显示copy the window surface to the screen
}VideoPlayer *VideoPlayer::CreateNew(void * hwnd)
{SDL_Window *screen = SDL_CreateWindowFrom(hwnd);if(screen){VideoPlayer *vplayer = new VideoPlayer(hwnd);vplayer->screen = screen;return vplayer;}else{qDebug()<<SDL_GetError();return NULL;}
}

QT ffmpeg 播放器相关推荐

  1. 某牛的QT+ffmpeg 播放器 的readme

    http://download.csdn.net/source/2802201 write by :lbwave@sina.com QT+ffmpeg 大名顶顶的QT ffmpeg 我就不用说了.看看 ...

  2. C++基于ffmpeg和QT开发播放器~学习笔记

    C++基于ffmpeg和QT开发播放器 B站网址 https://www.bilibili.com/video/BV1h44y1t7D8?p=2&spm_id_from=pageDriver ...

  3. 《Android FFmpeg 播放器开发梳理》第一章 播放器初始化与解复用流程

    <Android FFmpeg 播放器开发梳理>: 第零章 基础公共类的封装 播放器初始化与解复用流程 这一章,我们来讲解播放器解复用(从文件中读取数据包)的流程.在讲解播放器的读数据包流 ...

  4. FFmpeg音频播放器(8)-创建FFmpeg播放器

    原文地址::https://www.jianshu.com/p/73b0a0a9bb0d 相关文章 1.FFmpeg音频解码播放----https://www.jianshu.com/p/76562a ...

  5. ffmpeg播放器实现详解 - 音频同步控制

    ffplay是ffmpeg源码中一个自带的开源播放器实例,同时支持本地视频文件的播放以及在线流媒体播放,功能非常强大. FFplay: FFplay is a very simple and port ...

  6. Qt+FFmpeg播放RTSP H264视频流(1)- 在Qt项目加入FFmpeg库

    Qt FFmpeg播放RTSP H264视频流(1) QtCreator引入FFmpeg库 下载FFmpeg库 添加FFmpeg库到Qt项目 测试FFmpeg库是否能正常使用 QtCreator引入F ...

  7. Qt FFmpeg视频播放器开发(八):播放器UI改造、高仿QQ影音

      最近把播放器项目进行了更新,决定参照QQ影音的界面进行实现,我现在的实现如下:   下图是真实的QQ影音   相比QQ影音界面,我的实现有一定的差距,主要是控件的配色,以及中间那个动态图,由于没有 ...

  8. 基于FFmpeg和Qt的播放器 QtAV库

    参与项目开发: https://github.com/wang-bin/QtAV 下载编译好的文件等:https://sourceforge.NET/projects/qtav win下编译好的可执行 ...

  9. C++实战手把手教您用ffmpeg和QT开发播放器--01音视频基础知识

    笔记:https://blog.csdn.net/tainjau/article/category/9272757 音视频基础知识 封装.解码.重采样.像素格式 1.MPEG-4 MPEG-4标准将众 ...

  10. ffmpeg播放器(一) 视频解码与播放

    1.环境搭建 首先需要导入所需要的包include.armeabi-v7a. 然后跟项目建立连接,在CMakeList.txt,并做了相关的解释: cmake_minimum_required(VER ...

最新文章

  1. 初学Java Web(4)——Servlet学习总结
  2. ubuntu下面制作ubuntu系统启动盘(转载+自己整理)
  3. 切换器黑屏_景阳华泰科技高清无缝矩阵切换器高端视频会议运用
  4. int最大值java_Java 中一个int型数组的求最大值最小值 | 学步园
  5. cmos和ttl_TTL电平和CMOS电平的区别
  6. 重写Android系统自带Dialog
  7. iOS的主要框架介绍
  8. c++中 . 和 - 的区别
  9. 21个实用便利的PHP代码
  10. 学习Struts2框架笔记-第1天
  11. LaTex常用特殊符号对应表
  12. @Modifying 注解
  13. 移动APP云测试平台测评分析
  14. O2O模式成功案例分享 汲取精华化为己用
  15. FAST-LIO公式推导
  16. javaweb练手项目jsp+servlet简易购物车系统
  17. 通信码元速率和带宽理解
  18. QGIS插件python开发环境配置和PyCharm配置调试环境
  19. 细数最暖心的地铁营销事件,你最喜欢哪一个?
  20. 联盟广告回利模式4种

热门文章

  1. 什么是软件外包公司?要不要去外包公司?
  2. 初学者必读VRay 2.0材质设置(2)——反射材质的表现
  3. 8个免费在线字体转换器
  4. storm风暴英雄 tempo_Tempostorm战队攻略:新版本下的终结者天赋
  5. 快恢复二极管工作原理、反向恢复时间详解
  6. Python文件(二):数据组织的维度,一维数据的表示、存储、处理
  7. ajax到底怎么读呢
  8. 微信协议服务器端口,优咖对你说| 从TCP协议栈角度分析微信常用业务模型
  9. DoublyLinkedList
  10. android 钛备份,钛备份使用教程