Skip to content

Instantly share code, notes, and snippets.

@deedos
Created July 14, 2015 19:12
Show Gist options
  • Save deedos/2bf9564decd1923093a1 to your computer and use it in GitHub Desktop.
Save deedos/2bf9564decd1923093a1 to your computer and use it in GitHub Desktop.
Instalação e configuração de nginx-rtmp module e ffmpeg para transcoding

Instalação e Configuração de nginx-rtmp com live transcoding - S.O : Debian SID stable

  • Instalar ffmpeg da fonte: https://trac.ffmpeg.org/wiki/CompilationGuide/Ubuntu

  • Instalar nginx com módulo rtmp

      wget http://nginx.org/download/nginx-1.9.3.tar.gz
      git clone https://github.com/arut/nginx-rtmp-module
      wget ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.36.tar.bz2
      tar -xvf pcre-8.36.tar.bz2
      tar -xvf nginx-1.9.3.tar.gz
      cd nginx-1.9.3
      ./configure --add-module=../nginx-rtmp-module/ --with-pcre=../pcre-8.36
      make -j8
      sudo make install
    

Configuração de nginx

user  system-user; #substiuir usuário do sistema
worker_processes  1;

error_log  logs/error-ffmpeg.log ;

events {
    worker_connections  1024;
}


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

    server {
        listen       80;
        server_name  localhost;
	
        # rtmp stat
        # se formar usar stats de rtmp, configurar e inserir aqui. prefiro usar somente HLS e trabalhar somente com métricas em http

        # rtmp control 
        location /control {
            rtmp_control all;
        }
	
    	location /hls {

	    add_header 'Access-Control-Allow-Origin' "$http_origin";
	    add_header 'Access-Control-Allow-Methods' 'GET, OPTIONS';
	    add_header 'Access-Control-Allow-Headers' 'X-Requested-With, X-Requested-By, Origin, Authorization, Accept, Content-Type, Pragma, Content-Length';
	    add_header 'Access-Control-Allow-Credentials' 'true';
	    add_header Access-Control-Expose-Headers "Content-Length";
	
        # Serve HLS fragments
        types {
             application/vnd.apple.mpegurl m3u8;
             video/mp2t ts;
        }
            root /var/www;
            add_header Cache-Control no-cache;
        }
	
	location /dash {
        # Serve DASH fragments
           root /var/www;
           add_header Cache-Control no-cache;
        }
	}
	location /crossdomain.xml {
        alias /var/www/crossdomain.xml;
	}
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
       }
    }
}

rtmp {
  server {
        listen 1935;
        ping 30s;
        notify_method get;
        chunk_size 4096;
        
        # conf da aplicação "live"
        
        application live {
        live on;
        exec on;
        allow publish all;
        allow play all
        record all;
        record_path /media/record; #alterar para local de gravação
        record_suffix -%d-%b-%y-%T.flv;
	    # exec_record_done sh /home/ubuntu/copy.sh; se quisermps executar algum script, por exemplo, para mover arquivos assim que gravaçao acabar
 
 # corrigir path de ffmpeg compilado, assim como bitrate de vídeos, se necessário
 
        exec  /home/ubuntu/ffmpeg/bin/ffmpeg -i rtmp://localhost/live/$name
	    -vcodec libx264 -profile:v baseline -level 1 -vb 1800k -keyint_min 70 -sc_threshold 0 -x264opts "keyint=70:min-keyint=70:no-scenecut" -acodec copy -f flv rtmp://localhost/multi/$name_hi
    	-vcodec libx264 -profile:v baseline -level 1 -vb 650k -keyint_min 70 -sc_threshold 0 -x264opts "keyint=70:min-keyint=70:no-scenecut" -acodec libfdk_aac -ab 96k -f flv rtmp://localhost/multi/$name_mid
    	-vcodec libx264 -profile:v baseline -level 1 -vb 300k -keyint_min 70 -sc_threshold 0 -x264opts "keyint=70:min-keyint=70:no-scenecut" -acodec libfdk_aac -ab 96k -f flv rtmp://localhost/multi/$name_low;
        }
            
application multi {
        live on;
	exec on;
        allow publish all;
        allow play all;
        hls on;
        hls_path /var/www/hls;
        hls_fragment 5s;
	    #hls_cleanup off;
	    hls_continuous off;
        hls_playlist_length 60m;
        hls_fragment_slicing aligned;
	    hls_nested on;
	    hls_fragment_naming system;
        hls_variant _low BANDWIDTH=400000;
        hls_variant _mid BANDWIDTH=700000;
        hls_variant _hi  BANDWIDTH=2000000;
        #dash on;
        #dash_path /var/www/dash;
	
        } 
	} 
}
 

Crossdomain.xml, criá-lo em /var/www - este exemplo está aberto para o mundo - incluir domínio do player somente

<?xml version="1.0"?> <!DOCTYPE cross-domain-policy SYSTEM "http://www.adobe.com/xml/dtds/cross-domain-policy.dtd"> <cross-domain-policy> <site-control permitted-cross-domain-policies="all"/> <allow-access-from domain="*" secure="false"/> <allow-http-request-headers-from domain="*" headers="*" secure="false"/> </cross-domain-policy>

*** Rodar o nginx ***

    sudo /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/path_to_conf.conf

***Para parar: ***

    sudo /usr/local/nginx/sbin/nginx -s stop
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment