你好,下面是一个基于 Qt 和 OpenCV 的 RTMP 推流示例:
首先,需要安装 ffmpeg 和 librtmp 库,这里以 Ubuntu 为例,使用以下命令进行安装:
```
sudo apt-get install libavcodec-dev libavformat-dev libavutil-dev libswscale-dev libavdevice-dev libavfilter-dev libavresample-dev librtmp-dev
```
然后,在 Qt 工程中添加以下头文件:
```cpp
#include <opencv2/opencv.hpp>
#include <libavcodec/avcodec.h>
#include <libavformat/avformat.h>
#include <libavutil/avutil.h>
#include <libswscale/swscale.h>
#include <libavdevice/avdevice.h>
#include <libavfilter/avfilter.h>
#include <libavresample/avresample.h>
#include <librtmp/rtmp.h>
```
接着,定义 RTMP 推流的相关参数:
```cpp
#define RTMP_URL "rtmp://xxxxx" // RTMP 服务器地址
#define WIDTH 640 // 视频宽度
#define HEIGHT 480 // 视频高度
#define FPS 25 // 视频帧率
#define BITRATE 500000 // 视频比特率
```
然后,初始化 ffmpeg 和 librtmp:
```cpp
av_register_all();
avformat_network_init();
avdevice_register_all();
avcodec_register_all();
avfilter_register_all();
av_log_set_level(AV_LOG_INFO);
```
接着,打开摄像头并设置参数:
```cpp
cv::VideoCapture cap(0);
cap.set(CV_CAP_PROP_FRAME_WIDTH, WIDTH);
cap.set(CV_CAP_PROP_FRAME_HEIGHT, HEIGHT);
cap.set(CV_CAP_PROP_FPS, FPS);
```
然后,创建 RTMP 推流句柄并连接 RTMP 服务器:
```cpp
RTMP *rtmp = RTMP_Alloc();
RTMP_Init(rtmp);
RTMP_SetupURL(rtmp, RTMP_URL);
RTMP_EnableWrite(rtmp);
if (!RTMP_Connect(rtmp, NULL) || !RTMP_ConnectStream(rtmp, 0)) {
qDebug() << "RTMP connect error";
return -1;
}
```
接着,创建 AVFrame 和 AVPacket 并初始化:
```cpp
AVFrame *frame = av_frame_alloc();
AVPacket *packet = av_packet_alloc();
memset(packet, 0, sizeof(AVPacket));
```
然后,设置帧参数:
```cpp
frame->width = WIDTH;
frame->height = HEIGHT;
frame->format = AV_PIX_FMT_BGR24;
av_frame_get_buffer(frame, 0);
```
接着,开始推流:
```cpp
while (true) {
cap >> frame;
if (frame.empty()) {
break;
}
frame->pts = static_cast<int64_t>(av_gettime() / 1000.0 / 1000.0 * FPS);
AVCodecContext *codec_context = rtmp->streams[0]->codec;
codec_context->codec_tag = 0;
codec_context->codec_id = AV_CODEC_ID_H264;
codec_context->bit_rate = BITRATE;
codec_context->width = WIDTH;
codec_context->height = HEIGHT;
codec_context->time_base.num = 1;
codec_context->time_base.den = FPS;
codec_context->gop_size = FPS * 2;
codec_context->pix_fmt = AV_PIX_FMT_YUV420P;
codec_context->max_b_frames = 0;
codec_context->flags |= AV_CODEC_FLAG_GLOBAL_HEADER;
AVCodec *codec = avcodec_find_encoder(codec_context->codec_id);
if (!codec) {
qDebug() << "Codec not found";
return -1;
}
if (avcodec_open2(codec_context, codec, NULL) < 0) {
qDebug() << "Could not open codec";
return -1;
}
AVPixelFormat pixel_format = static_cast<AVPixelFormat>(frame->format);
int width = frame->width;
int height = frame->height;
AVFrame *frame_yuv = av_frame_alloc();
frame_yuv->format = AV_PIX_FMT_YUV420P;
frame_yuv->width = width;
frame_yuv->height = height;
av_frame_get_buffer(frame_yuv, 0);
SwsContext *sws_context = sws_getContext(width, height, pixel_format, width, height, AV_PIX_FMT_YUV420P, SWS_BICUBIC, NULL, NULL, NULL);
sws_scale(sws_context, frame->data, frame->linesize, 0, height, frame_yuv->data, frame_yuv->linesize);
avcodec_send_frame(codec_context, frame_yuv);
av_frame_unref(frame_yuv);
int ret = avcodec_receive_packet(codec_context, packet);
if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF) {
continue;
} else if (ret < 0) {
qDebug() << "Error during encoding";
return -1;
}
packet->stream_index = rtmp->streams[0]->id;
RTMP_SendPacket(rtmp, packet, 1);
av_packet_unref(packet);
avcodec_free_context(&codec_context);
}
```
最后,关闭推流句柄和摄像头:
```cpp
RTMP_Close(rtmp);
RTMP_Free(rtmp);
cap.release();
```
完整的 Qt 工程代码如下所示:
```cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <opencv2/opencv.hpp>
#include <libavcodec/avcodec.h>
#include <libavformat/avformat.h>
#include <libavutil/avutil.h>
#include <libswscale/swscale.h>
#include <libavdevice/avdevice.h>
#include <libavfilter/avfilter.h>
#include <libavresample/avresample.h>
#include <librtmp/rtmp.h>
#define RTMP_URL "rtmp://xxxxx" // RTMP 服务器地址
#define WIDTH 640 // 视频宽度
#define HEIGHT 480 // 视频高度
#define FPS 25 // 视频帧率
#define BITRATE 500000 // 视频比特率
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
av_register_all();
avformat_network_init();
avdevice_register_all();
avcodec_register_all();
avfilter_register_all();
av_log_set_level(AV_LOG_INFO);
cv::VideoCapture cap(0);
cap.set(CV_CAP_PROP_FRAME_WIDTH, WIDTH);
cap.set(CV_CAP_PROP_FRAME_HEIGHT, HEIGHT);
cap.set(CV_CAP_PROP_FPS, FPS);
RTMP *rtmp = RTMP_Alloc();
RTMP_Init(rtmp);
RTMP_SetupURL(rtmp, RTMP_URL);
RTMP_EnableWrite(rtmp);
if (!RTMP_Connect(rtmp, NULL) || !RTMP_ConnectStream(rtmp, 0)) {
qDebug() << "RTMP connect error";
return;
}
AVFrame *frame = av_frame_alloc();
AVPacket *packet = av_packet_alloc();
memset(packet, 0, sizeof(AVPacket));
frame->width = WIDTH;
frame->height = HEIGHT;
frame->format = AV_PIX_FMT_BGR24;
av_frame_get_buffer(frame, 0);
while (true) {
cap >> frame;
if (frame.empty()) {
break;
}
frame->pts = static_cast<int64_t>(av_gettime() / 1000.0 / 1000.0 * FPS);
AVCodecContext *codec_context = rtmp->streams[0]->codec;
codec_context->codec_tag = 0;
codec_context->codec_id = AV_CODEC_ID_H264;
codec_context->bit_rate = BITRATE;
codec_context->width = WIDTH;
codec_context->height = HEIGHT;
codec_context->time_base.num = 1;
codec_context->time_base.den = FPS;
codec_context->gop_size = FPS * 2;
codec_context->pix_fmt = AV_PIX_FMT_YUV420P;
codec_context->max_b_frames = 0;
codec_context->flags |= AV_CODEC_FLAG_GLOBAL_HEADER;
AVCodec *codec = avcodec_find_encoder(codec_context->codec_id);
if (!codec) {
qDebug() << "Codec not found";
return;
}
if (avcodec_open2(codec_context, codec, NULL) < 0) {
qDebug() << "Could not open codec";
return;
}
AVPixelFormat pixel_format = static_cast<AVPixelFormat>(frame->format);
int width = frame->width;
int height = frame->height;
AVFrame *frame_yuv = av_frame_alloc();
frame_yuv->format = AV_PIX_FMT_YUV420P;
frame_yuv->width = width;
frame_yuv->height = height;
av_frame_get_buffer(frame_yuv, 0);
SwsContext *sws_context = sws_getContext(width, height, pixel_format, width, height, AV_PIX_FMT_YUV420P, SWS_BICUBIC, NULL, NULL, NULL);
sws_scale(sws_context, frame->data, frame->linesize, 0, height, frame_yuv->data, frame_yuv->linesize);
avcodec_send_frame(codec_context, frame_yuv);
av_frame_unref(frame_yuv);
int ret = avcodec_receive_packet(codec_context, packet);
if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF) {
continue;
} else if (ret < 0) {
qDebug() << "Error during encoding";
return;
}
packet->stream_index = rtmp->streams[0]->id;
RTMP_SendPacket(rtmp, packet, 1);
av_packet_unref(packet);
avcodec_free_context(&codec_context);
}
RTMP_Close(rtmp);
RTMP_Free(rtmp);
cap.release();
av_frame_free(&frame);
av_packet_free(&packet);
}
MainWindow::~MainWindow()
{
delete ui;
}
```
请注意:这个例程只是一个简单的示例,还需要进行错误处理、内存管理等等。