[多媒体服务器] 通过nginx搭建 rtmp/hls/dash 媒体服务器,支持点播和直播

参考:

How To Set Up a Video Streaming Server using Nginx-RTMP on Ubuntu 20.04 | DigitalOcean

用到的工具:

nginx,nginx rtmp插件,OBS,ffmpeg,ubuntu,youtube-dl

Step1:安装和配置nginx

安装 nginx 和 rtmp 模块

bash 复制代码
sudo apt install nginx
sudo apt update
sudo apt install libnginx-mod-rtmp

增加如下内容到nginx配置文件 nginx.conf

bash 复制代码
rtmp {
        server {
                listen 1935;
                chunk_size 4096;
                allow publish 127.0.0.1;
                deny publish all;

                application live {
                        live on;
                        record off;
                }
        }
}

说明:

  • listen 1935 means that RTMP will be listening for connections on port 1935, which is standard.
  • chunk_size 4096 means that RTMP will be sending data in 4KB blocks, which is also standard.
  • allow publish 127.0.0.1 and deny publish all mean that the server will only allow video to be published from the same server, to avoid any other users pushing their own streams.
  • application live defines an application block that will be available at the /live URL path.
  • live on enables live mode so that multiple users can connect to your stream concurrently, a baseline assumption of video streaming.
  • record off disables Nginx-RTMP's recording functionality, so that all streams are not separately saved to disk by default.

打开1935端口的防火墙限制

bash 复制代码
sudo ufw allow 1935/tcp

nginx重新加载配置文件nginx.conf

bash 复制代码
sudo systemctl reload nginx.service

Step2: 点播场景,把媒体文件推送给nginx rtmp服务进行代理

安装ffmpeg

bash 复制代码
sudo apt install ffmpeg

安装youtube-dl

bash 复制代码
sudo pip install youtube-dl

(可选)从youtube上下载一个文件备用,也可以随便找一个MP4文件

bash 复制代码
youtube-dl https://www.youtube.com/watch?v=iom_nhYQIYk

使用ffmpeg处理媒体文件,并将其代理给rtmp服务器

bash 复制代码
ffmpeg -re -i "Introducing App Platform by DigitalOcean-iom_nhYQIYk.mkv" -c:v copy -c:a aac -ar 44100 -ac 1 -f flv rtmp://localhost/live/stream

rtmp://localhost/live/stream 中的 localhost 代表本机,不用动,live是nginx.conf文件里的 application live,如果是 application live1,那么这里就是 live1 , stream 是当前流的标识,可以自定义为任何字符串。

Note: You can also stream directly to, for example, Facebook Live using ffmpeg without needing to use Nginx-RTMP at all by replacing rtmp://localhost/live/stream in your ffmpeg command with rtmps://live-api-s.facebook.com:443/rtmp/your-facebook-stream-key. YouTube uses URLs like rtmp://a.rtmp.youtube.com/live2. Other streaming providers that can consume RTMP streams should behave similarly.

Step3:直播场景,使用OBS进行直播流代理

ffmpeg只能处理点播场景,直播场景需要使用OBS进行流代理。

安装OBS,check Open Broadcaster Software | OBS

Streaming via ffmpeg is convenient when you have a prepared video that you want to play back, but live streaming can be much more dynamic. The most popular software for live streaming is OBS, or Open Broadcaster Software -- it is free, open source, and very powerful.

OBS is a desktop application, and will connect to your server from your local computer.

After installing OBS, configuring it means customizing which of your desktop windows and audio sources you want to add to your stream, and then adding credentials for a streaming service. This tutorial will not be covering your streaming configuration, as it is down to preference, and by default, you can have a working demo by just streaming your entire desktop. In order to set your streaming service credentials, open OBS' settings menu, navigate to the Stream option and input the following options:

Streaming Service: Custom
Server: rtmp://your_domain/live
Play Path/Stream Key: obs_stream

obs_stream is an arbitrarily chosen path -- in this case, your video would be available at rtmp://your_domain/live/obs_stream. You do not need to enable authentication, but you do need to add an additional entry to the IP whitelist that you configured in Step 1.

Back on the server, open Nginx's main configuration file, /etc/nginx/nginx.conf, and add an additional allow publish entry for your local IP address. If you don't know your local IP address, it's best to just go to a site like What's my IP which can tell you where you accessed it from:

  1. sudo nano /etc/nginx/nginx.conf

Copy

/etc/nginx/nginx.conf

. . .
                allow publish 127.0.0.1;
                allow publish your_local_ip_address;
                deny publish all;
. . .

Save and close the file, then reload Nginx:

  1. sudo systemctl reload nginx.service

Copy

You should now be able to close OBS' settings menu and click Start Streaming from the main interface! Try viewing your stream in a media player as before. Now that you've seen the fundamentals of streaming video in action, you can add a few other features to your server to make it more production-ready.

Step4:管理rtmp资源

