Jiniya

WEB

nginx 로 간단한 스트리밍 서버 구축하기.

Ubuntu LTS(14.04) 에서 수동 컴파일 환경을 준비하는 것으로 시작합니다. 

apt-get install build-essential libpcre3 libpcre3-dev libssl-dev unzip software-properties-common 

이런거 한번 해줘야 컴파일 할 때 무리없습니다. 

적당히 디렉토리를 만들어서(저는 /usr/local/src 에 했습니다.) 

wget http://nginx.org/download/nginx-1.9.7.tar.gz 
tar xvzf nginx-1.9.7.tar.gz 
wget https://github.com/arut/nginx-rtmp-module/archive/master.zip 
unzip master.zip 
cd nginx-1.9.7/ 
./configure --with-http_ssl_module --with-http_stub_status_module --user=www-data --group=www-data --add-module=../nginx-rtmp-module-master 
make 
make install 
sudo chmod +x /etc/init.d/nginx 
sudo update-rc.d nginx defaults 
sudo vi /etc/init.d/nginx 
sudo service nginx start 


짜잔 nginx 설치 완료! rtmp 모듈도 설치 완료!! 
이제 hls 를 만들어야 합니다. 검색 해보니 ffmpeg 과 관련된 몇가지들이 필요하네요. 

sudo add-apt-repository ppa:kirillshkrogalev/ffmpeg-next 
sudo apt-get update 
sudo apt-get install ffmpeg 

일단 여기저기 뒤져서 쉽게 필요한 것만 설치하게 해주는 패키지 셋을 찾았습니다. 런치패드 짱짱맨! 
여튼 설치 이후에 테스트 해볼 mp4 파일을 업로드하고 

ffmpeg -i test.mp4 -hls_time 30 -s 1024x768 -c:v libx264 -b:v 528k -b:a 128k -ar 44100 -ac 2 -c:a aac -strict -2 ./test.m3u8 
으로 대략 샘플 만들고 생성된 파일을 nginx 설정에 맞춰서 이동. 

그리고 nginx.conf 에 
rtmp { 
    server { 
        listen 1935; 
        allow play all; 
        #다른 곳 찾아보시면 라이브 스트리밍 쏘는 설정이 있는데 그렇게 까지 필요한 건 아니라서 간단하게.. 
        application vod { 
            allow play all; 
            live on; 
            hls on; 
            hls_nested on; 
            hls_path /hls/vod; 
            hls_fragment 10s; 
        } 
    } 
} 

이런 rtmp 블록 하나 추가해 주시구요. server 블록 밑에 
        location /vod { 
            types { application/vnd.apple.mpegurl m3u8; } 
            alias hls/vod; 
            add_header Cache-Control no-cache; 
        } 
같은거 하나 추가해주세요. 

그리고 (nginx의 접근경로의 ) root 에 

cat crossdomain.xml 
=== 
<?xml version="1.0" ?> 
<cross-domain-policy>