qt c++中ffmpeg推流无法拉流
时间: 2023-12-02 08:04:08 浏览: 124
QT+FFMPEG的推流客户端.7z
5星 · 资源好评率100%
为了在Qt C++中使用FFmpeg进行推流和拉流,您需要遵循以下步骤:
1.安装FFmpeg库并确保其在系统路径中。
2.在Qt项目中包含FFmpeg头文件和库文件。
3.使用FFmpeg API编写推流和拉流代码。
以下是一个简单的Qt C++程序,演示如何使用FFmpeg进行推流和拉流:
```c++
#include <QCoreApplication>
#include <QDebug>
#include <QThread>
#include <QTimer>
extern "C" {
#include <libavcodec/avcodec.h>
#include <libavformat/avformat.h>
#include <libavutil/imgutils.h>
#include <libswscale/swscale.h>
}
// 推流
void pushStream()
{
AVFormatContext *outFormatCtx = NULL;
AVOutputFormat *outFormat = NULL;
AVStream *videoStream = NULL;
AVCodecContext *codecCtx = NULL;
AVCodec *codec = NULL;
AVFrame *frame = NULL;
AVPacket pkt;
int ret = 0;
// 初始化FFmpeg
av_register_all();
avformat_network_init();
// 打开输出流
ret = avformat_alloc_output_context2(&outFormatCtx, NULL, "rtsp", "rtsp://localhost:8554/zyx");
if (ret < 0) {
qDebug() << "Error: avformat_alloc_output_context2 failed";
return;
}
outFormat = outFormatCtx->oformat;
// 添加视频流
videoStream = avformat_new_stream(outFormatCtx, NULL);
if (!videoStream) {
qDebug() << "Error: avformat_new_stream failed";
return;
}
codecCtx = videoStream->codec;
codecCtx->codec_id = outFormat->video_codec;
codecCtx->codec_type = AVMEDIA_TYPE_VIDEO;
codecCtx->pix_fmt = AV_PIX_FMT_YUV420P;
codecCtx->width = 800;
codecCtx->height = 600;
codecCtx->time_base.num = 1;
codecCtx->time_base.den = 25;
codec = avcodec_find_encoder(codecCtx->codec_id);
if (!codec) {
qDebug() << "Error: avcodec_find_encoder failed";
return;
}
ret = avcodec_open2(codecCtx, codec, NULL);
if (ret < 0) {
qDebug() << "Error: avcodec_open2 failed";
return;
}
frame = av_frame_alloc();
frame->format = codecCtx->pix_fmt;
frame->width = codecCtx->width;
frame->height = codecCtx->height;
ret = av_image_alloc(frame->data, frame->linesize, codecCtx->width, codecCtx->height, codecCtx->pix_fmt, 32);
if (ret < 0) {
qDebug() << "Error: av_image_alloc failed";
return;
}
// 打开输出流
ret = avio_open(&outFormatCtx->pb, "rtsp://localhost:8554/zyx", AVIO_FLAG_WRITE);
if (ret < 0) {
qDebug() << "Error: avio_open failed";
return;
}
ret = avformat_write_header(outFormatCtx, NULL);
if (ret < 0) {
qDebug() << "Error: avformat_write_header failed";
return;
}
// 推流
for (int i = 0; i < 100; i++) {
av_init_packet(&pkt);
pkt.data = NULL;
pkt.size = 0;
// 生成测试图像
for (int y = 0; y < codecCtx->height; y++) {
for (int x = 0; x < codecCtx->width; x++) {
frame->data[0][y * frame->linesize[0] + x] = x + y + i * 3;
}
}
frame->pts = i;
ret = avcodec_send_frame(codecCtx, frame);
if (ret < 0) {
qDebug() << "Error: avcodec_send_frame failed";
return;
}
ret = avcodec_receive_packet(codecCtx, &pkt);
if (ret < 0) {
qDebug() << "Error: avcodec_receive_packet failed";
return;
}
pkt.stream_index = videoStream->index;
ret = av_interleaved_write_frame(outFormatCtx, &pkt);
if (ret < 0) {
qDebug() << "Error: av_interleaved_write_frame failed";
return;
}
av_packet_unref(&pkt);
}
av_write_trailer(outFormatCtx);
avcodec_close(codecCtx);
avio_close(outFormatCtx->pb);
avformat_free_context(outFormatCtx);
av_frame_free(&frame);
}
// 拉流
void pullStream()
{
AVFormatContext *inFormatCtx = NULL;
AVCodecContext *codecCtx = NULL;
AVCodec *codec = NULL;
AVFrame *frame = NULL;
AVPacket pkt;
int ret = 0;
// 初始化FFmpeg
av_register_all();
avformat_network_init();
// 打开输入流
ret = avformat_open_input(&inFormatCtx, "rtsp://localhost:8554/zyx", NULL, NULL);
if (ret < 0) {
qDebug() << "Error: avformat_open_input failed";
return;
}
ret = avformat_find_stream_info(inFormatCtx, NULL);
if (ret < 0) {
qDebug() << "Error: avformat_find_stream_info failed";
return;
}
// 查找视频流
int videoStreamIndex = -1;
for (int i = 0; i < inFormatCtx->nb_streams; i++) {
if (inFormatCtx->streams[i]->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) {
videoStreamIndex = i;
break;
}
}
if (videoStreamIndex == -1) {
qDebug() << "Error: video stream not found";
return;
}
codecCtx = avcodec_alloc_context3(NULL);
if (!codecCtx) {
qDebug() << "Error: avcodec_alloc_context3 failed";
return;
}
ret = avcodec_parameters_to_context(codecCtx, inFormatCtx->streams[videoStreamIndex]->codecpar);
if (ret < 0) {
qDebug() << "Error: avcodec_parameters_to_context failed";
return;
}
codec = avcodec_find_decoder(codecCtx->codec_id);
if (!codec) {
qDebug() << "Error: avcodec_find_decoder failed";
return;
}
ret = avcodec_open2(codecCtx, codec, NULL);
if (ret < 0) {
qDebug() << "Error: avcodec_open2 failed";
return;
}
frame = av_frame_alloc();
// 拉流
while (true) {
ret = av_read_frame(inFormatCtx, &pkt);
if (ret < 0) {
qDebug() << "Error: av_read_frame failed";
break;
}
if (pkt.stream_index == videoStreamIndex) {
ret = avcodec_send_packet(codecCtx, &pkt);
if (ret < 0) {
qDebug() << "Error: avcodec_send_packet failed";
break;
}
while (ret >= 0) {
ret = avcodec_receive_frame(codecCtx, frame);
if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF) {
break;
} else if (ret < 0) {
qDebug() << "Error: avcodec_receive_frame failed";
break;
}
// 在UI界面播放视频数据
// ...
av_frame_unref(frame);
}
}
av_packet_unref(&pkt);
}
avcodec_close(codecCtx);
avformat_close_input(&inFormatCtx);
av_frame_free(&frame);
}
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
// 推流
QThread pushThread;
QObject::connect(&pushThread, &QThread::started, [](){
pushStream();
});
pushThread.start();
// 拉流
QTimer pullTimer;
QObject::connect(&pullTimer, &QTimer::timeout, [](){
pullStream();
});
pullTimer.start(1000);
return a.exec();
}
```
阅读全文