參考:
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
增加如下內(nèi)容到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:靜態(tài)流點(diǎn)播場景,通過hls或dash將MP4之類的多媒體封裝文件轉(zhuǎn)換為m3u8進(jìn)行點(diǎn)播
通過ffmpeg把MP4文件轉(zhuǎn)換為m3u8列表集:
ffmpeg -i input.mp4 -codec: copy -start_number 0 -hls_time 10 -hls_list_size 0 -hls_wrap 10 output.m3u8
參數(shù)解釋:
-i input.mp4
: 輸入文件
-codec: copy
: 復(fù)制編解碼器
-start_number 0
: 分片從0開始編號(hào)
-hls_time 10
: 每個(gè)分片的時(shí)長為10秒
-hls_list_size 0
: 播放列表不限制大小
-hls_wrap 10
: 當(dāng)達(dá)到指定的段數(shù)時(shí),重新開始
output.m3u8
: 輸出的播放列表文件
配置nginx以支持HLS,打開/etc/nginx/nginx.conf,添加如下內(nèi)容:
http {
...
server {
listen 80;
server_name localhost.com;
location /hls/ {
# 假設(shè)m3u8和TS文件存儲(chǔ)在/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: 靜態(tài)流直播場景,把媒體文件推送給nginx rtmp服務(wù)進(jìn)行代理(rtmp不支持瀏覽器播放,只能通過vlc等播放器播放)
安裝ffmpeg
sudo apt install ffmpeg
安裝youtube-dl
sudo pip install youtube-dl
(可選)從youtube上下載一個(gè)文件備用,也可以隨便找一個(gè)MP4文件
youtube-dl https://www.youtube.com/watch?v=iom_nhYQIYk
使用ffmpeg處理媒體文件,并將其代理給rtmp服務(wù)器
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 代表本機(jī),不用動(dòng),live是nginx.conf文件里的 application live,如果是 application live1,那么這里就是 live1 , stream 是當(dāng)前流的標(biāo)識(shí),可以自定義為任何字符串。
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:靜態(tài)流直播場景,支持hls和dash,以便通過瀏覽器播放
瀏覽器目前都不支持rtmp協(xié)議播放流媒體,如果希望通過瀏覽器播放,那么需要打開hls和dash協(xié)議支持。
打開 nginx.conf 文件,添加如下內(nèi)容:
. . .
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 ,添加如下內(nèi)容:
. . .
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
創(chuàng)建臨時(shí)媒體文件存放路徑給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資源(可選,數(shù)據(jù)統(tǒng)計(jì)頁面)
經(jīng)過step2以后,我們便可以通過支持rtmp協(xié)議的播放器進(jìn)行點(diǎn)播了。接著,需要開啟RTMP的統(tǒng)計(jì)頁面,這樣就不需要不斷地往nginx.conf文件里加配置來實(shí)現(xiàn)rtmp功能。我們可以創(chuàng)建一個(gè)配置文件:
/etc/nginx/sites-available/rtmp
把如下內(nèi)容放入這個(gè)文件里:
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;
}
}
?接著創(chuàng)建目錄:
/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
打開訪問統(tǒng)計(jì)數(shù)據(jù)頁面的防火墻:
sudo ufw allow from your_ip_address to any port http-alt
創(chuàng)建軟連接(這步是慣例,可以不做,具體原因后續(xù)補(bǔ)充):
sudo ln -s /etc/nginx/sites-available/rtmp /etc/nginx/sites-enabled/rtmp
重新加載nginx配置
sudo systemctl reload nginx.service
至此,統(tǒng)計(jì)頁面可以通過如下方式訪問
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:動(dòng)態(tài)流直播場景,使用OBS進(jìn)行直播流代理
ffmpeg只能處理點(diǎn)播場景,直播場景需要使用OBS進(jìn)行流代理。
安裝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文章來源:http://www.zghlxwxcb.cn/news/detail-857362.html
/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.文章來源地址http://www.zghlxwxcb.cn/news/detail-857362.html
到了這里,關(guān)于[多媒體服務(wù)器] 通過nginx搭建 rtmp/hls/dash 媒體服務(wù)器,支持點(diǎn)播和直播的文章就介紹完了。如果您還想了解更多內(nèi)容,請?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!