개발/기타

[RTSP] nodejs로 rtsp 실시간 스트리밍 서버구축

아크투어 2023. 5. 4. 07:02
반응형

rtsp 스트리밍 서버구축

 

실시간 스트리밍 프로토콜(Real Time Streaming Protocol, RTSP)은 스트리밍 미디어 서버를 제어할 목적으로 엔터테인먼트, 통신 시스템에 사용하도록 설계된 네트워크 제어 프로토콜이다. 

 

nodejs rtsp
nodejs rtsp

 

1. 개요

  • node에서 제공하는 node-rtsp-stream 를 사용하여 실시간으로 카메라 영상을 볼수있는 웹사이트를 만들기
  • 실제로 사용할 주소는 rtsp://... 형식이다. 카메라 제조사마다 url이 다르게 제공되니 참고한다.
  • Linux 우분투 환경에서 구축한 샘플소스이다.

 

2. node설치

  • 아래명령어를 실행한다.
  • 설치이후 node -v 입력하여 제대로 설치가 되었는지 확인한다.
$ curl -sL https://deb.nodesource.com/setup_14.x | sudo bash -E -

$ sudo apt install -y nodejs

 

 

3. RTSP관련 패키지 설치

$ sudo apt-get update

$ sudo apt-get install ffmpeg

 

 

4. 폴더생성후 npm 패키지 install

  • nodejs라는 폴더생성
  • /www/rtsp/nodejs 에서 npm install node-rtsp-stream 실행
  • /www/rtsp/nodejs 에서 npm install ws 실행
$ mkdir /www/rtsp/nodejs

$ npm install node-rtsp-stream

$ npm install ws

 

 

5. index.js 생성

Stream = require('node-rtsp-stream')
stream = new Stream({
    name: 'name',
    streamUrl: 'rtsp://.......',
    wsPort: 9999,
    ffmpegOptions: { // options ffmpeg flags
        '-stats': '', // an option with no neccessary value uses a blank string
        '-r': 30 // options with required values specify the value after the key
    }
})

 

 

6. index.html 생성

  • Linux서버 아이피를 WebSocket 항목에 입력한다.
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jsmpeg/0.1/jsmpg.js"></script>
</head>
<body>
<h3>RTSP 영상보기</h3>
<canvas style="width:800px; height: 600px;"></canvas>

<script>
    var client = new WebSocket('ws://127.0.0.1:9999'); //리눅스 서버주소
    var canvas = document.querySelector('canvas');
    var player = new jsmpeg(client, {
        canvas: canvas
    });
</script>
</body>
</html>

 

 

7. mpeg1muxer.js 파일수정

  • 대략 23번째줄에 this.spawnOptions = [] 항목을 아래처럼 한다.
  • 해당파일은 node_modules > node-rtsp-stream > mpeg1muxer.js 에 존재한다.
this.spawnOptions = [
    "-i",
    this.url,
    '-f',
    'mpegts',
    '-codec:v',
    'mpeg1video',
    // additional ffmpeg options go here
    ...this.additionalFlags,
    '-'
  ]

 

 

8. index.js 백그라운드 실행하기

  • 아래명령어로 실행하면 아래 이미지처럼 영상정보를 실시간으로 수신받는다.
  • 앞에서 작성한 index.html파일을 실행해서 영상을 확인한다.
& nohup node index.js > rtsp.log &

rtsp nodejs 실행
rtsp nodejs 실행

 

반응형