How to use ffmpeg to realize rtsp stream fetch in C++
Today, the editor will share with you the relevant knowledge points about how C++ uses ffmpeg to achieve rtsp streaming. The content is detailed and the logic is clear. I believe most people still know too much about this knowledge, so share this article for your reference. I hope you can get something after reading this article.
C++ uses ffmpeg to realize rtsp stream fetching
Flyfish
Environment
Ubuntu 18.04
Qt 5.14.2
FFmpeg-n5.0.1
download
Choose version n5.0.1 here.
Installation and compilation depends on sudo apt-get install nasm configuration
Generation includes static and dynamic libraries
Header files and libraries are in the current install folder
FFmpeg-n5.0.1 $. / configure-- prefix= ". / install"-- enable-shared
Re-execution
Makemake install
Include in the install folder
Lib in the install folder
Ffmepg uses rtsp to fetch flow chart
CMakeLists.txt writing method 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.cpptarget_link_libraries (rtsp avcodec avformat avfilter avutil swresample swscale swscale) implementation code # 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 temporarily stores the media data before decoding avformat_network_init (); / / performs global initialization of the network library. / / this function is only used to solve thread safety problems with older GNUTLS or OpenSSL libraries. / / once support for older GNUTLS and OpenSSL libraries is removed, this function will be deprecated and will no longer be of any use. Av_dict_set (& options, "buffer_size", "4096000", 0); / set cache size av_dict_set (& options, "rtsp_transport", "tcp", 0); / / Open it as tcp, av_dict_set (& options, "stimeout", "5000000", 0) / / set timeout disconnection time, in us, 5s av_dict_set (& options, "max_delay", "500000", 0); / / set maximum delay pFormatCtx = avformat_alloc_context () / / used to apply for AVFormatContext type variables and initialize default parameters. The requested space / / opens the network stream or file stream if (avformat_open_input (& pFormatCtx, videourl.c_str (), NULL, & options)! = 0) {std::cout