Use nginx and ffmpeg to build HTTP FLV streaming media server (camera RTSP video stream->RTMP->http-flv)

Glossary

 

RTSP

(Real-Time Streaming Protocol)

Is a network protocol used to control the transmission of real-time streaming media. It is an application layer protocol commonly used to establish and control the transmission of media streams between clients and streaming media servers. RTSP allows the client to send requests to the server, such as play, pause, stop, forward, backward, etc., to control the playback and operation of the media stream. RTSP can be used in conjunction with different transport protocols (such as RTP, TCP, UDP) to achieve real-time audio and video streaming. It is widely used in applications such as video surveillance, live streaming, video conferencing, etc.

RTMP

RTMP (Real-Time Messaging Protocol) is a network protocol used for real-time data transmission. It is commonly used in streaming and real-time communication applications.

RTMP was originally developed by Adobe for audio and video transmission between Adobe Flash players and media servers. It uses TCP as the transport layer protocol and supports the transmission of real-time audio, video and data.

RTMP can be used in application scenarios such as live broadcast, video conferencing, and online games. Through RTMP, users can upload audio and video data streams from the client to the media server, and then other users can obtain these data streams from the media server through the same protocol.

The difference between RTSP and RTMP

RTSP (Real-Time Streaming Protocol) and RTMP (Real-Time Messaging Protocol) are two protocols used for real-time streaming media transmission. They have the following main differences:

  1. Transmission method: RTSP uses TCP (optional UDP) as the transport layer protocol, while RTMP only uses TCP. RTSP achieves control and data transmission of streaming media by establishing a separate control connection and data connection, while RTMP performs control and data transmission simultaneously on a single connection.

  2. Functions and uses: RTSP is mainly used for streaming media control and session management, providing more flexible control capabilities in operations such as playback, pause, and jump. RTMP focuses on real-time audio and video data transmission and interaction, and is suitable for live broadcast, video chat and other scenarios.

  3. Platform support: RTSP is an open standard protocol that is widely used on a variety of platforms and devices, including PCs, mobile devices, smart TVs, etc. RTMP was originally developed by Adobe, and although it is also widely used on many platforms, it may be restricted or require special licensing on some platforms and devices.

  4. Compression and encoding support: RTMP supports a wide range of audio and video encoding formats, including H.264, AAC, etc. RTSP does not support compression and encoding in the original protocol, but it can be used in combination with other compression protocols (such as RTP) to realize the transmission of audio and video data.

In general, RTSP focuses more on streaming media control and session management, and is suitable for scenarios that require flexible control; while RTMP focuses more on real-time audio and video data transmission and interaction, and is suitable for scenarios such as live broadcast and real-time communication. Choosing which protocol to use should be a decision based on specific needs and platform support.

HTTP-FLV

HTTP-FLV encapsulates streaming media data into FLV format first, and then transmits it to the client through the HTTP protocol. Through the live broadcast of HTTP FLV, an HTTP protocol convention is used here. If the content-length header field of http does not exist, then The client will continue to receive data until the HTTP connection is disconnected. The process is very simple. The video client sends an HTTP request without the content-length header field. The server responds to HTTP and keeps sending FLV data; the client receives the response. and keeps receiving data until the connection is disconnected.

Nginx

Nginx is a web server widely used for serving static content, proxying and load balancing. In this scenario, Nginx is used to receive the RTSP stream and serve it to the client over HTTP

FFmpeg

FFmpeg is a powerful open source multimedia framework that allows users to decode, encode, transcode, multiplex, demux, stream, filter and play various types of media files. In this case, FFmpeg is used to decode the RTSP stream and transcode it to FLV format.

nginx-http-flv-module

This is an Nginx module specifically designed to stream live FLV (Flash Video) over HTTP. It acts as a plugin for Nginx and provides the functionality required to serve FLV files in a streaming manner

The whole thing is to use nginx to build an HTTP FLV streaming media server:

Process: camera rtsp video stream->rtmp->http-flv.

Converting rtsp to rtmp requires the help of ffmpeg conversion.

Converting rtmp to http-flv requires nginx conversion.

nginx-http-flv-module is developed based on nginx-rtmp-module and includes all functions of nginx-rtmp-module. Therefore, nginx-rtmp-module cannot be installed at the same time. Its compilation and installation steps are similar to nginx-rtmp-module.

Install dependent libraries:

nginx编译依赖 gcc 环境,如果没有 gcc 环境,则需要安装:
yum -y install gcc gcc-c++
nginx的http模块使用pcre来解析正则表达式,pcre-devel 是使用 pcre 开发的一个二次开发库,nginx需要依赖这两个库,执行如下命令:
yum install -y pcre pcre-devel
nginx使用zlib对http包的内容进行gzip,需要安装此库:
yum install -y zlib zlib-devel
nginx可能需要支持HTTPS,最好在系统中安装好openssl,一般系统自带,可通过如下命令确认:
openssl version
如果安装了会显示版本号,如果提示未安装,执行如下命令安装:
yum install -y openssl openssl-devel

