@smt-lib/m3u-parser

m3u index parser

Usage no npm install needed!

<script type="module">
  import smtLibM3uParser from 'https://cdn.skypack.dev/@smt-lib/m3u-parser';
</script>

README

HLS协议M3U文件解析库

m3u-parser

解释: HLS(http live stream), Apple 公司于2009年提出的基于 HTTP 协议的流媒体协议. 支持度: iOS、MacOSX 及 Apple TV 原生支持; Android 3.0+ 支持.

    它的传输内容包括两部份, 一是 M3U8 描述文件, 二是 TS 媒体文件,这里主要解决 M3U8 描述文件的解析问题

M3U描述文件

M3U8 是 M3U 的 UTF8 编码版本, M3U 是一种列表文件, 只提供一些描述信息.

M3U 文件中可以包含多个 tag, 每个 tag 的功能和属性如下:

#EXTM3U

文件的头部,必须是文件的第一行。

#EXTINF

指示多媒体文件的信息,包括播放时间和标题。

#EXT-X-MEDIA-SEQUENCE:

每一个media URI 在 PlayList 中只有唯一的序号, 相邻之间序号 +1, 一个 media URI 并不是必须要包含的, 如果没有, 默认为 0

#EXT-X-TARGETDURATION:

指定最大的媒体段时间长(秒). 所以#EXTINF中指定的时间长度必须小于或是等于这个最大值. 这个 tag 在整个 PlayList 文件中只能出现一次 (在嵌套的情况下, 一般有真正 TS url 的 m3u8 才会出现该 tag)

