@soga/mediainfo 0.3.0 → 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.
@@ -1,4 +1,11 @@
1
- export type GeneralInfo = {
1
+ declare function parseMediaRaw(media_path: string): Promise<any>;
2
+ declare function parseMediaInfo(media_path: string): Promise<{
3
+ audio: AudioTrackInfo[];
4
+ video: VideoTrackInfo[];
5
+ text: TextTrackInfo[];
6
+ general: GeneralInfo;
7
+ }>;
8
+ type GeneralInfo = {
2
9
  audioCount: number;
3
10
  videoCount: number;
4
11
  textCount: number;
@@ -10,7 +17,7 @@ export type GeneralInfo = {
10
17
  duration: string;
11
18
  };
12
19
  };
13
- export type AudioTrackInfo = {
20
+ type AudioTrackInfo = {
14
21
  is_default: boolean;
15
22
  order: number;
16
23
  codec: string;
@@ -21,7 +28,7 @@ export type AudioTrackInfo = {
21
28
  duration: number;
22
29
  lossless: boolean;
23
30
  };
24
- export type VideoTrackInfo = {
31
+ type VideoTrackInfo = {
25
32
  is_default: boolean;
26
33
  order: number;
27
34
  width: number;
@@ -35,7 +42,7 @@ export type VideoTrackInfo = {
35
42
  format_profile: string;
36
43
  format_tier: string;
37
44
  };
38
- export type TextTrackInfo = {
45
+ type TextTrackInfo = {
39
46
  is_default: boolean;
40
47
  codec: string;
41
48
  order: number;
@@ -44,9 +51,11 @@ export type TextTrackInfo = {
44
51
  title?: string;
45
52
  is_vtt?: boolean;
46
53
  };
47
- export type Mediainfo = {
54
+ type Mediainfo = {
48
55
  general?: GeneralInfo;
49
56
  audio?: AudioTrackInfo[];
50
57
  video?: VideoTrackInfo[];
51
58
  text?: TextTrackInfo[];
52
59
  };
60
+
61
+ export { type AudioTrackInfo, type GeneralInfo, type Mediainfo, type TextTrackInfo, type VideoTrackInfo, parseMediaInfo, parseMediaRaw };
@@ -0,0 +1,61 @@
1
+ declare function parseMediaRaw(media_path: string): Promise<any>;
2
+ declare function parseMediaInfo(media_path: string): Promise<{
3
+ audio: AudioTrackInfo[];
4
+ video: VideoTrackInfo[];
5
+ text: TextTrackInfo[];
6
+ general: GeneralInfo;
7
+ }>;
8
+ type GeneralInfo = {
9
+ audioCount: number;
10
+ videoCount: number;
11
+ textCount: number;
12
+ fileSize: number;
13
+ codec: string;
14
+ title: string;
15
+ duration: number;
16
+ extra?: {
17
+ duration: string;
18
+ };
19
+ };
20
+ type AudioTrackInfo = {
21
+ is_default: boolean;
22
+ order: number;
23
+ codec: string;
24
+ bitrate: number;
25
+ framerate: number;
26
+ sample_rate: number;
27
+ channels: number;
28
+ duration: number;
29
+ lossless: boolean;
30
+ };
31
+ type VideoTrackInfo = {
32
+ is_default: boolean;
33
+ order: number;
34
+ width: number;
35
+ height: number;
36
+ codec: string;
37
+ profile: string;
38
+ framerate: number;
39
+ bitrate: number;
40
+ duration: number;
41
+ bitdepth: number;
42
+ format_profile: string;
43
+ format_tier: string;
44
+ };
45
+ type TextTrackInfo = {
46
+ is_default: boolean;
47
+ codec: string;
48
+ order: number;
49
+ duration: number;
50
+ language?: string;
51
+ title?: string;
52
+ is_vtt?: boolean;
53
+ };
54
+ type Mediainfo = {
55
+ general?: GeneralInfo;
56
+ audio?: AudioTrackInfo[];
57
+ video?: VideoTrackInfo[];
58
+ text?: TextTrackInfo[];
59
+ };
60
+
61
+ export { type AudioTrackInfo, type GeneralInfo, type Mediainfo, type TextTrackInfo, type VideoTrackInfo, parseMediaInfo, parseMediaRaw };
package/dist/index.js ADDED
@@ -0,0 +1,200 @@
1
+ "use strict";
2
+ var __create = Object.create;
3
+ var __defProp = Object.defineProperty;
4
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
+ var __getOwnPropNames = Object.getOwnPropertyNames;
6
+ var __getProtoOf = Object.getPrototypeOf;
7
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
8
+ var __export = (target, all) => {
9
+ for (var name in all)
10
+ __defProp(target, name, { get: all[name], enumerable: true });
11
+ };
12
+ var __copyProps = (to, from, except, desc) => {
13
+ if (from && typeof from === "object" || typeof from === "function") {
14
+ for (let key of __getOwnPropNames(from))
15
+ if (!__hasOwnProp.call(to, key) && key !== except)
16
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
17
+ }
18
+ return to;
19
+ };
20
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
21
+ // If the importer is in node compatibility mode or this is not an ESM
22
+ // file that has been converted to a CommonJS file using a Babel-
23
+ // compatible transform (i.e. "__esModule" has not been set), then set
24
+ // "default" to the CommonJS "module.exports" for node compatibility.
25
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
26
+ mod
27
+ ));
28
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
29
+
30
+ // src/index.ts
31
+ var index_exports = {};
32
+ __export(index_exports, {
33
+ parseMediaInfo: () => parseMediaInfo,
34
+ parseMediaRaw: () => parseMediaRaw
35
+ });
36
+ module.exports = __toCommonJS(index_exports);
37
+ var import_fs_extra = require("fs-extra");
38
+ var import_promises = require("fs/promises");
39
+ var import_mediainfo = __toESM(require("mediainfo.js"));
40
+ async function parseMediaRaw(media_path) {
41
+ const mediainfo = await (0, import_mediainfo.default)({
42
+ format: "JSON"
43
+ });
44
+ const fileList = typeof media_path === "string" ? [media_path] : media_path;
45
+ let total = 0;
46
+ const arr = [];
47
+ for await (const [index, file_path] of fileList.entries()) {
48
+ const { size } = await (0, import_promises.stat)(file_path);
49
+ arr.push({
50
+ index,
51
+ start: total,
52
+ end: total + size - 1,
53
+ size,
54
+ file_path
55
+ });
56
+ total += size;
57
+ }
58
+ const getBuffer = async (size, offset) => {
59
+ if (!size) {
60
+ return new Uint8Array(0);
61
+ }
62
+ const start = offset;
63
+ const end = offset + size - 1;
64
+ const first = arr.find((item) => item.start <= start && item.end >= start);
65
+ const last = arr.find((item) => item.start <= end && item.end >= end);
66
+ if (!first || !last) {
67
+ return new Uint8Array(0);
68
+ }
69
+ const list = [];
70
+ if (first.index === last.index) {
71
+ list.push({
72
+ index: first.index,
73
+ file_path: first.file_path,
74
+ from: start - first.start,
75
+ to: end - first.start
76
+ });
77
+ } else {
78
+ list.push({
79
+ index: first.index,
80
+ file_path: first.file_path,
81
+ from: start - first.start,
82
+ to: first.end
83
+ });
84
+ for (let i = first.index + 1; i < last.index; i++) {
85
+ const item = arr[i];
86
+ if (item) {
87
+ list.push({
88
+ index: item.index,
89
+ file_path: item.file_path,
90
+ from: 0,
91
+ to: item.size - 1
92
+ });
93
+ }
94
+ }
95
+ list.push({
96
+ index: last.index,
97
+ file_path: last.file_path,
98
+ from: 0,
99
+ to: end - last.start
100
+ });
101
+ }
102
+ const buffers = [];
103
+ for await (const item of list) {
104
+ const { file_path, from, to } = item;
105
+ const stream = (0, import_fs_extra.createReadStream)(file_path, { start: from, end: to });
106
+ for await (const data2 of stream) {
107
+ buffers.push(data2);
108
+ }
109
+ stream.close();
110
+ }
111
+ const buffer = Buffer.concat(buffers);
112
+ return buffer;
113
+ };
114
+ const data = await mediainfo.analyzeData(() => total, getBuffer);
115
+ try {
116
+ const rawJson = JSON.parse(data);
117
+ return rawJson;
118
+ } catch (err) {
119
+ const text = data.replace(',"":{}]}]}', ',"":{}}]}');
120
+ const rawJson = JSON.parse(text);
121
+ return rawJson;
122
+ }
123
+ }
124
+ async function parseMediaInfo(media_path) {
125
+ const raw = await parseMediaRaw(media_path);
126
+ const rawMediainfo = raw.media;
127
+ const list = rawMediainfo.track;
128
+ const info = {
129
+ audio: [],
130
+ video: [],
131
+ text: [],
132
+ general: {}
133
+ };
134
+ let audioOrder = 0;
135
+ let videoOrder = 0;
136
+ let textOrder = 0;
137
+ list.forEach((item) => {
138
+ const type = item["@type"];
139
+ if (type === "Audio") {
140
+ info.audio.push({
141
+ is_default: item.Default === "Yes",
142
+ codec: item.Format.toLowerCase(),
143
+ // order: Number(item.StreamOrder),
144
+ order: audioOrder++,
145
+ // size: Number(item.StreamSize),
146
+ bitrate: Number(item.BitRate),
147
+ framerate: Number(item.FrameRate),
148
+ sample_rate: Number(item.SamplingRate),
149
+ duration: Number(item.Duration),
150
+ channels: Number(item.Channels),
151
+ lossless: item.Compression_Mode === "Lossless"
152
+ });
153
+ } else if (type === "Text") {
154
+ info.text.push({
155
+ is_default: item.Default === "Yes",
156
+ codec: item.Format.toLowerCase(),
157
+ // order: Number(item.StreamOrder),
158
+ order: textOrder++,
159
+ duration: Number(item.Duration),
160
+ title: item.Title || item.Language,
161
+ language: item.Language
162
+ });
163
+ } else if (type === "Video") {
164
+ info.video.push({
165
+ is_default: item.Default === "Yes",
166
+ codec: item.Format.toLowerCase(),
167
+ order: videoOrder++,
168
+ width: Number(item.Width),
169
+ height: Number(item.Height),
170
+ profile: item.Format_Profile,
171
+ bitrate: Number(item.BitRate),
172
+ framerate: Number(item.FrameRate) ? Math.round(Number(item.FrameRate)) : 0,
173
+ // sample_rate: Number(item.SamplingRate),
174
+ duration: Number(item.Duration),
175
+ bitdepth: Number(item.BitDepth),
176
+ format_profile: item.Format_Profile,
177
+ format_tier: item.Format_Tier
178
+ });
179
+ } else if (type === "General") {
180
+ info.general = {
181
+ audioCount: Number(item.AudioCount),
182
+ textCount: Number(item.TextCount) || 0,
183
+ videoCount: Number(item.VideoCount),
184
+ fileSize: Number(item.FileSize),
185
+ title: item.Title || "",
186
+ duration: Number(item.Duration),
187
+ codec: item.Format
188
+ };
189
+ if (!info.general.duration && item.extra?.duration) {
190
+ info.general.duration = Number(item.extra.duration);
191
+ }
192
+ }
193
+ });
194
+ return info;
195
+ }
196
+ // Annotate the CommonJS export names for ESM import in node:
197
+ 0 && (module.exports = {
198
+ parseMediaInfo,
199
+ parseMediaRaw
200
+ });
package/dist/index.mjs ADDED
@@ -0,0 +1,164 @@
1
+ // src/index.ts
2
+ import { createReadStream } from "fs-extra";
3
+ import { stat } from "fs/promises";
4
+ import getMediainfo from "mediainfo.js";
5
+ async function parseMediaRaw(media_path) {
6
+ const mediainfo = await getMediainfo({
7
+ format: "JSON"
8
+ });
9
+ const fileList = typeof media_path === "string" ? [media_path] : media_path;
10
+ let total = 0;
11
+ const arr = [];
12
+ for await (const [index, file_path] of fileList.entries()) {
13
+ const { size } = await stat(file_path);
14
+ arr.push({
15
+ index,
16
+ start: total,
17
+ end: total + size - 1,
18
+ size,
19
+ file_path
20
+ });
21
+ total += size;
22
+ }
23
+ const getBuffer = async (size, offset) => {
24
+ if (!size) {
25
+ return new Uint8Array(0);
26
+ }
27
+ const start = offset;
28
+ const end = offset + size - 1;
29
+ const first = arr.find((item) => item.start <= start && item.end >= start);
30
+ const last = arr.find((item) => item.start <= end && item.end >= end);
31
+ if (!first || !last) {
32
+ return new Uint8Array(0);
33
+ }
34
+ const list = [];
35
+ if (first.index === last.index) {
36
+ list.push({
37
+ index: first.index,
38
+ file_path: first.file_path,
39
+ from: start - first.start,
40
+ to: end - first.start
41
+ });
42
+ } else {
43
+ list.push({
44
+ index: first.index,
45
+ file_path: first.file_path,
46
+ from: start - first.start,
47
+ to: first.end
48
+ });
49
+ for (let i = first.index + 1; i < last.index; i++) {
50
+ const item = arr[i];
51
+ if (item) {
52
+ list.push({
53
+ index: item.index,
54
+ file_path: item.file_path,
55
+ from: 0,
56
+ to: item.size - 1
57
+ });
58
+ }
59
+ }
60
+ list.push({
61
+ index: last.index,
62
+ file_path: last.file_path,
63
+ from: 0,
64
+ to: end - last.start
65
+ });
66
+ }
67
+ const buffers = [];
68
+ for await (const item of list) {
69
+ const { file_path, from, to } = item;
70
+ const stream = createReadStream(file_path, { start: from, end: to });
71
+ for await (const data2 of stream) {
72
+ buffers.push(data2);
73
+ }
74
+ stream.close();
75
+ }
76
+ const buffer = Buffer.concat(buffers);
77
+ return buffer;
78
+ };
79
+ const data = await mediainfo.analyzeData(() => total, getBuffer);
80
+ try {
81
+ const rawJson = JSON.parse(data);
82
+ return rawJson;
83
+ } catch (err) {
84
+ const text = data.replace(',"":{}]}]}', ',"":{}}]}');
85
+ const rawJson = JSON.parse(text);
86
+ return rawJson;
87
+ }
88
+ }
89
+ async function parseMediaInfo(media_path) {
90
+ const raw = await parseMediaRaw(media_path);
91
+ const rawMediainfo = raw.media;
92
+ const list = rawMediainfo.track;
93
+ const info = {
94
+ audio: [],
95
+ video: [],
96
+ text: [],
97
+ general: {}
98
+ };
99
+ let audioOrder = 0;
100
+ let videoOrder = 0;
101
+ let textOrder = 0;
102
+ list.forEach((item) => {
103
+ const type = item["@type"];
104
+ if (type === "Audio") {
105
+ info.audio.push({
106
+ is_default: item.Default === "Yes",
107
+ codec: item.Format.toLowerCase(),
108
+ // order: Number(item.StreamOrder),
109
+ order: audioOrder++,
110
+ // size: Number(item.StreamSize),
111
+ bitrate: Number(item.BitRate),
112
+ framerate: Number(item.FrameRate),
113
+ sample_rate: Number(item.SamplingRate),
114
+ duration: Number(item.Duration),
115
+ channels: Number(item.Channels),
116
+ lossless: item.Compression_Mode === "Lossless"
117
+ });
118
+ } else if (type === "Text") {
119
+ info.text.push({
120
+ is_default: item.Default === "Yes",
121
+ codec: item.Format.toLowerCase(),
122
+ // order: Number(item.StreamOrder),
123
+ order: textOrder++,
124
+ duration: Number(item.Duration),
125
+ title: item.Title || item.Language,
126
+ language: item.Language
127
+ });
128
+ } else if (type === "Video") {
129
+ info.video.push({
130
+ is_default: item.Default === "Yes",
131
+ codec: item.Format.toLowerCase(),
132
+ order: videoOrder++,
133
+ width: Number(item.Width),
134
+ height: Number(item.Height),
135
+ profile: item.Format_Profile,
136
+ bitrate: Number(item.BitRate),
137
+ framerate: Number(item.FrameRate) ? Math.round(Number(item.FrameRate)) : 0,
138
+ // sample_rate: Number(item.SamplingRate),
139
+ duration: Number(item.Duration),
140
+ bitdepth: Number(item.BitDepth),
141
+ format_profile: item.Format_Profile,
142
+ format_tier: item.Format_Tier
143
+ });
144
+ } else if (type === "General") {
145
+ info.general = {
146
+ audioCount: Number(item.AudioCount),
147
+ textCount: Number(item.TextCount) || 0,
148
+ videoCount: Number(item.VideoCount),
149
+ fileSize: Number(item.FileSize),
150
+ title: item.Title || "",
151
+ duration: Number(item.Duration),
152
+ codec: item.Format
153
+ };
154
+ if (!info.general.duration && item.extra?.duration) {
155
+ info.general.duration = Number(item.extra.duration);
156
+ }
157
+ }
158
+ });
159
+ return info;
160
+ }
161
+ export {
162
+ parseMediaInfo,
163
+ parseMediaRaw
164
+ };
package/package.json CHANGED
@@ -1,31 +1,31 @@
1
1
  {
2
2
  "name": "@soga/mediainfo",
3
- "version": "0.3.0",
4
3
  "publishConfig": {
5
4
  "access": "public"
6
5
  },
7
- "description": "",
8
- "main": "dist/main.js",
9
- "types": "dist/main.d.ts",
6
+ "version": "0.5.0",
7
+ "main": "./dist/index.js",
8
+ "module": "./dist/index.mjs",
9
+ "types": "./dist/index.d.ts",
10
+ "scripts": {
11
+ "build": "rimraf dist && tsup src/index.ts --format cjs,esm --dts",
12
+ "dev": "tsup src/index.ts --format cjs,esm --dts --watch"
13
+ },
10
14
  "files": [
11
15
  "dist"
12
16
  ],
13
17
  "keywords": [],
14
18
  "author": "",
15
19
  "license": "ISC",
20
+ "description": "",
16
21
  "dependencies": {
17
- "@soga/types": "^0.3.0",
18
22
  "fs-extra": "^11.3.0",
19
- "m3u8-parser": "^7.2.0",
20
23
  "mediainfo.js": "^0.3.5",
21
- "mp4box": "^0.5.3"
24
+ "tsup": "^8.5.0"
22
25
  },
23
- "scripts": {
24
- "build": "rimraf dist && tsc && ts-node ./scripts/minify",
25
- "minify": "ts-node ./scripts/minify",
26
- "demo": "ts-node ./demo/demo.ts",
27
- "test": "jest",
28
- "dev": "ts-node ./src/main.ts",
29
- "lint": "eslint . --ext .ts"
30
- }
31
- }
26
+ "devDependencies": {
27
+ "@types/fs-extra": "^11.0.4",
28
+ "rimraf": "^6.0.1"
29
+ },
30
+ "prepublishOnly": "npm run build"
31
+ }
package/README.md DELETED
@@ -1,5 +0,0 @@
1
- ```ts
2
- import { parseMediaInfo } from '@soga/mediainfo';
3
- const mediaInfo = await parseMediaInfo('path/to/file');
4
- console.log(mediaInfo);
5
- ```
package/dist/m3u8.d.ts DELETED
@@ -1,6 +0,0 @@
1
- import { M3U8Manifest } from './m3u8.types';
2
- export declare function parseM3U8(input: string): Promise<{
3
- duration: number;
4
- data: M3U8Manifest;
5
- input: string;
6
- }>;
package/dist/m3u8.js DELETED
@@ -1 +0,0 @@
1
- "use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.parseM3U8=parseM3U8;const fs_extra_1=require("fs-extra"),m3u8_parser_1=require("m3u8-parser"),path_1=require("path");async function parseM3U8(e){const t=Date.now(),r=new m3u8_parser_1.Parser,s=await(0,fs_extra_1.readFile)(e,"utf8");r.push(s),r.end();const a=r.manifest,i=(0,path_1.dirname)(e),n=a.segments[0].map.uri,u=(0,path_1.resolve)(i,n),_={uri:n,folder:i,file_path:u,size:(await(0,fs_extra_1.stat)(u)).size};let o=0;const p=1e6,d=[],{length:f}=a.segments;for(let e=0;e<f;e++){const t=a.segments[e],r=(0,path_1.resolve)(i,t.uri),s=await(0,fs_extra_1.stat)(r),u=t.duration*p,_=o/p,f=(o+u)/p;d.push({index:e,duration:t.duration,init_uri:n,uri:t.uri,file_path:r,size:s.size,start_time:_,end_time:f,folder:i}),o+=u}const l={init:_,...a,segments:d};return{duration:Date.now()-t,data:l,input:e}}
@@ -1,35 +0,0 @@
1
- type CommonManifest = {
2
- version: number;
3
- targetDuration: number;
4
- mediaSequence: number;
5
- endList: boolean;
6
- };
7
- export type M3U8RawManifest = CommonManifest & {
8
- segments: {
9
- duration: number;
10
- uri: string;
11
- map: {
12
- uri: string;
13
- };
14
- }[];
15
- };
16
- export type M3U8SegmentInfo = {
17
- index: number;
18
- duration: number;
19
- init_uri: string;
20
- uri: string;
21
- file_path: string;
22
- folder: string;
23
- size: number;
24
- start_time: number;
25
- end_time: number;
26
- };
27
- export type M3U8Manifest = CommonManifest & {
28
- init: {
29
- uri: string;
30
- file_path: string;
31
- size: number;
32
- };
33
- segments: M3U8SegmentInfo[];
34
- };
35
- export {};
@@ -1 +0,0 @@
1
- "use strict";Object.defineProperty(exports,"__esModule",{value:!0});
package/dist/main.d.ts DELETED
@@ -1,8 +0,0 @@
1
- export * from './mediainfo';
2
- export * from './mediainfo.types';
3
- export * from './mp4box';
4
- export * from './mp4box.types';
5
- export * from './resolution';
6
- export * from './resolution.types';
7
- export * from './m3u8';
8
- export * from './m3u8.types';
package/dist/main.js DELETED
@@ -1 +0,0 @@
1
- "use strict";var __createBinding=this&&this.__createBinding||(Object.create?function(e,r,t,o){void 0===o&&(o=t);var i=Object.getOwnPropertyDescriptor(r,t);i&&!("get"in i?!r.__esModule:i.writable||i.configurable)||(i={enumerable:!0,get:function(){return r[t]}}),Object.defineProperty(e,o,i)}:function(e,r,t,o){void 0===o&&(o=t),e[o]=r[t]}),__exportStar=this&&this.__exportStar||function(e,r){for(var t in e)"default"===t||Object.prototype.hasOwnProperty.call(r,t)||__createBinding(r,e,t)};Object.defineProperty(exports,"__esModule",{value:!0}),__exportStar(require("./mediainfo"),exports),__exportStar(require("./mediainfo.types"),exports),__exportStar(require("./mp4box"),exports),__exportStar(require("./mp4box.types"),exports),__exportStar(require("./resolution"),exports),__exportStar(require("./resolution.types"),exports),__exportStar(require("./m3u8"),exports),__exportStar(require("./m3u8.types"),exports);
@@ -1,8 +0,0 @@
1
- import type { Mediainfo } from './mediainfo.types';
2
- import { RecordType } from '@soga/types';
3
- export declare function parseMediaRaw(media_path: string): Promise<any>;
4
- export declare function parseMediaInfo(media_path: string): Promise<Mediainfo>;
5
- export declare function judgeMediaType(filepath: string): Promise<RecordType.VIDEO | RecordType.AUDIO>;
6
- export declare const isVideoSupport: (format: string) => boolean;
7
- export declare const isAudioAdapt: (codec: string) => boolean;
8
- export declare const isAudioHigh: (codec: string) => boolean;
package/dist/mediainfo.js DELETED
@@ -1 +0,0 @@
1
- "use strict";var __importDefault=this&&this.__importDefault||function(e){return e&&e.__esModule?e:{default:e}};Object.defineProperty(exports,"__esModule",{value:!0}),exports.isAudioHigh=exports.isAudioAdapt=exports.isVideoSupport=void 0,exports.parseMediaRaw=parseMediaRaw,exports.parseMediaInfo=parseMediaInfo,exports.judgeMediaType=judgeMediaType;const fs_1=require("fs"),promises_1=require("fs/promises"),mediainfo_js_1=__importDefault(require("mediainfo.js")),types_1=require("@soga/types");async function parseMediaRaw(e){const t=await(0,mediainfo_js_1.default)({format:"JSON"}),a="string"==typeof e?[e]:e;let r=0;const i=[];for await(const[e,t]of a.entries()){const a=(await(0,promises_1.stat)(t)).size;i.push({index:e,start:r,end:r+a-1,size:a,file_path:t}),r+=a}const o=await t.analyzeData((()=>r),(async(e,t)=>{if(!e)return new Uint8Array(0);const a=t,r=t+e-1,o=i.find((e=>e.start<=a&&e.end>=a)),s=i.find((e=>e.start<=r&&e.end>=r));if(!o||!s)return new Uint8Array(0);const n=[];if(o.index===s.index)n.push({index:o.index,file_path:o.file_path,from:a-o.start,to:r-o.start});else{n.push({index:o.index,file_path:o.file_path,from:a-o.start,to:o.end});for(let e=o.index+1;e<s.index;e++){const t=i[e];n.push({index:t.index,file_path:t.file_path,from:0,to:t.size-1})}n.push({index:s.index,file_path:s.file_path,from:0,to:r-s.start})}const u=[];for await(const e of n){const{file_path:t,from:a,to:r}=e,i=(0,fs_1.createReadStream)(t,{start:a,end:r});for await(const e of i)u.push(e);i.close()}return Buffer.concat(u)}));try{return JSON.parse(o)}catch(e){const t=o.replace(',"":{}]}]}',',"":{}}]}');return JSON.parse(t)}}async function parseMediaInfo(e){const t=(await parseMediaRaw(e)).media.track,a={audio:[],video:[],text:[],general:{}};let r=0,i=0,o=0;return t.forEach((e=>{const t=e["@type"];"Audio"===t?a.audio.push({is_default:"Yes"===e.Default,codec:e.Format.toLowerCase(),order:r++,bitrate:Number(e.BitRate),framerate:Number(e.FrameRate),sample_rate:Number(e.SamplingRate),duration:Number(e.Duration),channels:Number(e.Channels),lossless:"Lossless"===e.Compression_Mode}):"Text"===t?a.text.push({is_default:"Yes"===e.Default,codec:e.Format.toLowerCase(),order:o++,duration:Number(e.Duration),title:e.Title||e.Language,language:e.Language}):"Video"===t?a.video.push({is_default:"Yes"===e.Default,codec:e.Format.toLowerCase(),order:i++,width:Number(e.Width),height:Number(e.Height),profile:e.Format_Profile,bitrate:Number(e.BitRate),framerate:Number(e.FrameRate)?Math.round(Number(e.FrameRate)):void 0,duration:Number(e.Duration),bitdepth:Number(e.BitDepth),format_profile:e.Format_Profile,format_tier:e.Format_Tier}):"General"===t&&(a.general={audioCount:Number(e.AudioCount),textCount:Number(e.TextCount)||0,videoCount:Number(e.VideoCount),fileSize:Number(e.FileSize),title:e.Title||"",duration:Number(e.Duration),codec:e.Format},!a.general.duration&&e.extra?.duration&&(a.general.duration=Number(e.extra.duration)))})),a}async function judgeMediaType(e){const t=await parseMediaInfo(e);return t?.video?.length?types_1.RecordType.VIDEO:types_1.RecordType.AUDIO}const isVideoSupport=e=>["avc","h264","avc1","hevc","h265"].includes(e);exports.isVideoSupport=isVideoSupport;const isAudioAdapt=e=>["aac","mp3"].includes(e);exports.isAudioAdapt=isAudioAdapt;const isAudioHigh=e=>["flac"].includes(e);exports.isAudioHigh=isAudioHigh;
@@ -1 +0,0 @@
1
- "use strict";Object.defineProperty(exports,"__esModule",{value:!0});
package/dist/mp4box.d.ts DELETED
@@ -1,7 +0,0 @@
1
- import { Mp4boxAudioInfo, Mp4boxVideoInfo } from './mp4box.types';
2
- export declare function getMp4boxInfo(filePath: string): Promise<{
3
- duration: number;
4
- mime: string;
5
- audios: Mp4boxAudioInfo[];
6
- videos: Mp4boxVideoInfo[];
7
- }>;
package/dist/mp4box.js DELETED
@@ -1 +0,0 @@
1
- "use strict";var __importDefault=this&&this.__importDefault||function(e){return e&&e.__esModule?e:{default:e}};Object.defineProperty(exports,"__esModule",{value:!0}),exports.getMp4boxInfo=getMp4boxInfo;const fs_extra_1=require("fs-extra"),mp4box_1=__importDefault(require("mp4box"));async function getMp4boxInfo(e){const t=await new Promise(((t,r)=>{const o=mp4box_1.default.createFile();let n=0;const a=(0,fs_extra_1.createReadStream)(e,{highWaterMark:41943040});a.on("data",(e=>{const t=new Uint8Array(e).buffer;t.fileStart=n,n+=e.byteLength,o.appendBuffer(t)})),a.on("end",(()=>{o.flush()})),a.on("error",(e=>{r(e)})),o.onError=function(e){a.destroy(),o.flush(),r(e)},o.onReady=function(e){a.destroy(),o.flush(),t(e)}})),r=t.tracks.filter((e=>!!e.audio||"SoundHandler"==e.name)),o=t.tracks.filter((e=>!!e.video||"VideoHandler"==e.name));return{duration:t.duration,mime:t.mime,audios:r,videos:o}}
@@ -1,75 +0,0 @@
1
- export type Mp4boxVideoInfo = {
2
- id: number;
3
- name?: 'VideoHandler';
4
- references: any[];
5
- edits: {
6
- segment_duration: number;
7
- media_time: number;
8
- media_rate_integer: number;
9
- media_rate_fraction: number;
10
- }[];
11
- created: string;
12
- modified: string;
13
- movie_duration: number;
14
- movie_timescale: number;
15
- layer: number;
16
- alternate_group: number;
17
- volume: number;
18
- track_width: number;
19
- track_height: number;
20
- timescale: number;
21
- duration: number;
22
- samples_duration: number;
23
- codec: string;
24
- kind: {
25
- schemeURI: string;
26
- value: string;
27
- };
28
- language: string;
29
- nb_samples: number;
30
- size: number;
31
- bitrate: number;
32
- type: 'video';
33
- video: {
34
- width: number;
35
- height: number;
36
- };
37
- };
38
- export type Mp4boxAudioInfo = {
39
- id: number;
40
- name?: 'SoundHandler';
41
- references: any[];
42
- edits: {
43
- segment_duration: number;
44
- media_time: number;
45
- media_rate_integer: number;
46
- media_rate_fraction: number;
47
- }[];
48
- created: string;
49
- modified: string;
50
- movie_duration: number;
51
- movie_timescale: number;
52
- layer: number;
53
- alternate_group: number;
54
- volume: number;
55
- track_width: number;
56
- track_height: number;
57
- timescale: number;
58
- duration: number;
59
- samples_duration: number;
60
- codec: string;
61
- kind: {
62
- schemeURI: string;
63
- value: string;
64
- };
65
- language: string;
66
- nb_samples: number;
67
- size: number;
68
- bitrate: number;
69
- type: 'audio';
70
- audio: {
71
- sample_rate: number;
72
- channel_count: number;
73
- sample_size: number;
74
- };
75
- };
@@ -1 +0,0 @@
1
- "use strict";Object.defineProperty(exports,"__esModule",{value:!0});
@@ -1,6 +0,0 @@
1
- import type { StandardItem } from './resolution.types';
2
- export declare function getVideoStandard(width: number, height: number): {
3
- standard: StandardItem;
4
- adapts: StandardItem[];
5
- };
6
- export declare const getStandardInfo: (width: number, height: number) => StandardItem;
@@ -1 +0,0 @@
1
- "use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.getStandardInfo=void 0,exports.getVideoStandard=getVideoStandard;const resolutionMap=new Map,horizontal_resolutions=[{key:"h_p360",is_horizontal:!0,width:640,height:360,label:"360p",tag:"",edge:"width",codec:"libx264"},{key:"h_p540",is_horizontal:!0,width:960,height:540,label:"540p",tag:"",edge:"width",codec:"libx264"},{key:"h_p720",is_horizontal:!0,width:1280,height:720,label:"720p",tag:"",edge:"width",codec:"libx264"},{key:"h_p1080",is_horizontal:!0,width:1920,height:1080,label:"1080p",tag:"HD",edge:"width",codec:"libx264"},{key:"h_p1440",is_horizontal:!0,width:2560,height:1440,label:"1440p",tag:"HD",edge:"width",codec:"libx265"},{key:"h_k4",is_horizontal:!0,width:3840,height:2160,label:"2160p",tag:"4K",edge:"width",codec:"libx265"},{key:"h_k8",is_horizontal:!0,width:7680,height:4320,label:"4320p",tag:"8K",edge:"width",codec:"libx265"},{key:"h_k16",is_horizontal:!0,width:15360,height:8640,label:"16K",tag:"",edge:"width",codec:"libx265"},{key:"h_k32",is_horizontal:!0,width:30720,height:17280,label:"32K",tag:"32K",edge:"width",codec:"libx265"},{key:"h_k32plus",is_horizontal:!0,width:61440,height:34560,label:"64K",tag:"",edge:"width",codec:"libx265"}],vertical_resolutions=[{key:"v_p360",is_horizontal:!1,height:640,width:360,label:"360p",tag:"",edge:"height",codec:"libx264"},{key:"v_p540",is_horizontal:!1,height:960,width:540,label:"540p",tag:"",edge:"height",codec:"libx264"},{key:"v_p720",is_horizontal:!1,height:1280,width:720,label:"720p",tag:"",edge:"height",codec:"libx264"},{key:"v_p1080",is_horizontal:!1,height:1920,width:1080,label:"1080p",tag:"HD",edge:"height",codec:"libx264"},{key:"v_p1440",is_horizontal:!1,height:2560,width:1440,label:"1440p",tag:"HD",edge:"height",codec:"libx265"},{key:"v_k4",is_horizontal:!1,height:3840,width:2160,label:"2160p",tag:"4K",edge:"height",codec:"libx265"},{key:"v_k8",is_horizontal:!1,height:7680,width:4320,label:"4320p",tag:"8K",edge:"height",codec:"libx265"},{key:"v_k16",is_horizontal:!1,height:15360,width:8640,label:"16K",tag:"",edge:"height",codec:"libx265"},{key:"v_k32",is_horizontal:!1,height:30720,width:17280,label:"32K",tag:"32K",edge:"height",codec:"libx265"},{key:"v_k32plus",is_horizontal:!1,height:61440,width:34560,label:"64K",tag:"",edge:"height",codec:"libx265"}];function getVideoStandard(e,t){if(e<t){const i=[],h=[],{length:o}=vertical_resolutions;for(let l=0;l<o;l++){const o=vertical_resolutions[l],a=t>=.9*vertical_resolutions[l].height,d=e>=.9*vertical_resolutions[l].width;if(a&&i.unshift({...o,edge:"height"}),d&&h.unshift({...o,edge:"width"}),!a&&!d)break}const l=i.shift()??vertical_resolutions[0],a=h.shift()??vertical_resolutions[0],d=l.height>a.height;return{standard:d?l:a,adapts:d?i:h}}{const i=[],h=[],{length:o}=horizontal_resolutions;for(let l=0;l<o;l++){const o=horizontal_resolutions[l],a=e>=.9*horizontal_resolutions[l].width,d=t>=.9*horizontal_resolutions[l].height;if(a&&i.unshift({...o,edge:"width"}),d&&h.unshift({...o,edge:"height"}),!a&&!d)break}const l=i.shift()??horizontal_resolutions[0],a=h.shift()??horizontal_resolutions[0],d=l.width>a.width;return{standard:d?l:a,adapts:d?i:h}}}horizontal_resolutions.forEach((e=>{resolutionMap.set(`horizontal_${e.width}`,e)})),vertical_resolutions.forEach((e=>{resolutionMap.set(`vertical_${e.height}`,e)}));const getStandardInfo=(e,t)=>{const{standard:i}=getVideoStandard(e,t);return i};exports.getStandardInfo=getStandardInfo;
@@ -1,11 +0,0 @@
1
- export type StandardKey = 'h_p360' | 'h_p540' | 'h_p720' | 'h_p1080' | 'h_p1440' | 'h_k4' | 'h_k8' | 'h_k16' | 'h_k32' | 'h_k32plus' | 'v_p360' | 'v_p540' | 'v_p720' | 'v_p1080' | 'v_p1440' | 'v_k4' | 'v_k8' | 'v_k16' | 'v_k32' | 'v_k32plus';
2
- export type StandardItem = {
3
- key: StandardKey;
4
- is_horizontal: boolean;
5
- width: number;
6
- height: number;
7
- label: string;
8
- tag: string;
9
- edge: 'width' | 'height';
10
- codec: string;
11
- };
@@ -1 +0,0 @@
1
- "use strict";Object.defineProperty(exports,"__esModule",{value:!0});