備忘録

備忘録

Node-Media-Serverを利用してストリーミングサーバを作る方法

Ⅰ. はじめに

タイトルの通り「Node-Media-Serverを利用してストリーミングサーバを作る方法」です。

Ⅱ. やり方

1. NodeJSをインストールする
curl -sL https://deb.nodesource.com/setup_14.x | bash -
apt-get install -y nodejs
2. Node-Media-Serverを初期化する
git clone https://github.com/illuspas/Node-Media-Server
cd Node-Media-Server
npm i
3. Node-Media-Serverの設定を変更する(任意)
$ vim app.js

const config = {
  http: {
    port: 80
  }
}
4. Node-Media-Serverを起動する
$ node app.js
5. サンプルプログラムを書く
<html>
  <head>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/flv.js/1.5.0/flv.min.js"></script>
  </head>
  <body>
    <video id="player" autoplay muted width=640 height=480></video>

    <script>
      const videoElement = document.getElementById('player')
      const flvPlayer = flvjs.createPlayer({
        type: 'flv',
        url: 'ws://10.0.0.2/live/live001.flv'
        isLive: true,
        enableStashBuffer: false // falseを指定すると低遅延になる
        stashInitialSize: 1024 * 128, // 128 KB
        fixAudioTimestampGap: false, // 音ズレ防止
        autoCleanupSourceBuffer: true
      })
      flvPlayer.attachMediaElement(videoElement)
      flvPlayer.load()
      flvPlayer.play()
    </script>
  </body>
</html>
6. OBS等で配信を開始する

f:id:kagasu:20210121131124p:plain

実行結果

f:id:kagasu:20210121131151p:plain

メモ

その他方法