參考:
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 nginx
sudo apt update
sudo 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: 點播場景,把媒體文件推送給nginx rtmp服務進行代理
安裝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.
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:
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:
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
:
sudo nano /etc/nginx/sites-available/rtmp
Copy
Add the following contents:
/etc/nginx/sites-available/rtmp
server {listen 8080;server_name localhost;# rtmp statlocation /stat {rtmp_stat all;rtmp_stat_stylesheet stat.xsl;}location /stat.xsl {root /var/www/html/rtmp;}# rtmp controllocation /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:
sudo mkdir /var/www/html/rtmp
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:
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:
sudo ln -s /etc/nginx/sites-available/rtmp /etc/nginx/sites-enabled/rtmp
Copy
Now you can reload Nginx again to process your changes:
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 文件,添加如下內容:
. . .
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