[多媒體服務器] 通過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 模塊

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:

  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 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:

  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 文件,添加如下內容:

. . .
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

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/713368.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/713368.shtml
英文地址,請注明出處:http://en.pswp.cn/news/713368.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

jmeter如何請求訪問https接口

添加線程組http請求 新建線程組,添加http請求 填入協議,ip,端口,請求類型,路徑,以及請求參數,查看結果樹等。 然后最關鍵的一步來了。 導入證書 步驟:獲取證書,重新生…

基于SSM的高校競賽和考級查詢系統(有報告)。Javaee項目。ssm項目。

演示視頻: 基于SSM的高校競賽和考級查詢系統(有報告)。Javaee項目。ssm項目。 項目介紹: 采用M(model)V(view)C(controller)三層體系結構,通過Sp…

Java中的動態代理與Spring AOP編程

第一章:引言 大家好,我是小黑,在Java里,動態代理和Spring AOP(面向切面編程)是兩個能讓代碼更加靈活、更加干凈的強大工具。作為一名Java程序員,小黑覺得掌握它們對于寫出高質量的代碼來說非常…

Property ‘glob‘ does not exist on type ‘ImportMeta‘

參考文章: vite導入文件,Property ‘globEager‘ does not exist on type ‘ImportMeta‘

通過GitHub探索Python爬蟲技術

1.檢索爬取內容案例。 2.找到最近更新的。(最新一般都可以直接運行) 3.選擇適合自己的項目,目前測試下面畫紅圈的是可行的。 4.方便大家查看就把代碼粘貼出來了。 #圖中畫圈一代碼 import requests import os import rewhile True:music_id input("請輸入歌曲…

IDEA創建SpringMVC項目沒有java和resources

跟著一些教程創建SpringMVC項目,完了之后沒有java和resources兩個文件夾,他們教程讓我們自己新建(感覺不是很科學啊,為什么必須自己建,生成的就沒有呢) 分享一下新建的方法 在src-main目錄下右鍵new—>D…

鴻蒙Harmony應用開發—ArkTS聲明式開發(通用屬性:位置設置)

設置組件的對齊方式、布局方向和顯示位置。 說明: 從API Version 7開始支持。后續版本如有新增內容,則采用上角標單獨標記該內容的起始版本。 align align(value: Alignment) 設置容器元素繪制區域內的子元素的對齊方式。 卡片能力: 從API…

收盤價時空模式挖掘與多股票走勢聚類分析:探索市場行為共性

收盤價時空模式挖掘與多股走勢聚類分析:探索市場行為共性 一.版本信息二.操作步驟1.下載各股歷史交易數據A.代碼(download_stocks.py)B.執行2.遍歷各股的csv文件,提取收盤價數據,歸一化,繪制曲線,保存圖片A.代碼B.執行3.用上面的圖片集訓練VAE模型A.代碼B.執行4.用上面訓出的V…

【遠程開發調試】Pycharm或Webstorm使用遠程服務器調試開發

Pycharm如何使用遠程服務器環境進行開發_pycharm使用服務器環境-CSDN博客 Pycharm配置遠程調試_pycharm 遠程調試-CSDN博客

langchain學習筆記(八)

RunnableLambda: Run Custom Functions | 🦜?🔗 Langchain 可以在pipeline中使用任意函數,但要注意所有的輸入都只能是“1”個參數,當函數需要多個參數時需要采用字典來包裝 itemgetter用法見langchain學習筆記(六&…

【系統分析師】-系統配置與性能評價

1、性能指標 主頻:又稱時鐘頻率,1GHZ表示1秒有1G個時鐘周期 1s10^9ns 主頻外頻 * 倍頻 時鐘周期 主頻的倒數指令周期:取出并執行一條指令的時間 總線周期:一個訪存儲器或IO操作所用時間平均執行周期數:CPI表示…

【學習心得】網絡中常見數據格式(爬蟲入門知識)

在爬蟲爬取數據的之前,必須先系統的了解一下我們待爬取的數據有哪些格式,這樣做的好處在與能針對不同的數據類型采取不同分方法手段。 一、XML XML(Extensible Markup Language)是一種可擴展的標記語言,它定義了一套標…

如何解決幻獸帕魯/Palworld服務器聯機游戲時的丟包問題?

如何解決幻獸帕魯/Palworld服務器聯機游戲時的丟包問題? 等待服務器維護:首先,確保網絡連接穩定,然后查看游戲官方或社區論壇,了解是否有服務器維護的消息。這是解決丟包問題的一種直接且有效的方法。 更新顯卡驅動&a…

Siemens-NXUG二次開發-獲取prt中體與類型、實體面與類型、實體邊與類型、邊上點的Tag標識[Python UF][20240302]

Siemens-NXUG二次開發-獲取prt中體與類型、實體面與類型、實體邊與類型、邊上點的Tag標識[Python UF][20240302] 1.python uf函數1.1 NXOpen.UF.Obj.CycleObjsInPart1.2 NXOpen.UF.Obj.AskTypeAndSubtype1.3 NXOpen.UF.Modeling.AskBodyFaces1.4 NXOpen.UF.Modeling.AskFaceEdg…

RISC-V特權架構 - 機器模式下的異常處理

RISC-V特權架構 - 機器模式下的異常處理 1 進入異常1.1 從mtvec 定義的PC 地址開始執行1.2 更新CSR 寄存器mcause1.3 更新CSR 寄存器mepc1.4 更新CSR 寄存器mtval1.5 更新CSR 寄存器mstatus 2 退出異常2.1 從mepc 定義的PC 地址開始執行2.2 更新CSR 寄存器mstatus 3 異常服務程…

Android Tombstone 分析

1.什么是tombstone Tombstone是指在分布式系統中用于標記數據已被刪除的記錄,通常包含刪除操作的時間戳和相關信息。 當一個動態庫(native程序)開始執行時,系統會注冊一些連接到 debuggerd 的signal handlers。當系統發生崩潰時…

wpa_supplicant與用戶態程序的交互分析

1 wpa_supplicant與用戶態程序wpa_cli的交互過程 1.1 交互接口類型 wpa_supplicant與用戶態程序交互的主要接口包括以下幾種: 1)命令行界面:通過命令行工具 wpa_cli 可以與 wpa_supplicant 進行交互。wpa_cli 允許用戶執行各種 wpa_suppli…

Spark Shuffle Tracking 原理分析

Shuffle Tracking Shuffle Tracking 是 Spark 在沒有 ESS(External Shuffle Service)情況,并且開啟 Dynamic Allocation 的重要功能。如在 K8S 上運行 spark 沒有 ESS。本文檔所有的前提都是基于以上條件的。 如果開啟了 ESS,那么 Executor 計算完后&a…

MySQL 表的基本操作,結合項目的表自動初始化來講

有了數據庫以后,我們就可以在數據庫中對表進行增刪改查了,這也就意味著,一名真正的 CRUD Boy 即將到來(😁)。 查表 查看當前數據庫中所有的表,使用 show tables; 命令 由于當前數據庫中還沒有…

基于Python3的數據結構與算法 - 09 希爾排序

一、引入 希爾排序是一種分組插入排序的算法。 二、排序思路 首先取一個整數d1 n/2,將元素分為d1個組,每組相鄰量取元素距離為d1,在各組內直接進行插入排序;取第二個整數d2 d1/2, 重復上述分組排序過程&#xff0…