본문 바로가기
JavaScript

[JavaScript] Video.js 로 m3u8 파일 재생하기

by teamnova 2022. 7. 16.
728x90

먼저 ffmpeg 프로그램으로 m3u8 파일과 ts 파일을 생성해줍니다.

ffmpeg -i test.mp4 -vcodec libx264
-start_number 0
-hls_time 10
-hls_list_size 0
-f hls
test.m3u8

인코딩 옵션은 공식 홈페이지에서 확인할 수 있습니다.

http://ffmpeg.org/ffmpeg-all.html#hls-2

 

ffmpeg Documentation

 

ffmpeg.org

 

 

<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>hls_test</title>
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/video.js/7.20.1/video-js.min.css"
    integrity="sha512-D/n19dhr+LY8HOoGuybX+bJYsZOFKb0r7YpAWBMPg5Hnke6lxdHl6q21d7qiwIsMgdSnBG9pSMQlwTtZ+6ISmw=="
    crossorigin="anonymous" referrerpolicy="no-referrer" />
  <script src="https://cdnjs.cloudflare.com/ajax/libs/video.js/7.20.1/video.min.js"
    integrity="sha512-xT61DWoPRoAa4ir+LqER5L4/zzuO5tQOQywZYqU8BaZVqolq78bRobozDxozcCZ6tmo92zetGVQHRp+sQHIi/g=="
    crossorigin="anonymous" referrerpolicy="no-referrer"></script>

</head>

<body>
  <video id="my_video" class="video-js" controls width="800" height="500">
    <source src="test.m3u8" type="application/x-mpegURL">
  </video>

  <script>
    var player = videojs('my_video');
    player.play();
  </script>

</body>

</html>

cdn으로 video.js 라이브러리를 참조하고

src="" 이 부분에 m3u8 파일의 경로를 적어줍니다.

 

결과

 

cdn은 이 사이트에서 복사할 수 있습니다.

https://cdnjs.com/libraries/video.js

 

video.js - Libraries - cdnjs - The #1 free and open source CDN built to make life easier for developers

An HTML5 and Flash video player with a common API and skin for both. - Simple. Fast. Reliable. Content delivery at its finest. cdnjs is a free and open-source CDN service trusted by over 12.5% of all websites, serving over 200 billion requests each month,

cdnjs.com