@soga/m3u8 0.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.d.mts +41 -0
- package/dist/index.d.ts +41 -0
- package/dist/index.js +88 -0
- package/dist/index.mjs +63 -0
- package/package.json +31 -0
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
declare function parseM3U8(input: string): Promise<{
|
|
2
|
+
duration: number;
|
|
3
|
+
data: M3U8Manifest;
|
|
4
|
+
input: string;
|
|
5
|
+
}>;
|
|
6
|
+
type CommonManifest = {
|
|
7
|
+
version: number;
|
|
8
|
+
targetDuration: number;
|
|
9
|
+
mediaSequence: number;
|
|
10
|
+
endList: boolean;
|
|
11
|
+
};
|
|
12
|
+
type M3U8RawManifest = CommonManifest & {
|
|
13
|
+
segments: {
|
|
14
|
+
duration: number;
|
|
15
|
+
uri: string;
|
|
16
|
+
map: {
|
|
17
|
+
uri: string;
|
|
18
|
+
};
|
|
19
|
+
}[];
|
|
20
|
+
};
|
|
21
|
+
type M3U8SegmentInfo = {
|
|
22
|
+
index: number;
|
|
23
|
+
duration: number;
|
|
24
|
+
init_uri: string;
|
|
25
|
+
uri: string;
|
|
26
|
+
file_path: string;
|
|
27
|
+
folder: string;
|
|
28
|
+
size: number;
|
|
29
|
+
start_time: number;
|
|
30
|
+
end_time: number;
|
|
31
|
+
};
|
|
32
|
+
type M3U8Manifest = CommonManifest & {
|
|
33
|
+
init: {
|
|
34
|
+
uri: string;
|
|
35
|
+
file_path: string;
|
|
36
|
+
size: number;
|
|
37
|
+
};
|
|
38
|
+
segments: M3U8SegmentInfo[];
|
|
39
|
+
};
|
|
40
|
+
|
|
41
|
+
export { type M3U8Manifest, type M3U8RawManifest, type M3U8SegmentInfo, parseM3U8 };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
declare function parseM3U8(input: string): Promise<{
|
|
2
|
+
duration: number;
|
|
3
|
+
data: M3U8Manifest;
|
|
4
|
+
input: string;
|
|
5
|
+
}>;
|
|
6
|
+
type CommonManifest = {
|
|
7
|
+
version: number;
|
|
8
|
+
targetDuration: number;
|
|
9
|
+
mediaSequence: number;
|
|
10
|
+
endList: boolean;
|
|
11
|
+
};
|
|
12
|
+
type M3U8RawManifest = CommonManifest & {
|
|
13
|
+
segments: {
|
|
14
|
+
duration: number;
|
|
15
|
+
uri: string;
|
|
16
|
+
map: {
|
|
17
|
+
uri: string;
|
|
18
|
+
};
|
|
19
|
+
}[];
|
|
20
|
+
};
|
|
21
|
+
type M3U8SegmentInfo = {
|
|
22
|
+
index: number;
|
|
23
|
+
duration: number;
|
|
24
|
+
init_uri: string;
|
|
25
|
+
uri: string;
|
|
26
|
+
file_path: string;
|
|
27
|
+
folder: string;
|
|
28
|
+
size: number;
|
|
29
|
+
start_time: number;
|
|
30
|
+
end_time: number;
|
|
31
|
+
};
|
|
32
|
+
type M3U8Manifest = CommonManifest & {
|
|
33
|
+
init: {
|
|
34
|
+
uri: string;
|
|
35
|
+
file_path: string;
|
|
36
|
+
size: number;
|
|
37
|
+
};
|
|
38
|
+
segments: M3U8SegmentInfo[];
|
|
39
|
+
};
|
|
40
|
+
|
|
41
|
+
export { type M3U8Manifest, type M3U8RawManifest, type M3U8SegmentInfo, parseM3U8 };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// src/index.ts
|
|
21
|
+
var index_exports = {};
|
|
22
|
+
__export(index_exports, {
|
|
23
|
+
parseM3U8: () => parseM3U8
|
|
24
|
+
});
|
|
25
|
+
module.exports = __toCommonJS(index_exports);
|
|
26
|
+
var import_fs_extra = require("fs-extra");
|
|
27
|
+
var import_m3u8_parser = require("m3u8-parser");
|
|
28
|
+
var import_path = require("path");
|
|
29
|
+
async function parseM3U8(input) {
|
|
30
|
+
const time = Date.now();
|
|
31
|
+
const parser = new import_m3u8_parser.Parser();
|
|
32
|
+
const manifest = await (0, import_fs_extra.readFile)(input, "utf8");
|
|
33
|
+
parser.push(manifest);
|
|
34
|
+
parser.end();
|
|
35
|
+
const result = parser.manifest;
|
|
36
|
+
const m3u8_folder = (0, import_path.dirname)(input);
|
|
37
|
+
const first = result.segments[0];
|
|
38
|
+
if (!first?.map?.uri) {
|
|
39
|
+
throw new Error(`parse ${input} failed`);
|
|
40
|
+
}
|
|
41
|
+
const init_uri = first.map.uri;
|
|
42
|
+
const init_path = (0, import_path.resolve)(m3u8_folder, init_uri);
|
|
43
|
+
const init_stat = await (0, import_fs_extra.stat)(init_path);
|
|
44
|
+
const init = {
|
|
45
|
+
uri: init_uri,
|
|
46
|
+
folder: m3u8_folder,
|
|
47
|
+
file_path: init_path,
|
|
48
|
+
size: init_stat.size
|
|
49
|
+
};
|
|
50
|
+
let last_times = 0;
|
|
51
|
+
const times = 1e6;
|
|
52
|
+
const segments = [];
|
|
53
|
+
const { length } = result.segments;
|
|
54
|
+
for (let i = 0; i < length; i++) {
|
|
55
|
+
const segment = result.segments[i];
|
|
56
|
+
const file_path = (0, import_path.resolve)(m3u8_folder, segment.uri);
|
|
57
|
+
const info = await (0, import_fs_extra.stat)(file_path);
|
|
58
|
+
const scaled_duration = segment.duration * times;
|
|
59
|
+
const start_time = last_times / times;
|
|
60
|
+
const end_time = (last_times + scaled_duration) / times;
|
|
61
|
+
segments.push({
|
|
62
|
+
index: i,
|
|
63
|
+
duration: segment.duration,
|
|
64
|
+
init_uri,
|
|
65
|
+
uri: segment.uri,
|
|
66
|
+
file_path,
|
|
67
|
+
size: info.size,
|
|
68
|
+
start_time,
|
|
69
|
+
end_time,
|
|
70
|
+
folder: m3u8_folder
|
|
71
|
+
});
|
|
72
|
+
last_times += scaled_duration;
|
|
73
|
+
}
|
|
74
|
+
const json = {
|
|
75
|
+
init,
|
|
76
|
+
...result,
|
|
77
|
+
segments
|
|
78
|
+
};
|
|
79
|
+
return {
|
|
80
|
+
duration: Date.now() - time,
|
|
81
|
+
data: json,
|
|
82
|
+
input
|
|
83
|
+
};
|
|
84
|
+
}
|
|
85
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
86
|
+
0 && (module.exports = {
|
|
87
|
+
parseM3U8
|
|
88
|
+
});
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
// src/index.ts
|
|
2
|
+
import { readFile, stat } from "fs-extra";
|
|
3
|
+
import { Parser } from "m3u8-parser";
|
|
4
|
+
import { dirname, resolve } from "path";
|
|
5
|
+
async function parseM3U8(input) {
|
|
6
|
+
const time = Date.now();
|
|
7
|
+
const parser = new Parser();
|
|
8
|
+
const manifest = await readFile(input, "utf8");
|
|
9
|
+
parser.push(manifest);
|
|
10
|
+
parser.end();
|
|
11
|
+
const result = parser.manifest;
|
|
12
|
+
const m3u8_folder = dirname(input);
|
|
13
|
+
const first = result.segments[0];
|
|
14
|
+
if (!first?.map?.uri) {
|
|
15
|
+
throw new Error(`parse ${input} failed`);
|
|
16
|
+
}
|
|
17
|
+
const init_uri = first.map.uri;
|
|
18
|
+
const init_path = resolve(m3u8_folder, init_uri);
|
|
19
|
+
const init_stat = await stat(init_path);
|
|
20
|
+
const init = {
|
|
21
|
+
uri: init_uri,
|
|
22
|
+
folder: m3u8_folder,
|
|
23
|
+
file_path: init_path,
|
|
24
|
+
size: init_stat.size
|
|
25
|
+
};
|
|
26
|
+
let last_times = 0;
|
|
27
|
+
const times = 1e6;
|
|
28
|
+
const segments = [];
|
|
29
|
+
const { length } = result.segments;
|
|
30
|
+
for (let i = 0; i < length; i++) {
|
|
31
|
+
const segment = result.segments[i];
|
|
32
|
+
const file_path = resolve(m3u8_folder, segment.uri);
|
|
33
|
+
const info = await stat(file_path);
|
|
34
|
+
const scaled_duration = segment.duration * times;
|
|
35
|
+
const start_time = last_times / times;
|
|
36
|
+
const end_time = (last_times + scaled_duration) / times;
|
|
37
|
+
segments.push({
|
|
38
|
+
index: i,
|
|
39
|
+
duration: segment.duration,
|
|
40
|
+
init_uri,
|
|
41
|
+
uri: segment.uri,
|
|
42
|
+
file_path,
|
|
43
|
+
size: info.size,
|
|
44
|
+
start_time,
|
|
45
|
+
end_time,
|
|
46
|
+
folder: m3u8_folder
|
|
47
|
+
});
|
|
48
|
+
last_times += scaled_duration;
|
|
49
|
+
}
|
|
50
|
+
const json = {
|
|
51
|
+
init,
|
|
52
|
+
...result,
|
|
53
|
+
segments
|
|
54
|
+
};
|
|
55
|
+
return {
|
|
56
|
+
duration: Date.now() - time,
|
|
57
|
+
data: json,
|
|
58
|
+
input
|
|
59
|
+
};
|
|
60
|
+
}
|
|
61
|
+
export {
|
|
62
|
+
parseM3U8
|
|
63
|
+
};
|
package/package.json
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@soga/m3u8",
|
|
3
|
+
"version": "0.5.0",
|
|
4
|
+
"publishConfig": {
|
|
5
|
+
"access": "public"
|
|
6
|
+
},
|
|
7
|
+
"main": "./dist/index.js",
|
|
8
|
+
"module": "./dist/index.mjs",
|
|
9
|
+
"types": "./dist/index.d.ts",
|
|
10
|
+
"scripts": {
|
|
11
|
+
"build": "tsup src/index.ts --format cjs,esm --dts",
|
|
12
|
+
"dev": "tsup src/index.ts --format cjs,esm --dts --watch"
|
|
13
|
+
},
|
|
14
|
+
"files": [
|
|
15
|
+
"dist"
|
|
16
|
+
],
|
|
17
|
+
"keywords": [],
|
|
18
|
+
"author": "",
|
|
19
|
+
"license": "ISC",
|
|
20
|
+
"description": "",
|
|
21
|
+
"dependencies": {
|
|
22
|
+
"fs-extra": "^11.3.0",
|
|
23
|
+
"m3u8-parser": "^7.2.0",
|
|
24
|
+
"tsup": "^8.5.0"
|
|
25
|
+
},
|
|
26
|
+
"devDependencies": {
|
|
27
|
+
"@types/fs-extra": "^11.0.4",
|
|
28
|
+
"@types/m3u8-parser": "^7.2.2"
|
|
29
|
+
},
|
|
30
|
+
"prepublishOnly": "npm run build"
|
|
31
|
+
}
|