«

nginx搭建流媒体服务器

Sun 发布于 阅读:186


  1. 安装Nginx和相关模块
    首先,确保系统上安装了Nginx,并且安装了nginx-rtmp-module和nginx-mod-http-ffmpeg模块。
    在Ubuntu上安装:
    sudo apt-get update
    sudo apt-get install nginx libnginx-mod-rtmp
    在CentOS上安装:
    sudo yum install epel-release
    sudo yum install nginx nginx-mod-rtmp
  2. 配置Nginx
    在nginx的配置文件,nginx.conf的
    `
    user nginx;
    worker_processes auto;
    pid /run/nginx.pid;

events {
worker_connections 1024;
}

http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;

server {
    listen 80;
    server_name localhost;

    location /hls {
        types {
            application/vnd.apple.mpegurl m3u8;
            video/mp2t ts;
        }
        root /tmp;
        add_header Cache-Control no-cache;
    }
}

}

rtmp {
server {
listen 1935;
chunk_size 4096;

    application live {
        live on;
        record off;

        exec ffmpeg -i rtmp://localhost/live/stream -c:v libx264 -c:a aac -f flv rtmp://localhost/hls/stream;
    }

    application hls {
        live on;
        hls on;
        hls_path /tmp/hls;
        hls_fragment 10s;
        hls_playlist_length 60s;
    }
}

}`

  1. 启动Nginx
    保存配置文件并重启Nginx以应用更改。
    sudo systemctl restart nginx
  2. 推流RTSP到RTMP
    使用FFmpeg将RTSP流推送到Nginx的RTMP服务器。
    ffmpeg -i rtsp://your-rtsp-stream-url -c:v libx264 -c:a aac -f flv rtmp://localhost/live/stream
  3. 推流RTSP到RTMP
    访问HLS流
    Nginx会将RTMP流转换为HLS流,并存储在/tmp/hls目录下。您可以通过HTTP访问HLS流:
    http://your-server-ip/hls/stream.m3u8
    注意事项
  4. 权限问题:确保Nginx有足够的权限访问/tmp/hls目录。
  5. 防火墙设置:确保防火墙允许RTSP和RTMP端口的流量。
  6. FFmpeg版本:确保使用的FFmpeg版本支持所需的编解码器和协议。