#EXTINF: [, ]</b></p> <p>duration 指定每个媒体段(ts)的持续时间(秒), 仅对其后面的URI有效, title 是下载资源的 url</p> <p><b>#EXT-X-PROGRAM-DATE-TIME</b></p> <p>将一个绝对时间或是日期和一个媒体段中的第一个 Sample 相关联, 只对下一个 meida URI 有效 例子: #EXT-X-PROGRAM-DATE-TIME:2010-02-19T14:54:23.031+08:00</p> <h4><a id="m3u8-index" class="anchor" aria-hidden="true" href="#m3u8-index"></a>一个m3u8 index文件示例</h4> <pre><code>#EXTM3U #EXT-X-VERSION:3 #EXT-X-TARGETDURATION:15 #EXT-X-MEDIA-SEQUENCE:0 #EXT-X-PROGRAM-DATE-TIME:2019-12-27T13:52:58.107325501+08:00 #EXTINF:0.000, /v1/liveshowstatic/3262140290_1577425980848091643.ts?authorization=bce-auth-v1/3a9861ff96dd472f875791b14cb0b6a1/2019-12-27T05:53:00Z/604800/host/b0572bf3ff1643d95f5f4b64c788461912c2c8e880b003fd7f87fa68a8093f13 #EXT-X-PROGRAM-DATE-TIME:2019-12-27T13:53:04.423748954+08:00 #EXTINF:0.000, /v1/liveshowstatic/3262140290_1577425985846621096.ts?authorization=bce-auth-v1/3a9861ff96dd472f875791b14cb0b6a1/2019-12-27T05:53:05Z/604800/host/7ede18cc10a376b33ddd3fcb3faad0c75b117d3bebc3118dd955839791bff2fb #EXT-X-PROGRAM-DATE-TIME:2019-12-27T13:53:19.473701505+08:00 #EXTINF:0.000, /v1/liveshowstatic/3262140290_1577426000864220299.ts?authorization=bce-auth-v1/3a9861ff96dd472f875791b14cb0b6a1/2019-12-27T05:53:20Z/604800/host/21ce1874cc833778edad812a88267769ca48ad879feee9b29acfe416f1928880 </code></pre> <h3><a id="ts" class="anchor" aria-hidden="true" href="#ts"></a>TS 媒体文件</h3> <p>ts媒体文件是根据业务不同自定义的格式,比如直播消息流的ts文件格式是json string,而视频编码主要格式 h264/mpeg4, 音频为 acc/MP3。由于TS 媒体文件的定制化比较高,这里就不一一介绍了。</p> <h4><a id="npm" class="anchor" aria-hidden="true" href="#npm"></a>npm</h4> <p>npm使用方法方法,见<a href="https://docs.npmjs.com/about-npm/index.html" target="_self" title="npm使用说明">npm使用说明</a></p> <pre><code>npm install @smt-lib/m3u-parser </code></pre> <h3><a id="" class="anchor" aria-hidden="true" href="#"></a>示例</h3> <pre><code class="language-javascript"> // m3u-index文件内容 import {parseM3U8Text} from '@smt-lib/m3u-parser'; const m3uIndexText = '#EXTM3U\r\n...'; // 解析后的json格式m3u-index文件 cosnt m3uJson = parseM3U8Text(m3uIndexText); // console.log(m3uJson); </code></pre> <h2><a id="" class="anchor" aria-hidden="true" href="#"></a>方法</h2> <p><b>parseM3U8Text</b></p> <p>注册异常监听函数</p> <table> <thead> <tr> <th>字段名</th> <th>类型</th> <th>必有</th> <th>说明</th> </tr> </thead> <tbody> <tr> <td>fileText</td> <td>string</td> <td>Y</td> <td>m3u-index文件内容</td> </tr> </tbody> </table> <p>返回参数说明</p> <table> <thead> <tr> <th>字段名</th> <th>类型</th> <th>必有</th> <th>说明</th> </tr> </thead> <tbody> <tr> <td>version</td> <td>string</td> <td>N</td> <td>含义详见<code>M3U描述文件</code> #EXT-X-VERSION</td> </tr> <tr> <td>duration</td> <td>string</td> <td>N</td> <td>#EXT-X-TARGETDURATION</td> </tr> <tr> <td>sequence</td> <td>string</td> <td>N</td> <td>#EXT-X-MEDIA-SEQUENCE</td> </tr> <tr> <td>fileQueue</td> <td>array</td> <td>N</td> <td>文件队列</td> </tr> </tbody> </table> <p>fileQueue-item 项说明 |字段名 | 类型 | 必有 | 说明 | |---|---|---|---| |item.programTime | string| N |#EXT-X-PROGRAM-DATE-TIME 进度条时间| |item.info|object| N| 信息,包含title标题和duration持续时间| |item.path|string|Y | TS文件地址或嵌套m3u地址, <code>注意: path可能不包含host和protocol</code> | |item.type|number| y| 枚举 0:带进度条型ts文件项,1:不带进度条型ts文件项,2:m3u-index嵌套项 |</p> <pre><code>// type=0 #EXTM3U #EXT-X-VERSION:3 #EXT-X-TARGETDURATION:15 #EXT-X-MEDIA-SEQUENCE:0 #EXT-X-PROGRAM-DATE-TIME:2019-12-27T13:52:58.107325501+08:00 #EXTINF:0.000, /v1/liveshowstatic/3262140290_1577425980848091643.ts?authorization=bce-auth-v1/3a9861ff96dd472f875791b14cb0b6a1/2019-12-27T05:53:00Z/604800/host/b0572bf3ff1643d95f5f4b64c788461912c2c8e880b003fd7f87fa68a8093f13 #EXT-X-PROGRAM-DATE-TIME:2019-12-27T13:53:04.423748954+08:00 #EXTINF:0.000, /v1/liveshowstatic/3262140290_1577425985846621096.ts?authorization=bce-auth-v1/3a9861ff96dd472f875791b14cb0b6a1/2019-12-27T05:53:05Z/604800/host/7ede18cc10a376b33ddd3fcb3faad0c75b117d3bebc3118dd955839791bff2fb #EXT-X-PROGRAM-DATE-TIME:2019-12-27T13:53:19.473701505+08:00 #EXTINF:0.000, /v1/liveshowstatic/3262140290_1577426000864220299.ts?authorization=bce-auth-v1/3a9861ff96dd472f875791b14cb0b6a1/2019-12-27T05:53:20Z/604800/host/21ce1874cc833778edad812a88267769ca48ad879feee9b29acfe416f1928880 // type=1 #EXTM3U #EXT-X-VERSION:3 #EXT-X-TARGETDURATION:15 #EXT-X-MEDIA-SEQUENCE:0 #EXTINF:0.000, /v1/liveshowstatic/3262140290_1577425980848091643.ts?authorization=bce-auth-v1/3a9861ff96dd472f875791b14cb0b6a1/2019-12-27T05:53:00Z/604800/host/b0572bf3ff1643d95f5f4b64c788461912c2c8e880b003fd7f87fa68a8093f13 #EXTINF:0.000, /v1/liveshowstatic/3262140290_1577425985846621096.ts?authorization=bce-auth-v1/3a9861ff96dd472f875791b14cb0b6a1/2019-12-27T05:53:05Z/604800/host/7ede18cc10a376b33ddd3fcb3faad0c75b117d3bebc3118dd955839791bff2fb #EXTINF:0.000, /v1/liveshowstatic/3262140290_1577426000864220299.ts?authorization=bce-auth-v1/3a9861ff96dd472f875791b14cb0b6a1/2019-12-27T05:53:20Z/604800/host/21ce1874cc833778edad812a88267769ca48ad879feee9b29acfe416f1928880 // type=2 #EXTM3U #EXT-X-STREAM-INF:PROGRAM-ID=1,BANDWIDTH=1280000 http://example.com/low.m3u8 #EXT-X-STREAM-INF:PROGRAM-ID=1,BANDWIDTH=2560000 http://example.com/mid.m3u8 #EXT-X-STREAM-INF:PROGRAM-ID=1,BANDWIDTH=7680000 http://example.com/hi.m3u8 #EXT-X-STREAM-INF:PROGRAM-ID=1,BANDWIDTH=65000,CODECS="mp4a.40.5" http://example.com/audio-only.m3u8 </code></pre> </div></article> <aside><h2 class="svelte-1u3txz5">Details</h2> <dl class="details f-d1 svelte-1u3txz5"><div class="svelte-1u3txz5"><dt class="svelte-1u3txz5">Downloads (weekly)</dt> <dd class="f-mono f-h1"><div class="container svelte-a3j687"><div class="dot svelte-a3j687"></div> <div class="dot svelte-a3j687"></div> <div class="dot svelte-a3j687"></div> </div></dd> </div><div class="svelte-1u3txz5"><dt class="svelte-1u3txz5">Updated</dt> <dd><time class="" datetime="2021-03-11T05:40:27.423Z">March 11, 2021</time></dd> </div><div class="svelte-1u3txz5"><dt class="svelte-1u3txz5">Created</dt> <dd><time class="" datetime="2020-03-19T07:17:50.603Z">March 19, 2020</time></dd> </div><div class="svelte-1u3txz5"><div class="wrapper svelte-qjxoui"><dt class="header svelte-qjxoui"><span class="title svelte-qjxoui">Package score <a class="ml1 f-w400" href="https://docs.skypack.dev/package-authors/package-checks" target="_blank">learn more</a></span> <span class="f-u1"> <div class="container svelte-a3j687"><div class="dot svelte-a3j687"></div> <div class="dot svelte-a3j687"></div> <div class="dot svelte-a3j687"></div> </div></span></dt> <dd class="pt1"><ul class="checks"><li class="checks-check svelte-qjxoui"><span class="status__loading svelte-qjxoui" aria-label="loading"></span>Package Security </li><li class="checks-check svelte-qjxoui"><span class="status__warn svelte-qjxoui" aria-label="not added yet"></span> ES Module Entrypoint <a class="action svelte-qjxoui" href="https://docs.skypack.dev/package-authors/package-checks#esm" target="_blank" rel="noopener noreferrer">Info</a> </li><li class="checks-check svelte-qjxoui"><span class="status__warn svelte-qjxoui" aria-label="not added yet"></span> Export Map <a class="action svelte-qjxoui" href="https://docs.skypack.dev/package-authors/package-checks#export-map" target="_blank" rel="noopener noreferrer">Info</a> </li><li class="checks-check svelte-qjxoui"><svg class="status__check svelte-qjxoui" fill="none" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 13 9"><title>added Keywords

  • added License
  • added README
  • Repository URL Info
  • TypeScript Types Info
  • License
    ISC
    Dependencies
    0
    Keywords
    Collaborators