mpegts.js 是在 HTML5 上直接播放 MPEG2-TS 流的播放器,针对低延迟直播优化,可用于 DVB/ISDB 数字电视流或监控摄像头等的低延迟回放。
mpegts.js 基于 flv.js 改造而来。
mpegts.js 通过在 JavaScript 中渐进化解析 MPEG2-TS 流并实时转封装为 ISO BMFF (Fragmented MP4),然后通过 Media Source Extensions 把音视频数据喂入 HTML5 <video>
元素。
http://xqq.github.io/mpegts.js/demo/
.m2ts
文件(BDAV/BDMV)或 204 字节的 TS 流若在与页面不同的独立的服务器串流,必须设置 CORS 的 Access-Control-Allow-Origin
头。
参阅 cors.md。
npm install --save mpegts.js
npm install # install dev-dependences
npm install -g webpack-cli # install build tool
npm run build # packaged & minimized js will be emitted in dist folder
若在中国大陆可尝试 cnpm 镜像。
<script src="mpegts.js"></script>
<video id="videoElement"></video>
<script>
if (mpegts.getFeatureList().mseLivePlayback) {
var videoElement = document.getElementById('videoElement');
var player = mpegts.createPlayer({
type: 'mse', // could also be mpegts, m2ts, flv
isLive: true,
url: 'http://example.com/live/livestream.ts'
});
player.attachMediaElement(videoElement);
player.load();
player.play();
}
</script>
可使用 Simple Realtime Server 来测试 mpegts.js。
多段播放需要在 MediaDataSource
中提供文件列表。参阅 multipart.md
参阅 api.md
npm install # install dev-dependences
npm install -g webpack-cli # install build tool
npm run build:debug # packaged & minimized js will be emitted in dist folder
参阅 design.md
Copyright (C) 2021 magicxqq. All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.