1. 程式人生 > >Nginx+Nginx-rtmp-module做線上直播伺服器及線上錄播

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();
}