ffmpeg解封装rtsp并录制视频-(3)完成对rtsp流通过责任链接收解封装AVPacket数据并解码视频再渲染视频

30 篇文章 1 订阅
6 篇文章 0 订阅

主函数:

//108录制五秒视频
#include <iostream>
#include <thread>
#include "xtools.h"
#include "xdemux_task.h"
#include "xdecodetask.h"
#include "xvideo_view.h"
#include "xmux_task.h"
using namespace std;

//大华
#define CAM1 \
"rtsp://admin:admin@192.168.2.108/cam/realmonitor?channel=1&subtype=0"
//海康
#define CAM2 \
"rtsp://127.0.0.1:8554/test"
int main(int argc, char* argv[])
{
    XDemuxTask demux_task;
    for (;;)
    {
        if (demux_task.Open(CAM2))
        {
            cout << "shexiangtoudakaile!!!!";
            break;
        }
        MSleep(100);
        continue;
    }

    auto vpara = demux_task.CopyVideoPara();
    AVCodecParameters* video_para = nullptr;
    AVCodecParameters* audio_para = nullptr;
    AVRational* video_time_base = nullptr;
    AVRational* audio_time_base = nullptr;
    if (vpara)
    {
        video_para = vpara->para;
        video_time_base = vpara->time_base;
    }
    auto apara = demux_task.CopyAudioPara();
    if (apara)
    {
        audio_para = apara->para;
        audio_time_base = apara->time_base;
    }
    XMuxTask mux_task;
    if (!mux_task.Open("rtsp_out1.mp4", video_para, video_time_base,
        audio_para, audio_time_base
    ))
    {
        LOGERROR("mux_task.Open failed!");
        return -1;
    }
    demux_task.set_next(&mux_task);
    demux_task.Start();
    mux_task.Start();
    MSleep(5000);
    mux_task.Stop();

    if (!mux_task.Open("rtsp_out2.mp4", video_para, video_time_base,
        audio_para, audio_time_base
    ))
    {
        LOGERROR("mux_task.Open failed!");
        return -1;
    }
    mux_task.Start();
    MSleep(5000);
    mux_task.Stop();

    getchar();
    return 0;
}

 

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
使用 C++ 解码 RTSP 视频流需要使用 FFmpeg 库。以下是一个简单的示例代码: ``` extern "C" { #include <libavcodec/avcodec.h> #include <libavformat/avformat.h> #include <libavutil/imgutils.h> } int main() { av_register_all(); avformat_network_init(); AVFormatContext* pFormatCtx = NULL; AVCodecContext* pCodecCtx = NULL; AVCodec* pCodec = NULL; AVPacket packet; AVFrame* pFrame = NULL; const char* url = "rtsp://example.com/stream"; if (avformat_open_input(&pFormatCtx, url, NULL, NULL) != 0) { return -1; } if (avformat_find_stream_info(pFormatCtx, NULL) < 0) { 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) { return -1; } pCodecCtx = avcodec_alloc_context3(NULL); avcodec_parameters_to_context(pCodecCtx, pFormatCtx->streams[videoStream]->codecpar); pCodec = avcodec_find_decoder(pCodecCtx->codec_id); if (pCodec == NULL) { return -1; } if (avcodec_open2(pCodecCtx, pCodec, NULL) < 0) { return -1; } pFrame = av_frame_alloc(); while (av_read_frame(pFormatCtx, &packet) >= 0) { if (packet.stream_index == videoStream) { if (avcodec_send_packet(pCodecCtx, &packet) < 0) { break; } while (avcodec_receive_frame(pCodecCtx, pFrame) == 0) { // 处理解码后的图像数据 } } av_packet_unref(&packet); } avformat_close_input(&pFormatCtx); avcodec_free_context(&pCodecCtx); av_frame_free(&pFrame); return 0; } ``` 上述代码实现了 RTSP 视频流的解码,并可以通过处理解码后的图像数据实现各种功能。需要注意的是,该代码仅为示例代码,实际使用时需要根据具体情况进行修改和优化。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值