日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學無先后,達者為師

網站首頁 編程語言 正文

C++ 使用ffmpeg實現rtsp取流

作者:TheOldManAndTheSea 更新時間: 2022-05-13 編程語言

C++ 使用ffmpeg實現rtsp取流

flyfish

環境

Ubuntu 18.04
Qt 5.14.2
FFmpeg-n5.0.1

下載

https://git.ffmpeg.org/ffmpeg.git
https://github.com/FFmpeg/FFmpeg

這里選擇n5.0.1版本

安裝編譯依賴

sudo apt-get install nasm

配置

生成包括靜態和動態庫
頭文件和庫都在當前的install文件夾中

FFmpeg-n5.0.1$  ./configure --prefix="./install"  --enable-shared

再執行

make
make install

在install文件夾中的include
在這里插入圖片描述
在install文件夾中的lib
在這里插入圖片描述

ffmepg采用rtsp取流流程圖

在這里插入圖片描述

CMakeLists.txt編寫方法

cmake_minimum_required(VERSION 3.5)

project(rtsp LANGUAGES CXX)

set(CMAKE_INCLUDE_CURRENT_DIR ON)

set(CMAKE_AUTOUIC ON)
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTORCC ON)

set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

find_package(Qt5Core)

set(FFMPEG_PREFIX_PATH /path/to/FFmpeg-n5.0.1/install)

include_directories(
    ${FFMPEG_PREFIX_PATH}/include/
)

link_directories(
    ${FFMPEG_PREFIX_PATH}/lib/ )

add_executable(rtsp
  main.cpp
)
target_link_libraries(rtsp avcodec avformat avfilter avutil swresample swscale swscale )

實現代碼

#include 

extern "C" {
#include "libavcodec/avcodec.h"
#include "libavformat/avformat.h"
#include "libavutil/avutil.h"
}

int main(int argc, char *argv[])
{

    int status_error_=-1;
    std::string videourl= "rtsp://admin:Admin12345@192.168.3.64:554/Streaming/Channels/1";
    AVFormatContext *pFormatCtx = NULL;
    AVDictionary *options = NULL;
    AVPacket *av_packet = NULL; // AVPacket暫存解碼之前的媒體數據


    avformat_network_init();
    //執行網絡庫的全局初始化。
    //此函數僅用于解決舊版GNUTLS或OpenSSL庫的線程安全問題。
    //一旦刪除對較舊的GNUTLS和OpenSSL庫的支持,此函數將被棄用,并且此函數將不再有任何用途。
    av_dict_set(&options, "buffer_size", "4096000", 0); //設置緩存大小
    av_dict_set(&options, "rtsp_transport", "tcp", 0);  //以tcp的方式打開,
    av_dict_set(&options, "stimeout", "5000000", 0);    //設置超時斷開鏈接時間,單位us,   5s
    av_dict_set(&options, "max_delay", "500000", 0);    //設置最大時延

    pFormatCtx = avformat_alloc_context(); //用來申請AVFormatContext類型變量并初始化默認參數,申請的空間

    //打開網絡流或文件流
    if (avformat_open_input(&pFormatCtx, videourl.c_str(), NULL, &options) != 0)
    {

        std::cout << "Couldn't open input stream.\n"
                  << std::endl;

        return status_error_;
    }

    //獲取視頻文件信息
    if (avformat_find_stream_info(pFormatCtx, NULL) < 0)
    {

        std::cout << "Couldn't find stream information."<< std::endl;
        return status_error_;
    }

    std::cout << "av_dict_get:" << std::endl;
    AVDictionaryEntry *tag = NULL;
    //av_dict_set(&pFormatCtx->metadata, "rotate", "0", 0);這里可以設置一些屬性
    while ((tag = av_dict_get(pFormatCtx->metadata, "", tag, AV_DICT_IGNORE_SUFFIX)))
    {
        std::string key = tag->key;
        std::string value = tag->value;
        std::cout << "av_dict_get:" << key << ":" << value << std::endl;
    }


    //查找碼流中是否有視頻流
    int videoindex = -1;
    unsigned i = 0;
    for (i = 0; i < pFormatCtx->nb_streams; i++)
    {
        if (pFormatCtx->streams[i]->codecpar->codec_type == AVMEDIA_TYPE_VIDEO)
        {
            videoindex = i;
            break;
        }
    }
    if (videoindex == -1)
    {

        std::cout << "Didn't find a video stream.\n"
                  << std::endl;

        return status_error_;
    }

    av_packet = (AVPacket *)av_malloc(sizeof(AVPacket));

    while (true)
    {
        if (av_read_frame(pFormatCtx, av_packet) >= 0)
        {

            if (av_packet->stream_index == videoindex)
            {
                std::cout << "\ndata size is:" << av_packet->size;
                //這里就是接收到的未解碼之前的數據

            }
            if (av_packet != NULL)
                av_packet_unref(av_packet);
        }


    }

    av_free(av_packet);
    avformat_close_input(&pFormatCtx);

    return 0;
}

運行可執行文件前,可設置從當前文件夾查找so動態庫

export LD_LIBRARY_PATH=./

結果
在這里插入圖片描述

原文鏈接:https://blog.csdn.net/flyfish1986/article/details/124298473

欄目分類
最近更新