3、FFMPEG拉流转OpenCV并推流& Opencv 读视频转FFMPEG并推流

基本想法:最近看同事将android手机的视频流推到了服务器上,感觉挺有意思的,逐决定学习一下~

下载官方提供好的FFMPEG:https://www.gyan.dev/ffmpeg/builds/ffmpeg-release-full-shared.7z

1、解压之后,添加bin路径到window10的环境变量中~

C:\Users\sxj>ffmpeg
ffmpeg version 4.4-full_build-www.gyan.dev Copyright (c) 2000-2021 the FFmpeg developers
  built with gcc 10.2.0 (Rev6, Built by MSYS2 project)
  configuration: --enable-gpl --enable-version3 --enable-shared --disable-w32threads --disable-autodetect --enable-fontconfig --enable-iconv --enable-gnutls --enable-libxml2 --enable-gmp --enable-lzma --enable-libsnappy --enable-zlib --enable-librist --enable-libsrt --enable-libssh --enable-libzmq --enable-avisynth --enable-libbluray --enable-libcaca --enable-sdl2 --enable-libdav1d --enable-libzvbi --enable-librav1e --enable-libsvta
  • 2
    点赞
  • 27
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
敬告:该系列的课程在抓紧录制更新中,敬请大家关注。敬告: 该系列的课程涉及:FFmpeg,WebRTC,SRS,Nginx,Darwin,Live555,OpenCV,等。包括:音视频、流媒体、直播、Android、视频监控28181、等。 我将带领大家一起来学习OpenCV4的图像处理原理和编程知识,并动手操练58案例代码。具体内容包括: 一、小白入门与初体验:禁果尝鲜二、图像基本操作:懵懵懂懂学图像三、图像统计操作:七七八八有收获四、图像卷积:不入虎穴焉得虎子五、磨皮美颜:柳暗花明又一村六、二值图像:阴阳合一法自然七、图像形态学:登高望远天地阔 音视频与流媒体是一门很复杂的技术,涉及的概念、原理、理论非常多,很多初学者不学 基础理论,而是直接做项目,往往会看到c/c++的代码时一头雾水,不知道代码到底是什么意思,这是为什么呢? 因为没有学习音视频和流媒体的基础理论,就比如学习英语,不学习基本单词,而是天天听英语新闻,总也听不懂。所以呢,一定要认真学习基础理论,然后再学习播放器、码器、非编、流媒体直播、视频监控、等等。 梅老师从事音视频与流媒体行业18年;曾在永新视博、中科大洋、百度、美国Harris广播事业部等公司就职,经验丰富;曾亲手主导广电直播全套项目,精通h.264/h.265/aac,曾亲自参与百度app上的网页播放器等实战产品。目前全身心自主创业,主要聚焦音视频+流媒体行业,精通音视频加密、流媒体在线码快编等热门产品。     
可以使用FFmpegOpenCV来进行RTSP流和推流。 首先,需要使用FFmpeg进行RTSP流。可以使用以下代码来进行流: ```c++ #include <iostream> #include <opencv2/opencv.hpp> extern "C" { #include <libavformat/avformat.h> #include <libavcodec/avcodec.h> #include <libswscale/swscale.h> } int main(int argc, char* argv[]) { av_register_all(); AVFormatContext* pFormatCtx = nullptr; if (avformat_open_input(&pFormatCtx, "rtsp://your_rtsp_url", nullptr, nullptr) != 0) { std::cerr << "Failed to open input stream!" << std::endl; return -1; } if (avformat_find_stream_info(pFormatCtx, nullptr) < 0) { std::cerr << "Failed to retrieve stream information!" << std::endl; return -1; } int videoStream = -1; for (int i = 0; i < pFormatCtx->nb_streams; i++) { if (pFormatCtx->streams[i]->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) { videoStream = i; break; } } if (videoStream == -1) { std::cerr << "Failed to find video stream!" << std::endl; return -1; } AVCodecParameters* pCodecParams = pFormatCtx->streams[videoStream]->codecpar; AVCodec* pCodec = avcodec_find_decoder(pCodecParams->codec_id); if (pCodec == nullptr) { std::cerr << "Failed to find codec!" << std::endl; return -1; } AVCodecContext* pCodecCtx = avcodec_alloc_context3(pCodec); if (pCodecCtx == nullptr) { std::cerr << "Failed to allocate codec context!" << std::endl; return -1; } if (avcodec_parameters_to_context(pCodecCtx, pCodecParams) < 0) { std::cerr << "Failed to copy codec parameters to codec context!" << std::endl; return -1; } if (avcodec_open2(pCodecCtx, pCodec, nullptr) < 0) { std::cerr << "Failed to open codec!" << std::endl; return -1; } AVFrame* pFrame = av_frame_alloc(); if (pFrame == nullptr) { std::cerr << "Failed to allocate frame!" << std::endl; return -1; } AVPacket* pPacket = av_packet_alloc(); if (pPacket == nullptr) { std::cerr << "Failed to allocate packet!" << std::endl; return -1; } while (av_read_frame(pFormatCtx, pPacket) >= 0) { if (pPacket->stream_index == videoStream) { if (avcodec_send_packet(pCodecCtx, pPacket) < 0) { std::cerr << "Error sending a packet for decoding!" << std::endl; break; } while (avcodec_receive_frame(pCodecCtx, pFrame) == 0) { // Use OpenCV to display the frame cv::Mat matFrame(pFrame->height, pFrame->width, CV_8UC3, pFrame->data[0], pFrame->linesize[0]); cv::imshow("Frame", matFrame); cv::waitKey(1); } } av_packet_unref(pPacket); } av_packet_free(&pPacket); av_frame_free(&pFrame); avcodec_free_context(&pCodecCtx); avformat_close_input(&pFormatCtx); return 0; } ``` 然后,可以使用FFmpeg进行推流。可以使用以下代码来进行推流: ```c++ #include <iostream> #include <opencv2/opencv.hpp> extern "C" { #include <libavformat/avformat.h> #include <libavcodec/avcodec.h> #include <libswscale/swscale.h> } int main(int argc, char* argv[]) { av_register_all(); AVFormatContext* pFormatCtx = nullptr; if (avformat_alloc_output_context2(&pFormatCtx, nullptr, "flv", "rtmp://your_rtmp_url") < 0) { std::cerr << "Failed to allocate output context!" << std::endl; return -1; } AVOutputFormat* pOutputFormat = pFormatCtx->oformat; if (avio_open(&pFormatCtx->pb, "rtmp://your_rtmp_url", AVIO_FLAG_WRITE) < 0) { std::cerr << "Failed to open output URL!" << std::endl; return -1; } AVCodec* pCodec = avcodec_find_encoder(AV_CODEC_ID_H264); if (pCodec == nullptr) { std::cerr << "Failed to find encoder!" << std::endl; return -1; } AVStream* pStream = avformat_new_stream(pFormatCtx, pCodec); if (pStream == nullptr) { std::cerr << "Failed to create new stream!" << std::endl; return -1; } AVCodecContext* pCodecCtx = avcodec_alloc_context3(pCodec); if (pCodecCtx == nullptr) { std::cerr << "Failed to allocate codec context!" << std::endl; return -1; } if (avcodec_parameters_to_context(pCodecCtx, pStream->codecpar) < 0) { std::cerr << "Failed to copy codec parameters to codec context!" << std::endl; return -1; } pCodecCtx->codec_id = AV_CODEC_ID_H264; pCodecCtx->codec_type = AVMEDIA_TYPE_VIDEO; pCodecCtx->pix_fmt = AV_PIX_FMT_YUV420P; pCodecCtx->width = 640; pCodecCtx->height = 480; pCodecCtx->time_base = { 1, 25 }; pCodecCtx->bit_rate = 400000; pCodecCtx->gop_size = 10; if (avcodec_open2(pCodecCtx, pCodec, nullptr) < 0) { std::cerr << "Failed to open codec!" << std::endl; return -1; } AVFrame* pFrame = av_frame_alloc(); if (pFrame == nullptr) { std::cerr << "Failed to allocate frame!" << std::endl; return -1; } pFrame->format = pCodecCtx->pix_fmt; pFrame->width = pCodecCtx->width; pFrame->height = pCodecCtx->height; if (av_frame_get_buffer(pFrame, 0) < 0) { std::cerr << "Failed to allocate picture!" << std::endl; return -1; } AVPacket* pPacket = av_packet_alloc(); if (pPacket == nullptr) { std::cerr << "Failed to allocate packet!" << std::endl; return -1; } int gotOutput = 0; int frameCount = 0; while (frameCount < 10000) { cv::Mat matFrame = cv::imread("your_image_path"); AVFrame* pFrameYUV = av_frame_alloc(); if (pFrameYUV == nullptr) { std::cerr << "Failed to allocate YUV frame!" << std::endl; return -1; } av_image_alloc(pFrameYUV->data, pFrameYUV->linesize, pCodecCtx->width, pCodecCtx->height, pCodecCtx->pix_fmt, 1); SwsContext* pSwsCtx = sws_getContext(pCodecCtx->width, pCodecCtx->height, AV_PIX_FMT_BGR24, pCodecCtx->width, pCodecCtx->height, pCodecCtx->pix_fmt, SWS_BICUBIC, nullptr, nullptr, nullptr); sws_scale(pSwsCtx, (uint8_t const* const*)matFrame.data, matFrame.step, 0, pCodecCtx->height, pFrameYUV->data, pFrameYUV->linesize); pFrameYUV->pts = frameCount * pStream->time_base.den / pStream->time_base.num / 25; if (avcodec_send_frame(pCodecCtx, pFrameYUV) < 0) { std::cerr << "Error sending a frame for encoding!" << std::endl; break; } while (avcodec_receive_packet(pCodecCtx, pPacket) == 0) { pPacket->stream_index = pStream->index; av_interleaved_write_frame(pFormatCtx, pPacket); av_packet_unref(pPacket); } av_frame_free(&pFrameYUV); frameCount++; } av_write_trailer(pFormatCtx); av_packet_free(&pPacket); av_frame_free(&pFrame); avcodec_free_context(&pCodecCtx); avformat_free_context(pFormatCtx); return 0; } ``` 需要注意的是,这里的代码只是提供了一个基本的框架,需要根据实际情况进行修改和适配。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

sxj731533730

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

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

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

打赏作者

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

抵扣说明:

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

余额充值