首页 > 解决方案 > Nginx RTMP 动态流名

问题描述

我正在尝试创建一个简单的 RTMP/HLS 服务器,以便能够流式传输和查看流,但是,我无法更改nginx-rtmp-module(模块)保存数据的目录的名称。

一切正常,我可以流式传输到服务器,然后在浏览器中查看 HLS 流,但是,唯一的方法是输入Stream Key作为服务器上的目标文件夹以获取 m3u8 文件。

目前我有以下设置:

  1. 流密钥在服务器上生成,然后显示给客户端
  2. 客户端输入:rtmp://live.local/live 作为端点,然后插入步骤 1 中生成的密钥
  3. 在请求启动流时,模块调用on_publishhttp ://app.local/stream/start,首先,它检查数据库中是否存在密钥,如果存在,则为流创建新的数据库条目(以UUID为主键),接收创建entry获取的key并返回给脚本
  4. 脚本取第 3 步得到的 UUID,然后使用 SHA-512 算法生成唯一字符串(原计划用作目录名)
  5. 执行散列后,脚本返回 301 和位置标头
  6. 流本身现在已重命名,但存储文件的文件夹仍然是 Stream Key,而不是在步骤 4 中获得并在步骤 5 中返回的散列值。

有谁知道如何:

  1. 重命名流(我想我已经这样做了)
  2. 重命名流文件的目录
  3. 或者有没有办法动态重写所有请求,例如,客户端请求http://live.local/stream/here_is_the_hashed_uuid/index.m3u8,php获取哈希值,获取活动流列表,哈希每个流的uuid如果匹配,则返回另一个目录中的文件

PS:只是我现在拥有的和我想要实现的一个小例子

流密钥: test_stream_key_user_1 散列
密钥: 73E179A6DB3796A3120319BFE80763427A2122253A5C1D347461268304B28CEB98CE3813DDF5FA8B7173937ED9386169FD3BF8E8C375B1443

Current directory naming: /tmp/hls/test_stream_key_user_1/index.m3u8
Desired directory naming: /tmp/hls/73E179A6DB3796A3120319BFE80763427A2122253A5C1D347461268304B28CEB98CE3813DDF5FA8B7173937ED9386169FD3BF8E8C3765BC53BB151C7F5B1431E/index.m3u8

PPS:我认为我的 nginx 配置文件可能会有所帮助。

RTMP 配置

server {
listen 1935;
ping 30s;
notify_method get;
notify_update_timeout 10s;

application live {
    live on;
    hls on;
    hls_nested on;
    hls_path /tmp/hls;
    hls_fragment 3;
    hls_playlist_length 60;
    on_publish http://app.local/api/stream/start;
    on_done http://app.local/api/stream/stop;
    on_update http://app.local/api/stream/update;
    }
}

服务器配置(APP)

server {
    listen 80;
    listen [::]:80;

    root #replaced#;

    index index.php index.html index.htm index.nginx-debian.html;

    server_name app.local www.app.local;

    location / {
            proxy_pass http://localhost:3000;
            proxy_http_version 1.1;
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection 'upgrade';
            proxy_set_header Host $host;
            proxy_cache_bypass $http_upgrade;
    }

    # PHP-FPM Configuration Nginx
    location ~ \.php$ {
            try_files $uri =404;
            fastcgi_split_path_info ^(.+\.php)(/.+)$;
            fastcgi_pass unix:/var/run/php/php7.3-fpm.sock;
            fastcgi_index index.php;
            fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
            include fastcgi_params;
    }

    location /api {
            try_files $uri $uri/ /index.php?$query_string;
    }

    location /sitemap.xml {
            try_files $uri $uri/ /index.php?$query_string;
    }

    location /login {
            try_files $uri $uri/ /index.php?$query_string;
    }

    location /broadcasting {
            try_files $uri $uri/ /index.php?$query_string;
    }


    location ~ /\.ht {
            deny all;
    }
}

服务器配置(实时)

server {
    listen 80;
    listen [::]:80;

root #replaced#;

    index index.html index.htm index.nginx-debian.html;

    server_name live.local www.live.local;

location /stat {
    rtmp_stat all;
    rtmp_stat_stylesheet original_stat.xsl;
}

location /control {
    rtmp_control all;
}

location ~ \.php$ {
            try_files $uri =404;
            fastcgi_split_path_info ^(.+\.php)(/.+)$;
            fastcgi_pass unix:/var/run/php/php7.3-fpm.sock;
            fastcgi_index index.php;
            fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
            include fastcgi_params;
    }

    location /stream {
            try_files $uri $uri/ /index.php?$query_string;
    }

location /hls {
    add_header 'Access-Control-Allow-Origin' '*' always;
            add_header 'Access-Control-Expose-Headers' 'Content-Length,Content-Range';
            add_header 'Access-Control-Allow-Headers' 'Range';

    if ($request_method = 'OPTIONS') {
        add_header 'Access-Control-Allow-Headers' 'Range';
        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 /tmp;
    add_header Cache-Control no-cache;
    }

location /dash {
    root /tmp;
    add_header Cache-Control no-cache;
    add_header 'Access-Control-Allow-Origin' '*' always;
    }
}

标签: phpnginxrtmp

解决方案


推荐阅读