지금까지는 저장된 영상을 스트리밍을 해봤다.
실시간 영상 데이터를 스트리밍 하려면 어떻게 해야 할까?
실시간 스트리밍을 위한 다양한 프로토콜이 있지만 보편적으로 RTMP, HLS를 사용한다.
worker_processes auto;
user root;
events {
worker_connections 1024;
}
# http 모듈
http {
server {
listen 8080;
root /tmp;
# 8080 포트에서 hls 재생
location /hls {
types {
application/vnd.apple.mpegurl m3u8;
video/mp2t ts;
}
}
}
}
# rmtp 모듈
rtmp {
server {
# rtmp 는 기본적으로 1935 포트를 사용한다.
listen 1935;
chunk_size 4096; # 청크 사이즈
# app 이름 live
application live {
live on;
# 들어온 rtmp 데이터를 ffmpeg를 이용해서 hls로 인코딩
# 화질별로 인코딩 $app은 app 이름 $name은 식별자(채널 같은 의미)
exec_push ffmpeg -i rtmp://localhost:1935/$app/$name -async 1 -vsync -1
-c:v libx264 -c:a aac -b:v 256k -b:a 64k -vf "scale=480:trunc(ow/a/2)*2" -tune zerolatency -preset superfast -crf 23 -f flv rtmp://localhost:1935/hls/$name_low
-c:v libx264 -c:a aac -b:v 768k -b:a 128k -vf "scale=720:trunc(ow/a/2)*2" -tune zerolatency -preset superfast -crf 23 -f flv rtmp://localhost:1935/hls/$name_mid
-c:v libx264 -c:a aac -b:v 1024k -b:a 128k -vf "scale=960:trunc(ow/a/2)*2" -tune zerolatency -preset superfast -crf 23 -f flv rtmp://localhost:1935/hls/$name_high
-c:v libx264 -c:a aac -b:v 6000k -r 60 -b:a 128k -vf "scale=1280:trunc(ow/a/2)*2" -tune zerolatency -preset superfast -crf 23 -f flv rtmp://localhost:1935/hls/$name_hd720
-c copy -f flv rtmp://localhost:1935/hls/$name_src;
drop_idle_publisher 10s;
}
# hls 파일로 저장~
application hls {
live on;
hls on;
hls_fragment 10s;
hls_playlist_length 10;
hls_path /tmp/hls; # 파일 위치
hls_variant _src BANDWIDTH=4096000;
hls_variant _hd720 BANDWIDTH=2048000;
hls_variant _high BANDWIDTH=1152000;
hls_variant _mid BANDWIDTH=448000;
hls_variant _low BANDWIDTH=288000;
}
}
}
FROM alpine:3.13.4 as builder
RUN apk add --update build-base git bash gcc make g++ zlib-dev linux-headers pcre-dev openssl-dev
# nginx, nginx-rtmp-module 다운
RUN git clone https://github.com/arut/nginx-rtmp-module.git && \
git clone https://github.com/nginx/nginx.git
# nginx를 설치하고 nginx-rtmp-module 추가
RUN cd nginx && ./auto/configure --add-module=../nginx-rtmp-module && make && make install
FROM alpine:3.13.4 as nginx
# ffmpeg 설치
RUN apk add --update pcre ffmpeg
# 빌드된 파일 및 설정파일 복사
COPY --from=builder /usr/local/nginx /usr/local/nginx
COPY nginx.conf /usr/local/nginx/conf/nginx.conf
# 실행
ENTRYPOINT ["/usr/local/nginx/sbin/nginx"]
CMD ["-g", "daemon off;"]
# 이미지 빌드
docker build -t nginx-rtmp .
# 실행
docker run -d -p 1935:1935 -p 8080:8080 --name nginx-rtmp nginx-rtmp
좀 더 예쁜 streamlabs 사용했다.
docker exec -it nginx-rtmp sh
http://localhost:8080/hls/test-stream-key_src.m3u8
감사합니다.