Nginx+Nginx-rtmp-module做在线直播服务器及在线录播

nginx配置

worker_processes  1;

error_log  logs/error.log debug;

events {
    worker_connections  1024;
}

rtmp {
    server {
        listen 1935;

        application hls {
             live on;  #启用rtmp直播
                       #地址为rtmp://[server]:[rtmp_port]/[app]/[stream]
             on_publish http://127.0.0.1/auth_client.php; 
             hls on;   #启用hls直播
                       #地址为http://[server]:[http_port]/[app]/[stream].m3u8
                       #需要配合下面http段设置使用
             hls_path nginx-rtmp-module/tmp/app/;
             hls_fragment 5s;
             recorder rec {  #启用录制
               record all manual;  #手动控制录制启停
               record_suffix _rec.flv;
               record_path nginx-rtmp-module/tmp/rec/;  #录制保存地址
               record_unique on;
           }
       }
       application vod2{  #rtmp点播
               play nginx-rtmp-module/tmp/rec/;    
       }
    }
}

http {
    server {
        listen      80;

        location /stat {  #服务器状态
            rtmp_stat all;
            rtmp_stat_stylesheet stat.xsl;
        }

        location /stat.xsl {
            root nginx-rtmp-module/;
        }

        location /control { #控制器
            rtmp_control all;
        }
        
    location /hls/ {  #hls直播地址
           #server hls fragments
           types{
             application/vnd.apple.mpegurl m3u8;
             video/mp2t ts;
           }
           alias nginx-rtmp-module/tmp/app/;
           expires -1;
        }

        location /vod/{  #hls点播地址
            alias nginx-rtmp-module/tmp/rec/;
        }

        location / {
            root nginx-rtmp-module/test/www/;
        }

        location ~ \.mp4$ {
            mp4;
        }

        location ~ \.php$ {
            root /usr/local/nginx/html;
            fastcgi_pass 127.0.0.1:9000;
            fastcgi_index index.php;
            fastcgi_param SCRIPT_FILENAME /usr/local/nginx/html$fastcgi_script_name;
            include fastcgi_params;
        }
    }
}

测试

开始录制

http://127.0.0.1:18080/control/record/start?app=hls&name=mystream&rec=rec

停止录制

http://127.0.0.1:18080/control/record/stop?app=hls&name=mystream&rec=rec

推流地址:

rmtp://127.0.0.1:1935/hls/yourname

直播地址:

rmtp://127.0.0.1:1935/hls/yourname
http://127.0.0.1:18080/hls/youname.m3u8

MP4拖动播放

.mp4?start=60
.mp4?start=60&end=180

auth_client.php

// ?user=user&pass=pass

$user = isset($_GET['user']) ? $_GET['user'] : '';
$pass = isset($_GET['pass']) ? $_GET['pass'] : '';

if (empty($user) || empty($pass)) {
    echo "wrong query input";
    header('HTTP/1.0 404 Not Found');
    exit();
}

$saveuser = user;
$savepass = pass;

if (strcmp($user, $saveuser) == 0 && strcmp($pass, $savepass) == 0) {
    echo "Username and Password OK";
} else {
    echo "Username or Password wrong";
    header('HTTP/1.0 404 Not Found');
    exit();
}
最后修改时间为:2016 年 11 月 20 日 11 时 27 分 AM
如果觉得我的文章对你有用,请随意赞赏

1 条评论

  1. 陈序猿

    很感谢作者,解决了直播的录播这一大块,学到很多 感谢感谢

发表评论