npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@smt-lib/m3u-parser

v1.0.0-rc

Published

m3u index parser

Downloads

3

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: [, ]

duration 指定每个媒体段(ts)的持续时间(秒), 仅对其后面的URI有效, title 是下载资源的 url

#EXT-X-PROGRAM-DATE-TIME

将一个绝对时间或是日期和一个媒体段中的第一个 Sample 相关联, 只对下一个 meida URI 有效 例子: #EXT-X-PROGRAM-DATE-TIME:2010-02-19T14:54:23.031+08:00

一个m3u8 index文件示例

#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

TS 媒体文件

ts媒体文件是根据业务不同自定义的格式,比如直播消息流的ts文件格式是json string,而视频编码主要格式 h264/mpeg4, 音频为 acc/MP3。由于TS 媒体文件的定制化比较高,这里就不一一介绍了。

npm

npm使用方法方法,见npm使用说明

npm install @smt-lib/m3u-parser

示例

	// m3u-index文件内容
	import {parseM3U8Text} from '@smt-lib/m3u-parser';
	const m3uIndexText = '#EXTM3U\r\n...';
	// 解析后的json格式m3u-index文件
	cosnt m3uJson = parseM3U8Text(m3uIndexText);
	// console.log(m3uJson);

方法

parseM3U8Text

注册异常监听函数

|字段名 | 类型 | 必有 | 说明 | |---|---|---|---| |fileText| string |Y| m3u-index文件内容 |

返回参数说明

|字段名 | 类型 | 必有 | 说明 | |---|---|---|---| |version| string|N| 含义详见M3U描述文件 #EXT-X-VERSION | |duration| string| N |#EXT-X-TARGETDURATION| |sequence| string| N |#EXT-X-MEDIA-SEQUENCE| |fileQueue | array| N |文件队列|

fileQueue-item 项说明 |字段名 | 类型 | 必有 | 说明 | |---|---|---|---| |item.programTime | string| N |#EXT-X-PROGRAM-DATE-TIME 进度条时间| |item.info|object| N| 信息,包含title标题和duration持续时间| |item.path|string|Y | TS文件地址或嵌套m3u地址, 注意: path可能不包含host和protocol | |item.type|number| y| 枚举 0:带进度条型ts文件项,1:不带进度条型ts文件项,2:m3u-index嵌套项 |

// 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