詳解如何基于Nginx搭建流媒體服務器
HLS是最常見的視頻流媒體協(xié)議,HLS是一種自適應流媒體技術,可以根據(jù)用戶的設備和網(wǎng)絡條件對播放媒體內(nèi)容,以獲得最佳播放性能。
Nginx RTMP是一個Nginx插件,支持將RTMP和HLS流添加到媒體服務器。以ubuntu為力,下面介紹如何安裝使用nginx Rtmp 插件的步驟。

1.更新apt庫
apt-get update
2.安裝ffmpeg等所需要的軟件
apt-get install -y git build-essential ffmpeg libpcre3 libpcre3-dev libssl-dev zlib1g-dev
3.下載RTMP模塊
git clone https://github.com/sergey-dryabzhinsky/nginx-rtmp-module.git
4.下載并解壓Nginx
wget http://nginx.org/download/nginx-1.17.6.tar.gztar -xf nginx-1.17.6.tar.gzcd nginx-1.17.6
5.配置Nginx
拷貝一份nginx配置文件出來
mv /usr/local/nginx/conf/nginx.confnano /usr/local/nginx/conf/nginx.conf
將以下內(nèi)容復制到nginx.conf文件中
worker_processes auto;
events {
worker_connections 1024;
}
# RTMP configuration
rtmp {
server
{
listen 1935;
# Listen on standard RTMP
portchunk_size 4000;
application show
{
live on;
# Turn on HLS
hls on;
hls_path /mnt/hls/;
hls_fragment 3;
hls_playlist_length 60;
# disable consuming the stream from nginx as rtmpdeny
play all;
}
}
}
http {
sendfile off;
tcp_nopush on;
directio 512;
default_type application/octet-stream;
server {
listen 8080;
location / {
# Disable cache
add_header 'Cache-Control' 'no-cache';
# CORS setup
add_header 'Access-Control-Allow-Origin' '*' always;
add_header 'Access-Control-Expose-Headers' 'Content-Length';
# allow CORS preflight requests
if ($request_method = 'OPTIONS') {
add_header 'Access-Control-Allow-Origin' '*';
add_header 'Access-Control-Max-Age' 1728000;
add_header 'Content-Type' 'text/plain charset=UTF-8';
add_header 'Content-Length' 0;
return 204;
}
types {
application/dash+xml mpd;
application/vnd.apple.mpegurl m3u8;
video/mp2t ts;
}
root /mnt/;
}
}
}6.啟動Nginx
/usr/local/nginx/sbin/nginx
7.測試
該服務器可以從各種來源進行流式傳輸,包括靜態(tài)文件、網(wǎng)絡攝像頭等。由于上面的步驟中安裝了ffmpeg,我們可以將example-vid.mp4視頻文件流式傳輸?shù)絟ttp服務http://localhost/show/stream。
ffmpeg -re -i example-vid.mp4 -vcodec libx264 -vprofile baseline -g 30 -acodec aac -strict -2 -f flv rtmp://localhost/show/stream
8.最后
根據(jù)服務的需求,可以將http服務集成到您的應用程序或者網(wǎng)頁中。
到此這篇關于詳解如何基于Nginx搭建流媒體服務器的文章就介紹到這了,更多相關nginx搭建流媒體服務器內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
通過Nginx的proxy_set_header設置請求頭無效的解決
這篇文章主要介紹了通過Nginx的proxy_set_header設置請求頭無效的解決,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-12-12
windows10 系統(tǒng)配置nginx文件服務器的圖文教程
這篇文章主要介紹了windows10 系統(tǒng)配置nginx文件服務器的圖文教程,本文圖文并茂給大家介紹的非常詳細,具有一定的參考借鑒價值,需要的朋友可以參考下2019-12-12
Nginx proxy_set_header參數(shù)設置
本文主要介紹了Nginx proxy_set_header參數(shù)設置,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2023-09-09
Nginx限制特定IP訪問自己的網(wǎng)站實現(xiàn)的幾種方法
本文主要介紹了使用Nginx限制特定IP訪問自己的網(wǎng)站的幾種方法,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2024-12-12
Nginx一鍵安裝部署靜態(tài)網(wǎng)頁的過程詳解
這篇文章主要介紹了Nginx一鍵安裝部署靜態(tài)網(wǎng)頁,主要介紹nginx安裝和部署,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友參考下吧2022-06-06

