Nginx Intro - RTMP HLS

来源:互联网 发布:thunder mac破解 编辑:程序博客网 时间:2024/05/01 19:17



1. Nginx configuration


#user  nobody;
worker_processes  1;
master_process off;
daemon off;
error_log  logs/error.log  debug;
error_log  logs/error.log  notice;
error_log  logs/error.log  info;
#pid        logs/nginx.pid;

events {
    worker_connections  1024;
}
rtmp {
     server {
           listen 1935;
           chunk_size 4096;
           application live {
              live on;
              record off;
           }
           application hls {
            live on;
            hls on;
            hls_path /tmp/hls;
            hls_fragment 5s;
          }
    }
}
http {
    include       mime.types;
    default_type  application/octet-stream;
    #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
    #                  '$status $body_bytes_sent "$http_referer" '
    #                  '"$http_user_agent" "$http_x_forwarded_for"';
    #access_log  logs/access.log  main;
    sendfile        on;
    #tcp_nopush     on;
    #keepalive_timeout  0;
    keepalive_timeout  65;
    #gzip  on;
    server {
        listen       80;
        server_name  localhost;
        #charset koi8-r;
        #access_log  logs/host.access.log  main;
        location / {
            root   html;
            index  index.html index.htm;
        }
        location /hls {
            # Serve HLS fragments
            types {
                application/vnd.apple.mpegurl m3u8;
                video/mp2t ts;
            }
            root /tmp;
            add_header Cache-Control no-cache;
    if ($request_method = 'OPTIONS') {
    add_header 'Access-Control-Allow-Origin' '*';
    add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
    #
    # Custom headers and headers various browsers *should* be OK with but aren't
    #
    add_header 'Access-Control-Allow-Headers' 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type';
    #
    # Tell client that this pre-flight info is valid for 20 days
    #
    add_header 'Access-Control-Max-Age' 1728000;
    add_header 'Content-Type' 'text/plain charset=UTF-8';
    add_header 'Content-Length' 0;
    return 204;
    }
    if ($request_method = 'POST') {
    add_header 'Access-Control-Allow-Origin' '*';
    add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
    add_header 'Access-Control-Allow-Headers' 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type';
    }
    if ($request_method = 'GET') {
    add_header 'Access-Control-Allow-Origin' '*';
    add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
    add_header 'Access-Control-Allow-Headers' 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type';
    }
        }
        #error_page  404              /404.html;
        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ \.php$ {
        #    proxy_pass   http://127.0.0.1;
        #}
        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        #location ~ \.php$ {
        #    root           html;
        #    fastcgi_pass   127.0.0.1:9000;
        #    fastcgi_index  index.php;
        #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
        #    include        fastcgi_params;
        #}
        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #    deny  all;
        #}
    }

    # another virtual host using mix of IP-, name-, and port-based configuration
    #
    #server {
    #    listen       8000;
    #    listen       somename:8080;
    #    server_name  somename  alias  another.alias;
    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}

    # HTTPS server
    #
    #server {
    #    listen       443 ssl;
    #    server_name  localhost;
    #    ssl_certificate      cert.pem;
    #    ssl_certificate_key  cert.key;
    #    ssl_session_cache    shared:SSL:1m;
    #    ssl_session_timeout  5m;
    #    ssl_ciphers  HIGH:!aNULL:!MD5;
    #    ssl_prefer_server_ciphers  on;
    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}
}



2.  push stream by ffmpeg

ffmpeg -f mpegts -re -i war.ts -c:v libx264 -s 640x360 -c:a libfdk_aac -ar 48000 -f flv rtmp://localhost/hls/test1

ffmpeg -f mpegts -re -i war.ts -c:v libx264 -s 640x360 -c:a libmp3lame -ar 48000 -f flv rtmp://localhost/hls/test

Notes: As far as mp3, flv not support mp3 with sample rate 48000. Following errors will be output.
[flv @ 0x96e340] FLV does not support sample rate 48000, choose from (44100, 22050, 11025)
[flv @ 0x96e340] Audio codec mp3 not compatible with flv

If you change its sample rate from 48000 to 44100 as followings, it will be work normally.

ffmpeg -f mpegts -re -i war.ts -c:v libx264 -s 640x360 -c:a libmp3lame -ar 44100 -f flv rtmp://localhost/hls/test




3. pull stream by vlc

http://192.168.1.66/hls/test1.m3u8


///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

#
# Wide-open CORS config for nginx
#
location / {
     if ($request_method ='OPTIONS') {
        add_header 'Access-Control-Allow-Origin''*';
        #
        # Om nom nom cookies
        #
        add_header 'Access-Control-Allow-Credentials''true';
        add_header 'Access-Control-Allow-Methods''GET, POST, OPTIONS';
        #
        # Custom headers and headers various browsers *should* be OK with but aren't
        #
        add_header 'Access-Control-Allow-Headers''DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type';
        #
        # Tell client that this pre-flight info is valid for 20 days
        #
        add_header 'Access-Control-Max-Age'1728000;
        add_header 'Content-Type''text/plain charset=UTF-8';
        add_header 'Content-Length'0;
        return 204;
     }
     if ($request_method ='POST') {
        add_header 'Access-Control-Allow-Origin''*';
        add_header 'Access-Control-Allow-Credentials''true';
        add_header 'Access-Control-Allow-Methods''GET, POST, OPTIONS';
        add_header 'Access-Control-Allow-Headers''DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type';
     }
     if ($request_method ='GET') {
        add_header 'Access-Control-Allow-Origin''*';
        add_header 'Access-Control-Allow-Credentials''true';
        add_header 'Access-Control-Allow-Methods''GET, POST, OPTIONS';
        add_header 'Access-Control-Allow-Headers''DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type';
     }
}

0 0