Location>code7788 >text

FFmpeg development notes (forty) Nginx integration rtmp module to achieve RTMP push-pull streaming

Popularity:553 ℃/2024-07-21 19:49:13

The book "10.2.2 FFmpeg Push Streaming to the Network" in the book "FFmpeg Development in Action: From Zero Basis to Short Video Online" introduces a lightweight streaming media server, MediaMTX, which is very simple to use, but it can't satisfy the complex business requirements. Therefore, it is necessary to introduce a professional streaming media server in practical applications.
nginx-rtmp is an open source WEB server Nginx can be enhanced third-party rtmp module , the module encapsulates the rtmp server , you can provide a simple rtmp streaming media server functions . As Nginx itself is a high-performance WEB server , support for proxy distribution of data for a variety of protocols , so through Nginx integrated RTMP server is also a viable option.Nginx default did not open the rtmp module , you need to add the rtmp module in the compilation of nginx , but also to modify the configuration file in order to open the rtmp service .
Because nginx + rtmp in the Windows environment compilation process is very troublesome, not only to consider a variety of dependent packages version compatibility issues, but also pay attention to MSYS and Visual Studio command line switching issues, so here is only about how to integrate the rtmp module in the Linux environment to the Ngixn integration of the detailed integration steps are described below.

I. Preparing the source code for Nginx and nginx-rtmp-module

The official website address of Nginx is /, the download page is /en/, and the latest version is nginx-1.26.0 which was released in April 2024, and the download link for the source package of this version is /download/nginx-1.26.The latest version is nginx-1.26.0 which was released in April 2024, and the download link for the source package of this version is /download/nginx-1.26.
The source code for nginx-rtmp-module is hosted at /arut/nginx-rtmp-module, the download page for each version is /arut/nginx-rtmp-module/tags, and the latest version is the April 2024 release of nginx-rtmp-module-1.2.2, the The source package download link is /arut/nginx-rtmp-module/archive/refs/tags/v1.2.
Once the above source packages are downloaded, upload them to the /usr/local/src directory of your Linux server, where they will be subsequently extracted.

Second, compile and install Nginx (including integrated rtmp module)

Log in to the Linux server and execute the following unpacking commands in order to unpack the source packages of nginx-rtmp-module, nginx, etc. respectively.

cd /usr/local/src
tar zxvf nginx-rtmp-module-1.2.
tar zxvf nginx-1.26.

Next, go to the nginx source directory and execute the following command to configure nginx.

cd nginx-1.26.0
./configure --prefix=/usr/local/nginx --with-http_ssl_module --add-module=../nginx-rtmp-module-1.2.2

Once the configuration is complete, execute the following command to compile nginx.

make -j4

After compiling, execute the following command to install nginx.

make install

Third, start the RTMP service of Nginx

Open conf/ under the nginx installation path and add the following rtmp configuration at the end of the file, specifying the port number and service name on which the rtmp protocol listens.

rtmp {
    server {
        # rtmpProtocol port number
        listen 1935;
        # rtmpService name of the protocol
        application live {
            live on;
        }
    }
}

Once added, save and exit. Then run the following command to start nginx.

cd /usr/local/nginx/sbin
./nginx

Fourth, check if nginx-rtmp is running properly

The following ffmpeg video files to the rtmp service to push the stream, that is, through the following command to rtmp://127.0.0.1/live/test to push the stream of video files. For detailed usage of ffmpeg, please refer to the book "FFmpeg Development in Action: From Zero Basics to Short Video Online", so I won't repeat it here.

ffmpeg -re -stream_loop -1 -i "/usr/local/src/test/2018s.mp4" -vcodec h264 -f flv rtmp://127.0.0.1/live/test

Then start the streaming media player VLC media player on the computer, open the network stream "rtmp:///live/test", it can be seen that VLC media player normally plays the live video from the rtmp link, which indicates that the nginx-rtmp service correctly This means that the nginx-rtmp service has correctly realized the streaming media transmission function of the rtmp protocol.

For more details on FFmpeg development seeFFmpeg Development in Action: From Zero Basics to Short Video OnlineA book.