Now that you have Nginx configured to stream video using the Nginx-RTMP module, a common next step is to enable the RTMP statistics page. Rather than adding more and more configuration details to your main nginx.conf file, Nginx allows you to add per-site configurations to individual files in a subdirectory called sites-available/. In this case, you'll create one called rtmp:

  1. sudo nano /etc/nginx/sites-available/rtmp

Copy

Add the following contents:

/etc/nginx/sites-available/rtmp

server {
    listen 8080;
    server_name  localhost;

    # rtmp stat
    location /stat {
        rtmp_stat all;
        rtmp_stat_stylesheet stat.xsl;
    }
    location /stat.xsl {
        root /var/www/html/rtmp;
    }

    # rtmp control
    location /control {
        rtmp_control all;
    }
}

Save and close the file. The stat.xsl file from this configuration block is used to style and display an RTMP statistics page in your browser. It is provided by the libnginx-mod-rtmp library that you installed earlier, but it comes zipped up by default, so you will need to unzip it and put it in the /var/www/html/rtmp directory to match the above configuration. Note that you can find additional information about any of these options in the Nginx-RTMP documentation.

Create the /var/www/html/rtmp directory, and then uncompress the stat.xsl.gz file with the following commands:

  1. sudo mkdir /var/www/html/rtmp
  2. sudo gunzip -c /usr/share/doc/libnginx-mod-rtmp/examples/stat.xsl.gz > /var/www/html/rtmp/stat.xsl

Copy

Finally, to access the statistics page that you added, you will need to open another port in your firewall. Specifically, the listen directive is configured with port 8080, so you will need to add a rule to access Nginx on that port. However, you probably don't want others to be able to access your stats page, so it's best only to allow it for your own IP address. Run the following command:

  1. sudo ufw allow from your_ip_address to any port http-alt

Copy

Next, you'll need to activate this new configuration. Nginx's convention is to create symbolic links (like shortcuts) from files in sites-available/ to another folder called sites-enabled/ as you decide to enable or disable them. Using full paths for clarity, make that link:

  1. sudo ln -s /etc/nginx/sites-available/rtmp /etc/nginx/sites-enabled/rtmp

Copy

Now you can reload Nginx again to process your changes:

  1. sudo systemctl reload nginx.service

Copy

You should now be able to go to http://your_domain:8080/stat in a browser to see the RTMP statistics page. Visit and refresh the page while streaming video and watch as the stream statistics change.

You've now seen how to monitor your video stream and push it to third party providers. In the final section, you'll learn how to provide it directly in a browser without the use of third party streaming platforms or standalone media player apps.

Step5:支持hls和dash,以便通过浏览器播放

浏览器目前都不支持rtmp协议播放流媒体,如果希望通过浏览器播放,那么需要打开hls和dash协议支持。

打开 nginx.conf 文件,添加如下内容:

bash 复制代码
. . .
rtmp {
        server {
. . .
                application live {
                    	live on;
                    	record off;
                        hls on;
                        hls_path /var/www/html/stream/hls;
                        hls_fragment 3;
                        hls_playlist_length 60;

                        dash on;
                        dash_path /var/www/html/stream/dash;
                }
        }
}
. . .

打开 sites-available/rtmp ,添加如下内容:

bash 复制代码
. . .
server {
    listen 8088;

    location / {
        add_header Access-Control-Allow-Origin *;
        root /var/www/html/stream;
    }
}

types {
    application/dash+xml mpd;
}

放开8088端口的防火墙:

bash 复制代码
sudo ufw allow 8088/tcp

创建临时媒体文件存放路径给nginx用(参考上面的nginx.conf里的配置):

bash 复制代码
sudo mkdir /var/www/html/stream

重启nginx:

bash 复制代码
sudo systemctl reload nginx

浏览器上访问如下地址即可播放hls和dash

http://your_domain:8088/hls/stream.m3u8

http://your_domain:8088/dash/stream.mpd

相关推荐
龙哥说跨境1 小时前
如何利用指纹浏览器爬虫绕过Cloudflare的防护?
服务器·网络·python·网络爬虫
pk_xz1234563 小时前
Shell 脚本中变量和字符串的入门介绍
linux·运维·服务器
小珑也要变强3 小时前
Linux之sed命令详解
linux·运维·服务器
海绵波波1073 小时前
Webserver(4.3)TCP通信实现
服务器·网络·tcp/ip
九河云5 小时前
AWS账号注册费用详解:新用户是否需要付费?
服务器·云计算·aws
Lary_Rock5 小时前
RK3576 LINUX RKNN SDK 测试
linux·运维·服务器
幺零九零零6 小时前
【计算机网络】TCP协议面试常考(一)
服务器·tcp/ip·计算机网络
云飞云共享云桌面7 小时前
8位机械工程师如何共享一台图形工作站算力?
linux·服务器·网络
幺零九零零10 小时前
【C++】socket套接字编程
linux·服务器·网络·c++
free11 小时前
netstat中sendq/recvq用于排查发送端发送数据的问题
服务器