Qt开源小项目-VCProject-rc文件的版本号修改-md/小白学习音视频(五)OpenCV获取USB Camera画面推流nginx-rtmp
小白学习音视频(五)OpenCV获取USB Camera画面推流nginx-rtmp
关注微信号:cpp手艺人,获取更多文章
在上一节中,我们编译了OpenCV的源码,并且演示了简单的demo。
这一节中,我们利用OpenCV 打开USB Camera,并且推流。
我这里使用的USB Camera就是一个普通的摄像头,大家可以在淘宝挑一个就行了
这里我就列出主要的代码,每行代码都有注释,这里我就说下整个源码的思路:
1.首先利用OpenCV打开USB Camera。
2.准备ffmpeg推流的数据结构。
3.利用OpenCV获取Camera的每一帧数据。
4.因为OpenCV获取的到数据都是RGB格式,需要转换为YUV420P格式。
5.获取YUV420p数据之后,在利用FFmpeg编码。
6.在利用FFmpeg将编码后的数据推送到Nginx服务器
下面就是主要的源码了。
void init_av() {
// 注册所有的编解码器
avcodec_register_all();
// 注册所有的封装器
av_register_all();
// 注册所有网络协议
avformat_network_init();
}
void opencv_rtmp() {
char *out_url = "rtmp://192.168.26.31/live";
init_av();
// 输出的数据结构
AVFrame *yuv = NULL;
unique_ptr<VideoCapture, std::function<void(VideoCapture *)>> video_ptr(
new VideoCapture(), [](VideoCapture *captrue) {
if (captrue->isOpened()) { captrue->release(); }
delete captrue;
});
Mat frame;
// 1.使用opencv 打开usb 摄像头
video_ptr->open(0);
if (! video_ptr->isOpened()) {
cout << "camera open usb camera error" << endl;
return;
}
cout << "open usb camera successful." << endl;
int width = video_ptr->get(CAP_PROP_FRAME_WIDTH);
int height = video_ptr->get(CAP_PROP_FRAME_HEIGHT);
int fps = video_ptr->get(CAP_PROP_FPS);
// 如果fps为0,这里就设置25。因为在fps=0时,调用avcodec_open2返回-22,
// 参数不合法
if (0 == fps) { fps = 25; }
// 2.初始化格式转换上下文
SwsContext *sws_context = NULL;
sws_context = sws_getCachedContext(sws_context,
width, height, AV_PIX_FMT_BGR24, // 源格式
width, height, AV_PIX_FMT_YUV420P, // 目标格式
SWS_BICUBIC, // 尺寸变化使用算法
0, 0, 0);
if (NULL == sws_context) {
cout << "sws_getCachedContext error" << endl;
return;
}
unique_ptr<SwsContext, std::function<void(SwsContext *)>> swscontext_tr
(sws_context, [](SwsContext *swscontext) {
sws_freeContext(swscontext);
swscontext = nullptr;
});
// 3.初始化输出的数据结构
yuv = av_frame_alloc();
yuv->format = AV_PIX_FMT_YUV420P;
yuv->width = width;
yuv->height = height;
yuv->pts = 0;
// 分配yuv空间
int ret_code = av_frame_get_buffer(yuv, 32);
if (0 != ret_code) {
av_error(ret_code);
return;
}
// 4.初始化编码上下文
// 4.1找到编码器
AVCodec *codec = avcodec_find_encoder(AV_CODEC_ID_H264);
if (NULL == codec) {
cout << "Can't find h264 encoder." << endl;
return;
}
// 4.2创建编码器上下文
AVCodecContext *codec_context = avcodec_alloc_context3(codec);
if (NULL == codec_context) {
cout << "avcodec_alloc_context3 failed." << endl;
return;
}
unique_ptr<AVCodecContext, std::function<void(AVCodecContext *)>> codec_context_ptr
(codec_context, [](AVCodecContext *context) {
avcodec_free_context(&context);
});
// 4.3配置编码器参数
// vc->flags |= AV_CODEC_FLAG_GLOBAL_HEADER;
codec_context->codec_id = codec->id;
codec_context->thread_count = 8;
// 压缩后每秒视频的bit流 50k
codec_context->bit_rate = 50*1024*8;
codec_context->width = width;
codec_context->height = height;
codec_context->time_base = {1, fps};
codec_context->framerate = {fps, 1};
// 画面组的大小,多少帧一个关键帧
codec_context->gop_size = 50;
codec_context->max_b_frames = 0;
codec_context->pix_fmt = AV_PIX_FMT_YUV420P;
codec_context->qmin = 10;
codec_context->qmax = 51;
// 4.4打开编码器上下文
ret_code = avcodec_open2(codec_context, 0, 0);
if (0 != ret_code) {
av_error(ret_code);
return;
}
cout << "avcodec_open2 success!" << endl;
// 5.输出封装器和视频流配置
// 5.1创建输出封装器上下文
// rtmp flv封装器
AVFormatContext *format_context = nullptr;
ret_code = avformat_alloc_output_context2(&format_context, 0, "flv", out_url);
if (0 != ret_code) {
av_error(ret_code);
return;
}
unique_ptr<AVFormatContext, std::function<void(AVFormatContext *)>> format_context_ptr
(format_context, [](AVFormatContext *context) {
avio_closep(&context->pb);
});
// 5.2添加视频流
AVStream *vs = avformat_new_stream(format_context, NULL);
if (NULL == vs) {
cout << "avformat_new_stream failed." << endl;
return;
}
vs->codecpar->codec_tag = 0;
// 从编码器复制参数
avcodec_parameters_from_context(vs->codecpar, codec_context);
av_dump_format(format_context, 0, out_url, 1);
// 打开rtmp 的网络输出IO
ret_code = avio_open(&format_context->pb, out_url, AVIO_FLAG_WRITE);
if (0 != ret_code) {
av_error(ret_code);
return;
}
// 写入封装头
ret_code = avformat_write_header(format_context, NULL);
if (0 != ret_code) {
av_error(ret_code);
return;
}
AVPacket pack;
memset(&pack, 0, sizeof(pack));
int vpts = 0;
for (;;) {
// 读取rtsp视频帧,解码视频帧
if (! video_ptr->grab()) { continue; }
// yuv转换为rgb
if (! video_ptr->retrieve(frame)) { continue; }
imshow("video", frame);
waitKey(1);
// rgb to yuv
uint8_t *in_data[AV_NUM_DATA_POINTERS] = {0};
in_data[0] = frame.data;
int in_size[AV_NUM_DATA_POINTERS] = {0};
// 一行(宽)数据的字节数
in_size[0] = frame.cols * frame.elemSize();
int h = sws_scale(sws_context, in_data, in_size, 0, frame.rows,
yuv->data, yuv->linesize);
if (h <= 0) { continue; }
// h264编码
yuv->pts = vpts;
vpts++;
ret_code = avcodec_send_frame(codec_context, yuv);
if (0 != ret_code) { continue; }
ret_code = avcodec_receive_packet(codec_context, &pack);
if (0 != ret_code || pack.buf > 0) {
//TODO something
} else { continue; }
// 推流
pack.pts = av_rescale_q(pack.pts, codec_context->time_base, vs->time_base);
pack.dts = av_rescale_q(pack.dts, codec_context->time_base, vs->time_base);
pack.duration = av_rescale_q(pack.duration,
codec_context->time_base,
vs->time_base);
ret_code = av_interleaved_write_frame(format_context, &pack);
if (0 == ret_code) { cout << "#" << flush; }
}
destroyAllWindows();
}
int main(int argc, char *argv[])
{
opencv_rtmp();
getchar();
return 0;
}
接下来,我们看下效果。点击bin目录的player.bat就可以启动ffplay拉流。
画面比较黑,是因为灯关闭了
项目地址:https://github.com/MingYueRuYa/FFmpeg-RTMP
转载请注明来源,欢迎对文章中的引用来源进行考证,欢迎指出任何有错误或不够清晰的表达。可以在下面评论区评论,也可以邮件至 635672377@qq.com
文章标题:Qt开源小项目-VCProject-rc文件的版本号修改-md/小白学习音视频(五)OpenCV获取USB Camera画面推流nginx-rtmp
文章字数:1.1k
本文作者:刘世雄
发布时间:2020-06-23, 13:15:35
最后更新:2020-06-23, 13:15:35
原始链接:http://lsxcpp.com/2020/06/23/Qt%E5%BC%80%E6%BA%90%E5%B0%8F%E9%A1%B9%E7%9B%AE-VCProject-rc%E6%96%87%E4%BB%B6%E7%9A%84%E7%89%88%E6%9C%AC%E5%8F%B7%E4%BF%AE%E6%94%B9-md/%E5%B0%8F%E7%99%BD%E5%AD%A6%E4%B9%A0%E9%9F%B3%E8%A7%86%E9%A2%91%EF%BC%88%E4%BA%94%EF%BC%89OpenCV%E8%8E%B7%E5%8F%96USB%20Camera%E7%94%BB%E9%9D%A2%E6%8E%A8%E6%B5%81nginx-rtmp/版权声明: "署名-非商用-相同方式共享 4.0" 转载请保留原文链接及作者。