如何基于Nginx搭建流媒体服务器
创始人
2025-07-03 18:00:20
0

HLS是最常见的视频流媒体协议,HLS是一种自适应流媒体技术,可以根据用户的设备和网络条件对播放媒体内容,以获得最佳播放性能。

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

将以下内容复制到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.测试

该服务器可以从各种来源进行流式传输,包括静态文件、网络摄像头等。由于上面的步骤中安装了ffmpeg,我们可以将example-vid.mp4视频文件流式传输到http服务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.最后

根据服务的需求,可以将http服务集成到您的应用程序或者网页中。

相关内容

热门资讯

如何允许远程连接到MySQL数... [[277004]]【51CTO.com快译】默认情况下,MySQL服务器仅侦听来自localhos...
如何利用交换机和端口设置来管理... 在网络管理中,总是有些人让管理员头疼。下面我们就将介绍一下一个网管员利用交换机以及端口设置等来进行D...
施耐德电气数据中心整体解决方案... 近日,全球能效管理专家施耐德电气正式启动大型体验活动“能效中国行——2012卡车巡展”,作为该活动的...
Windows恶意软件20年“... 在Windows的早期年代,病毒游走于系统之间,偶尔删除文件(但被删除的文件几乎都是可恢复的),并弹...
20个非常棒的扁平设计免费资源 Apple设备的平面图标PSD免费平板UI 平板UI套件24平图标Freen平板UI套件PSD径向平...
德国电信门户网站可实时显示全球... 德国电信周三推出一个门户网站,直观地实时提供其安装在全球各地的传感器网络检测到的网络攻击状况。该网站...
着眼MAC地址,解救无法享受D... 在安装了DHCP服务器的局域网环境中,每一台工作站在上网之前,都要先从DHCP服务器那里享受到地址动...
为啥国人偏爱 Mybatis,... 关于 SQL 和 ORM 的争论,永远都不会终止,我也一直在思考这个问题。昨天又跟群里的小伙伴进行...