欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

C++使用ffmpeg實(shí)現(xiàn)rtsp取流的代碼

 更新時(shí)間:2022年04月21日 15:45:43   作者:TheOldManAndTheSea  
這篇文章主要介紹了C++使用ffmpeg實(shí)現(xiàn)rtsp取流,文章介紹了ffmepg采用rtsp取流流程圖,CMakeLists.txt編寫方法,通過(guò)示例代碼給大家介紹的非常詳細(xì),需要的朋友可以參考下

C++ 使用ffmpeg實(shí)現(xiàn)rtsp取流

flyfish

環(huán)境

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

配置

生成包括靜態(tài)和動(dòng)態(tài)庫(kù)
頭文件和庫(kù)都在當(dāng)前的install文件夾中

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

再執(zhí)行

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 )

實(shí)現(xiàn)代碼

#include <iostream>

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暫存解碼之前的媒體數(shù)據(jù)
    avformat_network_init();
    //執(zhí)行網(wǎng)絡(luò)庫(kù)的全局初始化。
    //此函數(shù)僅用于解決舊版GNUTLS或OpenSSL庫(kù)的線程安全問(wèn)題。
    //一旦刪除對(duì)較舊的GNUTLS和OpenSSL庫(kù)的支持,此函數(shù)將被棄用,并且此函數(shù)將不再有任何用途。
    av_dict_set(&options, "buffer_size", "4096000", 0); //設(shè)置緩存大小
    av_dict_set(&options, "rtsp_transport", "tcp", 0);  //以tcp的方式打開(kāi),
    av_dict_set(&options, "stimeout", "5000000", 0);    //設(shè)置超時(shí)斷開(kāi)鏈接時(shí)間,單位us,   5s
    av_dict_set(&options, "max_delay", "500000", 0);    //設(shè)置最大時(shí)延
    pFormatCtx = avformat_alloc_context(); //用來(lái)申請(qǐng)AVFormatContext類型變量并初始化默認(rèn)參數(shù),申請(qǐng)的空間
    //打開(kāi)網(wǎng)絡(luò)流或文件流
    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;
    std::cout << "av_dict_get:" << std::endl;
    AVDictionaryEntry *tag = NULL;
    //av_dict_set(&pFormatCtx->metadata, "rotate", "0", 0);這里可以設(shè)置一些屬性
    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"
    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;
                //這里就是接收到的未解碼之前的數(shù)據(jù)
            }
            if (av_packet != NULL)
                av_packet_unref(av_packet);
    av_free(av_packet);
    avformat_close_input(&pFormatCtx);
    return 0;

運(yùn)行可執(zhí)行文件前,可設(shè)置從當(dāng)前文件夾查找so動(dòng)態(tài)庫(kù)

export LD_LIBRARY_PATH=./

結(jié)果

在這里插入圖片描述

到此這篇關(guān)于C++使用ffmpeg實(shí)現(xiàn)rtsp取流的文章就介紹到這了,更多相關(guān)C++ rtsp取流內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論