直播影片 — HTML video, Video.JS , hls(附檔名 .m3u8)的範例
5 min readApr 9, 2024
使用 <video> 來播放 mp4影片、或是直播影片(在此以 HLS , hls為例。附檔名 .m3u8)
資料來源 https://juejin.cn/post/6903533860907302925 特別感謝
可運作的 範例一
<!doctype html>
<html lang="en" data-bs-theme="auto">
<head>
<link href="https://vjs.zencdn.net/8.10.0/video-js.css" rel="stylesheet" />
<!-- If you'd like to support IE8 (for Video.js versions prior to v7) -->
<script src="https://vjs.zencdn.net/ie8/1.1.2/videojs-ie8.min.js"></script>
<script>
videojs("my-video", {// 这里的 my-video 是 video 标签的 id
with: 1000,height: 800,//我指定了高宽度了
html5: {
hls: {
withCredentials: true,
},
},
});
</script>
</head>
<body>
<video id="my-video" class="video-js" controls
preload="auto"
width="640" height="264"
poster="影片的標題圖檔.jpg"
data-setup="{}">
<!-- 把地址换成自己的 MP4檔案
<source src="MY_VIDEO.mp4" type="video/mp4" /> -->
<!-- 直播(影片的測試網址) -->
<source
src="https://bitdash-a.akamaihd.net/content/sintel/hls/playlist.m3u8"
type="application/x-mpegURL">
<p class="vjs-no-js">
To view this video please enable JavaScript, and consider upgrading to a web browser that
<a href="https://videojs.com/html5-video-support/" target="_blank">supports HTML5 video</a>
</p>
</video>
<script src="https://vjs.zencdn.net/8.10.0/video.min.js"></script>
<br />
資料來源:https://juejin.cn/post/6903533860907302925 <br />
实时直播测试地址rtmp,hls https://www.jianshu.com/p/20f9e9bb89aa
</body>
</html>
可運作的 範例二
從 Video .JS 原廠說明的範例修改, https://videojs.com/getting-started/
但如果沒有搭配 範例一 的 <video>寫法,只憑原廠說明的寫法,則無法運作。
關於 <video>的使用說明,請參閱 https://www.letswrite.tw/video-js-player/
<!doctype html>
<html lang="en" data-bs-theme="auto">
<head>
<link href="https://vjs.zencdn.net/8.10.0/video-js.css" rel="stylesheet" />
<!-- If you'd like to support IE8 (for Video.js versions prior to v7) -->
<!-- <script src="https://vjs.zencdn.net/ie8/1.1.2/videojs-ie8.min.js"></script> -->
</head>
<body>
<video id="my-video" class="video-js" controls
preload="auto"
width="640" height="264"
poster="影片的標題圖檔.jpg"
data-setup="{}">
<source
src="https://bitdash-a.akamaihd.net/content/sintel/hls/playlist.m3u8"
type="application/x-mpegURL">
</video>
<!-- 可運作 -->
<!--
<video id="my-video" class="video-js" controls
preload="auto"
width="640" height="264"
poster="影片的標題圖檔.jpg"
data-setup="{}">
</video>
<script>
var player = videojs('my-video');
player.src({
src: 'https://bitdash-a.akamaihd.net/content/sintel/hls/playlist.m3u8',
type: 'application/x-mpegURL',
withCredentials: true
});
</script>
-->
<script src="https://vjs.zencdn.net/8.10.0/video.min.js"></script>
</body>
</html>
關於HLS , hls直播格式的說明,請參閱 https://ithelp.ithome.com.tw/articles/10203792