체인의정석

비디오 스트리밍 - HLS 모듈 사용하기 본문

개발/backend(js,ts)

비디오 스트리밍 - HLS 모듈 사용하기

체인의정석 2025. 11. 19. 18:54
728x90

비디오 스트리밍의 경우

https://github.com/video-dev/hls.js

 

GitHub - video-dev/hls.js: HLS.js is a JavaScript library that plays HLS in browsers with support for MSE.

HLS.js is a JavaScript library that plays HLS in browsers with support for MSE. - video-dev/hls.js

github.com

다음 모듈을 써서 구현하면 된다.

<script src="https://cdn.jsdelivr.net/npm/hls.js@1"></script>
<!-- Or if you want the latest version from the main branch -->
<!-- <script src="https://cdn.jsdelivr.net/npm/hls.js@canary"></script> -->
<video id="video"></video>
<script>
  var video = document.getElementById('video');
  var videoSrc = 'https://test-streams.mux.dev/x36xhzz/x36xhzz.m3u8';
  if (Hls.isSupported()) {
    var hls = new Hls();
    hls.loadSource(videoSrc);
    hls.attachMedia(video);
  }
  // HLS.js is not supported on platforms that do not have Media Source
  // Extensions (MSE) enabled.
  //
  // When the browser has built-in HLS support (check using `canPlayType`),
  // we can provide an HLS manifest (i.e. .m3u8 URL) directly to the video
  // element through the `src` property. This is using the built-in support
  // of the plain video element, without using HLS.js.
  else if (video.canPlayType('application/vnd.apple.mpegurl')) {
    video.src = videoSrc;
  }
</script>

위와 같이 CDN을 사용하면 바로 구현이 가능하다.

참고로 html자체로만 띄우면 cors에러가 나기 때문에 pm2로 띄우거나

python3 -m http.server 8080 --bind 0.0.0.0

다음과 같이 서버를 띄워서 시도해야 한다.

728x90
반응형
Comments