Skip to content

Instantly share code, notes, and snippets.

@ufologist
Last active September 21, 2023 09:01
Show Gist options
  • Save ufologist/1ec2b820738a95b25cf7d805ed615949 to your computer and use it in GitHub Desktop.
Save ufologist/1ec2b820738a95b25cf7d805ed615949 to your computer and use it in GitHub Desktop.
Web 前端如何播放 HLS(.m3u8) 视频
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Mobile HLS video</title>
</head>
<body>
<h1>Mobile HLS video</h1>
<ul>
<li>移动端可直接通过 &lt;video&gt; 标签来播放 <code>.m3u8</code> 格式的视频</li>
<li>PC端需要通过其他手段(例如 videojs-contrib-hls)来解码 <code>.m3u8</code> 格式的视频, 才能够通过 &lt;video&gt; 标签或者 flash 来播放</li>
</ul>
<h2>移动端播放 HLS(<code>.m3u8</code>) 视频</h2>
<video width="300" height="200"
playsinline webkit-playsinline
autoplay controls preload="auto"
x-webkit-airplay="true" x5-video-player-fullscreen="true" x5-video-player-typ="h5">
<source src="http://live.zzbtv.com:80/live/live123/800K/tzwj_video.m3u8" type="application/x-mpegURL">
</video>
<h2>参考</h2>
<p><a href="https://m.zhanqi.tv/1128054">xx直播</a></p>
<pre>&lt;video class="vjs-tech" width="100%" height="100%"
controls="controls" autoplay="autoplay"
x-webkit-airplay="true" x5-video-player-fullscreen="true"
preload="auto" playsinline="true" webkit-playsinline
x5-video-player-typ="h5"&gt;
&lt;source type="application/x-mpegURL" src="http://dlhls.cdn.zhanqi.tv/zqlive/22578_yKdJM.m3u8"&gt;
&lt;/video&gt;</pre>
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>PC HLS video</title>
<link href="http://cdn.bootcss.com/video.js/6.0.0-RC.5/alt/video-js-cdn.min.css" rel="stylesheet">
</head>
<body>
<h1>PC 端播放 HLS(<code>.m3u8</code>) 视频</h1>
<p>借助 video.js 和 videojs-contrib-hls</p>
<p>由于 videojs-contrib-hls 需要通过 XHR 来获取解析 m3u8 文件, 因此会遭遇跨域问题, 请设置浏览器运行跨域</p>
<video id="hls-video" width="300" height="200" class="video-js vjs-default-skin"
playsinline webkit-playsinline
autoplay controls preload="auto"
x-webkit-airplay="true" x5-video-player-fullscreen="true" x5-video-player-typ="h5">
<!-- 直播的视频源 -->
<source src="http://live.zzbtv.com:80/live/live123/800K/tzwj_video.m3u8" type="application/x-mpegURL">
<!-- 点播的视频源 -->
<!--<source src="http://devstreaming.apple.com/videos/wwdc/2015/413eflf3lrh1tyo/413/hls_vod_mvp.m3u8" type="application/x-mpegURL">-->
</video>
<script src="http://cdn.bootcss.com/video.js/6.0.0-RC.5/video.js"></script>
<!-- PC 端浏览器不支持播放 hls 文件(m3u8), 需要 videojs-contrib-hls 来给我们解码 -->
<script src="http://cdn.bootcss.com/videojs-contrib-hls/5.3.3/videojs-contrib-hls.js"></script>
<script>
// XMLHttpRequest cannot load http://xxx/video.m3u8. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://192.168.198.98:8000' is therefore not allowed access.
// 由于 videojs-contrib-hls 需要通过 XHR 来获取解析 m3u8 文件, 因此会遭遇跨域问题, 请设置浏览器运行跨域
var player = videojs('hls-video');
player.play();
</script>
</body>
</html>
@andarm
Copy link

andarm commented Jul 24, 2018

  • 请教一下,使用下面的推流方式,实现的是直播方式,无进度条。
    ffmpeg.exe -re -fflags +genpts -stream_loop -1 -i example\1.mp4 -acodec copy -vcodec copy -f flv rtmp://10.169.38.195:1935/live/movie

如果想实现点播,该如何修改?


@a1341450200
Copy link

为什么我显示的是黑屏呢,在vlc打开m3u8文件是没问题的

@hyxiangwsl
Copy link

为什么我显示的是黑屏呢,在vlc打开m3u8文件是没问题的

同问 请问解决了吗

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment