目錄
1. Set up Nginx with RTMP Module for Ingest
2. Configure HLS Output for Compatibility
3. Enable DASH for Cross-Platform Support
4. Serve Streams via HTTP
首頁 運維 Nginx 如何使用nginx與HLS或DASH流式傳輸內容?

如何使用nginx與HLS或DASH流式傳輸內容?

Jul 29, 2025 am 02:05 AM
nginx 串流媒體

要使用Nginx通過HLS或DASH流媒體傳輸內容,需安裝RTMP模塊並進行正確配置。 1. 安裝帶有nginx-rtmp-module的Nginx,並在配置文件中添加RTMP服務器塊以接收RTMP流並轉換為HLS和DASH格式;2. 配置HLS輸出路徑和分段時長,確保目錄可寫並支持多碼率適配;3. 啟用DASH並指定輸出路徑,生成.mpd和.mp4文件,但需注意其在Safari上不兼容;4. 通過HTTP服務HLS和DASH文件,設置Nginx的location塊指向對應路徑並禁用緩存,最終可通過標準URL訪問流內容。

How to use Nginx to stream content with HLS or DASH?

Streaming content using Nginx with HLS (HTTP Live Streaming) or DASH (Dynamic Adaptive Streaming over HTTP) is actually a pretty common setup, especially for live video delivery. The trick is to use Nginx with the right modules and configuration. Let's break it down.


1. Set up Nginx with RTMP Module for Ingest

Before you can stream via HLS or DASH, you need to get your source video into Nginx. That's where the RTMP module comes in — it allows Nginx to accept incoming RTMP streams from encoders like OBS or FFmpeg.

To do this:

  • Install Nginx with the nginx-rtmp-module . This usually means compiling Nginx from source or installing a pre-built package that includes the module.
  • Add an RTMP block in your Nginx config:
 rtmp {
    server {
        listen 1935;
        chunk_size 4096;

        application live {
            live on;
            record off;

            # Push to HLS
            hls on;
            hls_path /tmp/hls;
            hls_fragment 4s;

            # Push to DASH
            dash on;
            dash_path /tmp/dash;
            dash_fragment 4s;
        }
    }
}

This tells Nginx to accept RTMP input on port 1935 under the live app, and convert it into both HLS and DASH formats.


2. Configure HLS Output for Compatibility

HLS works well across Apple devices and many modern players. It breaks the stream into small .ts chunks and maintains a playlist file ( m3u8 ) that clients use to switch between quality levels.

Key points:

  • Make sure the hls_path directory exists and is writable by Nginx.
  • Use short fragment durations (like 4 seconds) for lower latency — but keep in mind shorter segments mean more requests.
  • You can set multiple bitrates if doing adaptive streaming. For example:
 exec ffmpeg … -vf scale=1280x720 -b:v 2048k -f flv rtmp://localhost/live/stream_low \
      ffmpeg … -vf scale=1920x1080 -b:v 5120k -f flv rtmp://localhost/live/stream_high;

Then define them in the HLS playlist manually or via FFmpeg.

You'll end up with files like:

 /stream.m3u8
/stream_720p.m3u8
/stream_1080p.m3u8

And those can be played back in any compatible player.


3. Enable DASH for Cross-Platform Support

DASH is great for cross-platform support and offers similar adaptive bitrate features. Unlike HLS, which was originally Apple-only, DASH is an open standard.

To enable DASH:

  • Make sure dash on; is set in your RTMP application.
  • Set a valid dash_path that Nginx can write to.
  • The output will be .mpd and .mp4 segment files:
 /manifest.mpd
/segment-stream0-00001.mp4
/segment-stream0-00002.mp4

You'll need a DASH-compatible player like video.js with hls.js or dash.js .

One thing to note: DASH doesn't work natively in Safari, so if you're targeting iOS users, HLS is still the safer bet.


4. Serve Streams via HTTP

Once the stream is processed into HLS or DASH format, you just need to serve the files via HTTP.

Add something like this to your main Nginx config:

 server {
    listen 80;

    location /hls {
        types {}
        root /tmp/hls;
        add_header Cache-Control no-cache;
    }

    location /dash {
        types {}
        root /tmp/dash;
        add_header Cache-Control no-cache;
    }
}

Now, your streams are accessible at:

  • http://yourdomain/hls/stream.m3u8
  • http://yourdomain/dash/manifest.mpd

Make sure to test these URLs with a player or tool like VLC or ffplay to verify everything is working.


Basically, that's how you use Nginx to stream content with HLS or DASH. It's not too complicated once you've got the modules and paths set up right — just make sure permissions and paths are correct, and you're good to go.

以上是如何使用nginx與HLS或DASH流式傳輸內容?的詳細內容。更多資訊請關注PHP中文網其他相關文章!

本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn

熱AI工具

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Clothoff.io

Clothoff.io

AI脫衣器

Video Face Swap

Video Face Swap

使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱工具

記事本++7.3.1

記事本++7.3.1

好用且免費的程式碼編輯器

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

禪工作室 13.0.1

禪工作室 13.0.1

強大的PHP整合開發環境

Dreamweaver CS6

Dreamweaver CS6

