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

服务器 0

参考:

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 模块

sudo apt install nginxsudo apt updatesudo apt install libnginx-mod-rtmp

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

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端口的防火墙限制

sudo ufw allow 1935/tcp

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

sudo systemctl reload nginx.service



Step2:静态流点播场景,通过hls或dash将MP4之类的多媒体封装文件转换为m3u8进行点播

通过ffmpeg把MP4文件转换为m3u8列表集:

ffmpeg -i input.mp4 -codec: copy -start_number 0 -hls_time 10 -hls_list_size 0 -hls_wrap 10 output.m3u8

参数解释:

  • -i input.mp4: 输入文件

  • -codec: copy: 复制编解码器

  • -start_number 0: 分片从0开始编号

  • -hls_time 10: 每个分片的时长为10秒

  • -hls_list_size 0: 播放列表不限制大小

  • -hls_wrap 10: 当达到指定的段数时,重新开始

  • output.m3u8: 输出的播放列表文件

配置nginx以支持HLS,打开/etc/nginx/nginx.conf,添加如下内容:

http {    ...     server {        listen 80;        server_name localhost.com;         location /hls/ {            # 假设m3u8和TS文件存储在/home/yk/VOD/hls/1/目录下            root /home/yk/VOD/hls/1/;            # 添加跨域配置(如果需要)            add_header 'Access-Control-Allow-Origin' '*' always;            # 添加HLS所需的MIME类型            types {                application/vnd.apple.mpegurl m3u8;                video/mp2t ts;            }            # 禁止浏览目录            autoindex off;        }    }}

nginx重新加载配置:

sudo nginx -s reload

浏览器中访问:

 比如 ffmpeg 输出的 ts 文件集 和 m3u8 列表在 /home/yk/VOD/1/ 目录下,那么在浏览器中输入

http://localhost.com:80/output.m3u8




Step3: 静态流直播场景,把媒体文件推送给nginx rtmp服务进行代理(rtmp不支持浏览器播放,只能通过vlc等播放器播放)

安装ffmpeg

sudo apt install ffmpeg

安装youtube-dl

sudo pip install youtube-dl

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

youtube-dl https://www.youtube.com/watch?v=iom_nhYQIYk

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

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.




Step4:静态流直播场景,支持hls和dash,以便通过浏览器播放

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

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

. . .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 ,添加如下内容:

. . .server {    listen 8088;    location / {        add_header Access-Control-Allow-Origin *;        root /var/www/html/stream;    }}types {    application/dash+xml mpd;}

放开8088端口的防火墙:

sudo ufw allow 8088/tcp

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

sudo mkdir /var/www/html/stream

重启nginx:

sudo systemctl reload nginx

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

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

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




Step5:管理rtmp资源(可选,数据统计页面)

经过step2以后,我们便可以通过支持rtmp协议的播放器进行点播了。接着,需要开启RTMP的统计页面,这样就不需要不断地往nginx.conf文件里加配置来实现rtmp功能。我们可以创建一个配置文件:

/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;    }}

 接着创建目录:

/var/www/html/rtmp

然后通过如下命令把 libnginx-mod-rtmp的xsl配置文件解压到上述目录中:

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

打开访问统计数据页面的防火墙:

sudo ufw allow from your_ip_address to any port http-alt

创建软连接(这步是惯例,可以不做,具体原因后续补充):

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

重新加载nginx配置

sudo systemctl reload nginx.service

至此,统计页面可以通过如下方式访问

http://your_domain:8080/stat 

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.




Step6:动态流直播场景,使用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: CustomServer: rtmp://your_domain/livePlay 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.

也许您对下面的内容还感兴趣: