C++使用ffmpeg實(shí)現(xiàn)rtsp取流的代碼
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)文章
用標(biāo)準(zhǔn)c++實(shí)現(xiàn)string與各種類型之間的轉(zhuǎn)換
這個(gè)類在頭文件中定義, < sstream>庫(kù)定義了三種類:istringstream、ostringstream和stringstream,分別用來(lái)進(jìn)行流的輸入、輸出和輸入輸出操作。另外,每個(gè)類都有一個(gè)對(duì)應(yīng)的寬字符集版本2013-09-09c++實(shí)現(xiàn)十進(jìn)制轉(zhuǎn)換成16進(jìn)制示例
這篇文章主要介紹了c++實(shí)現(xiàn)十進(jìn)制轉(zhuǎn)換成16進(jìn)制示例,需要的朋友可以參考下2014-05-05Qt?多語(yǔ)言程序設(shè)計(jì)的實(shí)現(xiàn)
本文主要介紹了Qt?多語(yǔ)言程序設(shè)計(jì)的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-01-01c++優(yōu)先隊(duì)列用法知識(shí)點(diǎn)總結(jié)
在本篇文章里小編給大家整理的是關(guān)于c++優(yōu)先隊(duì)列用法知識(shí)點(diǎn)總結(jié)內(nèi)容,需要的朋友可以參考學(xué)習(xí)下。2020-02-02C++ new與malloc和delete及free動(dòng)態(tài)內(nèi)存管理及區(qū)別介紹
這篇文章主要介紹了深入理解C++中的new/delete和malloc/free動(dòng)態(tài)內(nèi)存管理,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-12-12詳解C/C++中低耦合代碼的設(shè)計(jì)實(shí)現(xiàn)
這篇文章主要為大家詳細(xì)介紹了C/C++?相關(guān)低耦合代碼的設(shè)計(jì)實(shí)現(xiàn),文中的示例代碼講解詳細(xì),對(duì)我們學(xué)習(xí)C++有一定的幫助,感興趣的小伙伴可以了解一下2023-01-01