視覺化網頁開發工具

SublimeText3 Mac版

SublimeText3 Mac版

神級程式碼編輯軟體(SublimeText3)

熱門話題

PHP教程
1596
276
php寫完代碼怎麼執行 php代碼執行的幾種常見方式 php寫完代碼怎麼執行 php代碼執行的幾種常見方式 May 23, 2025 pm 08:33 PM

PHP代碼可以通過多種方式執行:1.使用命令行,直接輸入“php文件名”執行腳本;2.通過Web服務器,將文件放入文檔根目錄並通過瀏覽器訪問;3.在IDE中運行,利用內置調試工具;4.使用在線PHP沙箱或代碼執行平台進行測試。

安裝Nginx後配置文件路徑及初始設置 安裝Nginx後配置文件路徑及初始設置 May 16, 2025 pm 10:54 PM

了解Nginx的配置文件路徑和初始設置非常重要,因為它是優化和管理Web服務器的第一步。 1)配置文件路徑通常是/etc/nginx/nginx.conf,使用nginx-t命令可以查找並測試語法。 2)初始設置包括全局設置(如user、worker_processes)和HTTP設置(如include、log_format),這些設置允許根據需求進行定制和擴展,錯誤配置可能導致性能問題和安全漏洞。

linux如何限制用戶資源? ulimit怎麼配置? linux如何限制用戶資源? ulimit怎麼配置? May 29, 2025 pm 11:09 PM

Linux系統通過ulimit命令限制用戶資源,防止資源過度佔用。 1.ulimit是shell內置命令,可限製文件描述符數(-n)、內存大小(-v)、線程數(-u)等,分為軟限制(當前生效值)和硬限制(最高上限)。 2.臨時修改直接使用ulimit命令,如ulimit-n2048,但僅對當前會話有效。 3.永久生效需修改/etc/security/limits.conf及PAM配置文件,並添加sessionrequiredpam_limits.so。 4.systemd服務需在unit文件中設置Lim

配置Nginx服務開機自啟動的具體步驟 配置Nginx服務開機自啟動的具體步驟 May 16, 2025 pm 10:39 PM

Nginx配置開機自啟動的步驟如下:1.創建systemd服務文件:sudonano/etc/systemd/system/nginx.service,並添加相關配置。 2.重新加載systemd配置:sudosystemctldaemon-reload。 3.啟用Nginx開機自啟動:sudosystemctlenablenginx。通過這些步驟,Nginx會在系統啟動時自動運行,確保網站或應用的可靠性和用戶體驗。

Debian Nginx配置技巧有哪些 Debian Nginx配置技巧有哪些 May 29, 2025 pm 11:06 PM

在Debian系統上配置Nginx時,以下是一些實用的技巧:配置文件的基本結構全局設置部分:定義影響整個Nginx服務的行為參數,比如工作線程數量及運行用戶權限。事件處理部分:決定Nginx如何應對網絡連接,是提升性能的關鍵配置。 HTTP服務部分:包含大量與HTTP服務相關的設定,可內嵌多個server和location塊。核心配置選項worker_connections:定義每個工作線程所能處理的最大連接數,通常設為1024。 multi_accept:激活多連接接收模式,增強並發處理的能力。 s

配置PhpStorm與Docker容器化開發環境 配置PhpStorm與Docker容器化開發環境 May 20, 2025 pm 07:54 PM

通過Docker容器化技術,PHP開發者可以利用PhpStorm提高開發效率和環境一致性。具體步驟包括:1.創建Dockerfile定義PHP環境;2.在PhpStorm中配置Docker連接;3.創建DockerCompose文件定義服務;4.配置遠程PHP解釋器。優點是環境一致性強,缺點包括啟動時間長和調試複雜。

Debian Apache2的SEO優化技巧有哪些 Debian Apache2的SEO優化技巧有哪些 May 28, 2025 pm 05:03 PM

DebianApache2的SEO優化技巧涵蓋多個層面,以下是一些關鍵方法:關鍵詞研究:利用工具(如關鍵詞魔術工具)挖掘頁面的核心及輔助關鍵詞。優質內容創作:產出有價值且原創的內容,內容需經過深入調研,確保語言流暢且格式清晰。內容排版與結構優化:運用標題和小標題引導閱讀。編寫簡潔明了的段落和句子。利用列表展示重點信息。結合圖片、視頻等多媒體增強表現力。留白設計提昇文本易讀性。技術層面SEO改進:robots.txt文件:規定搜索引擎爬蟲的訪問權限。加速網頁加載:借助緩存機制和Apache配置優化

如何在Debian上實現Docker的自動化部署 如何在Debian上實現Docker的自動化部署 May 28, 2025 pm 04:33 PM

在Debian系統上實現Docker的自動化部署可以通過多樣的方法來完成,以下是詳細的步驟指南:1.安裝Docker首先,確保你的Debian系統保持最新狀態:sudoaptupdatesudoaptupgrade-y接著,安裝必要的軟件包以支持APT通過HTTPS訪問倉庫:sudoaptinstallapt-transport-httpsca-certificatescurlsoftware-properties-common-y導入Docker的官方GPG密鑰:curl-

See all articles