采用OpenCv获取图像数据,通过ffmpeg推流给rtmp服务器
OpenCV获取的图像数据为BGR格式,需要转换成YUV格式,再将其编码为h264格式,通过ffmpeg推流
ps:ffmpeg版本是2.8
头文件
extern "C"
{
#include <libavcodec/avcodec.h>
#include <libavdevice/avdevice.h>
#include <libavformat/avformat.h>
#include <libavutil/mathematics.h>
#include <libavutil/opt.h>
#include <libavutil/time.h>
#include <libavutil/timestamp.h>
#include <libswscale/swscale.h>
}
主要实现
//nginx-rtmp 直播服务器rtmp推流URL
char *outUrl = "rtmp://localhost:6666/live/Cam";
//注册所有的编解码器
avcodec_register_all();
//注册所有的封装器
av_register_all();
//注册所有网络协议
avformat_network_init();
Mat orgFrame;
//像素格式转换上下文
SwsContext *vsc = NULL;
//输出的数据结构
AVFrame *yuv = NULL;
//编码器上下文
AVCodecContext *avctx = NULL;
//rtmp flv 封装器
AVFormatContext *ofmt_ctx = NULL;
if(m_cap) //VideoCapture* m_cap;
{
if(m_cap->isOpened() == false)
{
bool ret = m_cap->open(0);
if(ret == false && QFile::exists(DEFAULT_CAMERA_DEV))
{
ret = m_cap->open(DEFAULT_CAMERA_DEV);
}
// if(ret)
// {
// m_cap->set(CV_CAP_PROP_FRAME_WIDTH, 640);
// m_cap->set(CV_CAP_PROP_FRAME_HEIGHT, 480);
// m_cap->set(CV_CAP_PROP_FPS, 30);
// }
}
}
else
return -1;
int framecnt = 0;
i