ffmpeg推流和拉流rtsp

参考博客

目录

一、搭建本地服务器

二、ffmpeg推流rtsp

 三、ffplay播放


参考博客

一、搭建本地服务器

使用EasyDarwin,下载地址Releases · EasyDarwin/EasyDarwin (github.com)

双击运行后,不要关闭命令行窗口,在浏览器输入 http://localhost:10008 ,然后点击右上角登陆,账户密码admin/admin

二、ffmpeg推流rtsp

ffmpeg -re -i 1.mp4 -vcodec copy -codec copy -f rtsp rtsp://127.0.0.1:554/stream

推摄像头视频

ffmpeg -f dshow -i video="Integrated Camera" -vcodec libx264 -preset:v ultrafast -tune:v zerolatency -rtsp_transport udp -f rtsp rtsp://127.0.0.1/stream

 三、ffplay播放

ffplay rtsp://127.0.0.1:554/stream

 

  • 2
    点赞
  • 25
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 3
    评论
为了在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(); } ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

aspiretop

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值