nginx compilation and installation

下载nginx:
wget http://nginx.org/download/nginx-1.21.6.tar.gz

下载nginx模块HTTP FLV模块:
wget https://github.com/winshining/nginx-http-flv-module/archive/v1.2.10.tar.gz
或者是
https://github.com/winshining/nginx-http-flv-module.git
下载zip包也可以

解压nginx和HTTP FLV模块:
tar -zxvf nginx-1.21.6.tar.gz
tar -zxvf v1.2.10.tar.gz

nginx编译和安装:
./configure --prefix=/usr/local/nginx --add-module=../nginx-http-flv-module-1.2.10
这里配置了–prefix,配置安装路径,不安装到默认路径,以便安装文件移植和卸载,可直接拷贝次目录下的文件到其他电脑运行如果目录不同,启动时通过-p指定目录即可。
make & makeinstall

启动nginx,验证是否正确,执行命令(注意安装目录下的sbin/nginx):
/usr/local/nginx/sbin/nginx

Open the browser and enter: http://10.45.12.29/ . Note that the ip is the local ip. If the following content is displayed, it means the installation is successful.

Modify nginx related configuration to support HTTP FLV

在/home/wangdenuan/nginx/conf/nginx.conf文件中增加:
#user  nobody;
worker_processes  1;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;


events {
    worker_connections  1024;
}


rtmp {
        server {
                listen 1935;
                chunk_size 4096;

                application myapp {
                        live on;
                        meta off;#为了兼容flvj.js
                        hls on;
                        hls_path /tmp/hls;
                        hls_fragment 4;
                        hls_playlist_length 30;
                        record off;
                        allow play all;
                }
        }
}


http {
    include       mime.types;
    default_type  application/octet-stream;

    #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
    #                  '$status $body_bytes_sent "$http_referer" '
    #                  '"$http_user_agent" "$http_x_forwarded_for"';

    #access_log  logs/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;

    server {
        listen       80;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   html;
            index  index.html index.htm;
        }
        location /hls{
            add_header Access-Control-Allow-Origin *;
            types {
                application/vnd.apple.mpegurl m3u8;
                video/mp2t ts;
            }
            alias /tmp/hls;
            expires -1;
        }
        location /flv {
                flv_live on;
                chunked_transfer_encoding on;                         #支持'Transfer-Encoding: chunked'方式回复
                add_header 'Access-Control-Allow-Origin' '*';         #添加额外的 HTTP 头
                add_header 'Access-Control-Allow-Credentials' 'true'; #添加额外的 HTTP 头
        }

        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }

}


其配置与RTMP配置增加了HTTP FLV的URI的配置。
配置完成后,执行:/home/wangdenuan/nginx/sbin/nginx -s reload,使得配置生效。

If there are multiple rtmp addresses

rtmp {  
   # 第一个转流地址
    server {  
        listen 1935;      #监听的端口号
        application hik01 {     #自定义的名字
            live on;  
       }  
    } 
     # 第二个转流地址
    server {  
        listen 1936;      #监听的端口号
        application hik02 {     #自定义的名字
            live on;  
       }  
    } 
    # 第N个转流地址
    server {  
        listen xxxx;      #监听的端口号
        application xxxx {     #自定义的名字
            live on;  
       }  
    } 
}

Install ffmpeg

安装FFmpeg需要先安装其依赖:yasm
yum install yasm -y
# 获取
wget https://ffmpeg.org/releases/ffmpeg-4.1.tar.bz2
# 解压
tar -xvf ffmpeg-4.1.tar.bz2
# 查看
cd ffmpeg-4.1
# 编译
./configure
# 安装
make && make install

如果make报错,并且和yasm相关,那就配置一下yasm的环境变量

以上的在使用的过程中会报错,原因不支持libx264编解码,所以下面的会比较全面。

Download ffmpeg

Download the Linux version of ffmpeg from the official website, official website link: https://ffmpeg.org/Click
the download button, as shown in the figure below:
Insert image description here
Click the more releases hyperlink, as shown in the figure below:
Insert image description here
Select the latest stable version FFmpeg 5.0.1, and click Download xz tarball button, as shown below:
Insert image description here

Upload ffmpeg to VMware virtual machine

Use xftp to upload the source package ffmpeg-6.0.tar.xz to the Linux virtual machine (this tutorial uploads it to the /soft/ffmpeg directory)

Decompress the ffmpeg compressed package

# cd /soft/ffmpeg # tar -xvf ffmpeg-6.0.tar.xz

Install gcc

# yum install -y gcc

Install yasm compiler

Enter http://yasm.tortall.net/releases/Release1.3.0.html, download yasm-1.3.0.tar.gz, and upload it to the /soft/ffmpeg directory

Or use wget http://www.tortall.net/projects/yasm/releases/yasm-1.3.0.tar.gz

Unzip yasm

# tar -zxvf yasm-1.3.0.tar.gz

Enter the unzipped directory

# cd yasm-1.3.0

Compile and install

# ./configure # make # make install

View installation results

# yasm --version

Install nasm

Download: wget https://www.nasm.us/pub/nasm/releasebuilds/2.14.02/nasm-2.14.02.tar.gz

Unzip

# tar -xvf nasm-2.14.02.tar.gz

Enter directory

# cd nasm-2.14.02

Compile and install

# ./configure # make && make install

View installation results

# nasm --version

Install libx264

https://code.videolan.org/videolan/x264/ Visit git and download tar.gz

Unzip x264

# tar -xvf x264-master.tar.gz

Enter the unzipped directory

# cd x264-master

Compile and install

# ./configure --enable-shared # make && make install

View installation results

# x264 --version

Install ffmpeg

Enter the ffmpeg decompression directory

# cd /soft/ffmpeg/ffmpeg-6.0

Specify the installation directory/soft/ffmpeg

# ./configure --enable-libx264 --enable-gpl --prefix=/soft/ffmpeg

Install

# make && make install

Modify the file /etc/ld.so.conf

# vim /etc/ld.so.conf

Add two lines:

/soft/ffmpeg/lib/ /usr/local/lib/

Make the modified file /etc/ld.so.conf

# ldconfig

View installation results

# ./ffmpeg -version

Configure environment variables

# vim /etc/profile export PATH=$PATH:/soft/ffmpeg/bin

Make environment variables effective

# source /etc/profile

Check ffmpeg version

# ffmpeg -version

Problem 1 & Solution

When executing and compiling ffmpeg (./configure --enable-libx264 --enable-gpl --prefix=/soft/ffmpeg), exception:

Insert image description here

Solution 1: Download pkg-config-0.29.2.tar.gz, address: https://www.freedesktop.org/wiki/Software/pkg-config/

Or directly wget http://pkgconfig.freedesktop.org/releases/pkg-config-0.29.2.tar.gz to decompress the compressed package

# tar zxvf pkg-config-0.29.2.tar.gz

Enter the unzipped directory

# cd pkg-config-0.29.2

Compile and install

# ./configure --with-internal-glib # make # make check # make install

View installation results

# pkg-config –-version

Problem 2 & Solution

After installing ffmpeg, exception occurs when executing ./ffmpeg -version

Insert image description here

Solution 2: Modify the file /etc/ld.so.conf

# vim /etc/ld.so.conf

Add two lines:

/soft/ffmpeg/lib/ /usr/local/lib/

Make the modified file /etc/ld.so.conf effective

# ldconfig

After compilation, check the link status of the target file

# ldd ffmpeg

Insert image description here

The result you see is that several libraries have not found the linked library files, and xxx not found is displayed, but the library files exist. See: Solution 2.

Configure ffmpeg environment variables

# vim /etc/profile

Add two lines at the end

export FFMPEG_HOME=/soft/ffmpeg/ffmpeg-6.0 export PATH=$PATH:$FFMPEG_HOME/bin

Make the configuration effective

# source /etc/profile

Enable ffmpeg for streaming

The following commands need to modify the rtsp stream address. The rtmp address is subject to the actual configuration of the server. Other commands can be copied temporarily.

-rtsp_transport tcp converts the default udp protocol to tcp protocol, which can solve the problem of blurred screen (packet loss) to a certain extent.

# 命令
ffmpeg -rtsp_transport tcp -i [rtsp流地址] flv -r 25 -s 1920*1080 -an [转换后的rtmp流地址]
# 实例
ffmpeg -rtsp_transport tcp -i rtsp://admin:[email protected] -f flv -r 25 -s 1920*1080 -an rtmp://localhost:1935/hik01/
# 后台运行,在命令前加nohup,后加 &
nohup ffmpeg -rtsp_transport tcp -i rtsp://admin:[email protected] -f flv -r 25 -s 1920*1080 -an rtmp://localhost:1935/hik01/ &

Open the command prompt and enter the following ffmpeg command to push the rtmp video stream to the rtmp server. It is assumed that the rtmp server IP is: 10.45.12.29

ffmpeg -re -i rtsp://admin:zxm10@@@@10.45.12.141/h264/ch1/main/av_stream -vcodec libx264 -acodec aac -f flv rtmp://10.45.12.29:1935/myapp/mystream

Background process

nohup ffmpeg -re -i rtsp://admin:zxm10@@@@10.45.12.141/h264/ch1/main/av_stream -vcodec libx264 -acodec aac -f flv rtmp://10.45.12.29:1935/myapp/mystream &

Use video tools such as VLC to verify whether the flv stream is available. Download VLC from Baidu.

Use VLC to play the following stream: http://10.45.12.29/flv?port=1935&app=myapp&stream= mystream, verify whether HTTP FLV can be played successfully

Guess you like

Origin blog.csdn.net/weixin_38340874/article/details/131855113