pr-player 0.0.1

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/README.md ADDED
@@ -0,0 +1,340 @@
1
+ # 一些常用的js方法:时间处理、数组处理、md5加密、uuid、随机中文名等各种转换。
2
+
3
+ ## 立即开始
4
+
5
+ ### 安装
6
+
7
+ ```bash
8
+ npm i pr-player
9
+ ```
10
+
11
+ ### 引入
12
+
13
+ ```js
14
+ // 按需引入
15
+ import { uuid, random, randomName, md5, regExps, timeFormat, timeFrom, line2hump, hump2line } from 'pr-player'
16
+
17
+ // 或全量引入
18
+ import * as prTools from 'pr-player'
19
+ ```
20
+
21
+ ### 一些例子
22
+
23
+ ```js
24
+ // uuid
25
+ {
26
+ let str = uuid(32, 16)
27
+ console.log('\x1b[38;2;0;151;255m%c%s', 'color:#0097ff', `------->Breathe:test:uuid`, str)
28
+ }
29
+ {
30
+ // random
31
+ let str = random(100000, 999999) // 6位数字
32
+ console.log('\x1b[38;2;0;151;255m%c%s', 'color:#0097ff', `------->Breathe:test:random`, str)
33
+ }
34
+ {
35
+ // randomName
36
+ let str = randomName(2, 4) // 2-4位昵称
37
+ console.log('\x1b[38;2;0;151;255m%c%s', 'color:#0097ff', `------->Breathe:test:randomName`, str)
38
+ }
39
+ // filterKeys
40
+ {
41
+ const obj = { name: 'a', age: 10, phone: 22, 1: 3, '2': 44 }
42
+ let res = prTools.filterKeys(obj, ['phone', 'age', 1])
43
+ console.log('\x1b[38;2;0;151;255m%c%s', 'color:#0097ff', `------->Breathe:res`, res)
44
+ }
45
+
46
+ // arrFilterDup
47
+ {
48
+ const arr = [
49
+ { name: 'a', age: 10, phone: 123 },
50
+ { name: 'b', age: 12, phone: 456 },
51
+ { name: 'c', age: 10, phone: 789 }
52
+ ]
53
+ let res = prTools.arrFilterDup(arr, ['age'], true)
54
+ console.log('\x1b[38;2;0;151;255m%c%s', 'color:#0097ff', `------->Breathe:res`, res)
55
+ }
56
+
57
+ // arrFromEnum
58
+ {
59
+ const enum_template = {
60
+ lable_1: 'value_1',
61
+ lable_2: 'value_2',
62
+ lable_3: 'value_3',
63
+ lable_4: 'value_4'
64
+ } as const
65
+
66
+ const res = arrFromEnum(enum_template, 'key')
67
+ console.log('\x1b[38;2;0;151;255m%c%s', 'color:#0097ff', `------->Breathe:res`, res)
68
+
69
+ // 另外可能会用到的定义
70
+ type O = typeof enum_template
71
+ type K = keyof O // "lable_1" | "lable_2" | "lable_3" | "lable_4"
72
+ type V = O[K] // "value_1" | "value_2" | "value_3" | "value_4"
73
+ }
74
+
75
+ // md5
76
+ {
77
+ let str = md5('123456')
78
+ console.log('\x1b[38;2;0;151;255m%c%s', 'color:#0097ff', `------->Breathe:test:md5`, str)
79
+ }
80
+
81
+ // timeFormat
82
+ {
83
+ let str = timeFormat('2024/11/06 04:06:06', 'YYYY-MM-DD hh:mm:ss 星期WWW')
84
+ console.log('\x1b[38;2;0;151;255m%c%s', 'color:#0097ff', `------->Breathe:test:timeFormat`, str)
85
+ }
86
+ ```
87
+
88
+ #### highlight 字符串高亮
89
+
90
+ ```js
91
+ {
92
+ let res = prTools.highlight('123456', ['3', '5'])
93
+ console.log('\x1b[38;2;0;151;255m%c%s\x1b[0m', 'color:#0097ff;padding:16px 0;', `------->Breathe:res`, res)
94
+ }
95
+ ```
96
+
97
+ #### 在 react 中使用
98
+
99
+ ```jsx
100
+ <div dangerouslySetInnerHTML={{ __html: highlight('123456', ['3', '5']) }}></div>
101
+ ```
102
+
103
+ #### 在 vue 中使用
104
+
105
+ ```vue
106
+ <div v-html="highlight('123456', ['3', '5'])"></div>
107
+ ```
108
+
109
+ ### 更多的函数
110
+
111
+ #### 时间相关函数
112
+
113
+ ```js
114
+ /**
115
+ * 获取时间戳 失败返回 0
116
+ * @param _val Date | number | string
117
+ * @example timeStamp()
118
+ * @example timeStamp('2024-05-23')
119
+ * @returns 转换后的时间戳 | 0
120
+ */
121
+
122
+ /**
123
+ * 格式化时间
124
+ * @param _val Date | number | string
125
+ * @param _format 格式化模板 YYYY-MM-DD hh:mm:ss
126
+ * @param _options _options: { offset?: number; empty_str?: string }
127
+ * @example timeFormat('2024/09/24 04:06:06', 'YYYY-MM-DD hh:mm:ss')
128
+ * @returns 格式化后的字符串
129
+ */
130
+
131
+ /**
132
+ * 多久之前时间
133
+ * @param _val Date | number | string
134
+ * @param format 格式化模板 YYYY-MM-DD hh:mm:ss
135
+ * @param _options _options: { offset?: number; empty_str?: string }
136
+ * @example timeFrom(new Date().getTime() - 5600000)
137
+ * @returns 格式化后的字符串
138
+ */
139
+
140
+ /**
141
+ * 获取某个时间的范围日期
142
+ * @param _val Date | number | string
143
+ * @param _options _options: { offset?: number; empty_str?: string }
144
+ * @example timeRange(new Date().getTime())
145
+ * @returns [] 该范围的每一天集合
146
+ */
147
+ ```
148
+
149
+ #### 数组相关函数
150
+
151
+ ```js
152
+ /**
153
+ * 把一纬数组按指定长度分割
154
+ * @param _arr 一纬数组
155
+ * @param _size 分割长度
156
+ * @example arrSlice([1, 2, 3, 4, 5], 2)
157
+ * @returns 分割后的二维数组
158
+ */
159
+
160
+ /**
161
+ * 在数组里面向上向下取整数的一个范围
162
+ * @param _arr 多个数值的数组
163
+ * @param _accuracy 间隔精度
164
+ * @returns [min,max]
165
+ * @notes 例如传入[-13,37,67] 返回 [-20,70] ,类似于[0,0]将会返回[0,10]
166
+ * @notes 常在echart中使用 const [yAxisMin, yAxisMax] = arrRange([1,87], 10) // 取区间整数 [1,87] => [0,90]
167
+ * @example arrRange([-13, 37, 67]) // [-20,70]
168
+ * @example arrRange([0, 0], 10) // [0,10]
169
+ */
170
+
171
+ /**
172
+ * 数组去重
173
+ * @param _arr 数组
174
+ * @param _keys 根据哪些字段去重
175
+ * @param _cover 如果有重复 是否进行覆盖 默认为 true
176
+ * @example arrFilterDup(arr, ['name', 'age'])
177
+ * @returns 去重后的数组
178
+ */
179
+
180
+ /**
181
+ * 筛选 数组对象 中指定的key
182
+ * @param _arr 需要筛选的对象数组
183
+ * @param _keys 需要筛选哪些字段
184
+ * @example arrFilterKeys(arr, ['label', 'name'])
185
+ * @returns 筛选后结果 传入对象返回对象 传入数组返回数组
186
+ */
187
+
188
+ /**
189
+ * 根据一个枚举对象生成 常用的键值对数组
190
+ * @param _enum 枚举对象 { value: label }
191
+ * @param _value_name 值名
192
+ * @param _label_name 键名
193
+ * @example arrFromEnum(obj, ['value', 'label'])
194
+ * @returns 键值对数组
195
+ */
196
+ ```
197
+
198
+ #### 随机值相关函数
199
+
200
+ ```js
201
+ /**
202
+ * 随机生成区间数字
203
+ * @param _min 最小数字(大于等于0)
204
+ * @param _max 最大数字(大于等于1)
205
+ * @example random(100000, 999999)
206
+ * @returns 随机区间数字
207
+ */
208
+
209
+ /**
210
+ * 随机生成uuid
211
+ * @param _len 长度
212
+ * @param _radix 进制 为了保证唯一性 进制过低时会 按照最低长度返回
213
+ * @example uuid(32, 16)
214
+ * @returns 随机uuid
215
+ */
216
+
217
+ /**
218
+ * 随机生成昵称
219
+ * @param _min 最小名称长度(最小1)
220
+ * @param _max 最大名称长度(大于等于1)
221
+ * @example randomName(2, 4)
222
+ * @returns 随机昵称
223
+ */
224
+ ```
225
+
226
+ #### md5 加密
227
+
228
+ ```js
229
+ /**
230
+ * 对字符串进行md5加密
231
+ * @param _string 需要加密的字符串
232
+ * @example md5('123456')
233
+ * @returns 加密后的字符串
234
+ */
235
+ ```
236
+
237
+ #### 转换相关函数
238
+
239
+ ```js
240
+ /**
241
+ * ArrayBuffer转十六进制
242
+ * @param _buffer arrayBuffer
243
+ * @returns 十六进制字符串
244
+ */
245
+
246
+ /**
247
+ * 十六进制转ArrayBuffer
248
+ * @param _str 十六进制字符串
249
+ * @returns buffer
250
+ */
251
+
252
+ /**
253
+ * 十六进制转ASCII码
254
+ * @param _hexCharCodeStr 16进制字符串
255
+ * @returns 转换后的ASCII码
256
+ */
257
+
258
+ /**
259
+ * 短划线转换驼峰
260
+ * @param _str 短横线字符串
261
+ * @returns 驼峰字符串
262
+ */
263
+
264
+ /**
265
+ * 驼峰转换短横线
266
+ * @param _str 驼峰字符串
267
+ * @returns 短横线字符串
268
+ */
269
+
270
+ /**
271
+ * 去除首尾空格
272
+ * @param _str 字符串
273
+ * @returns 结果字符串
274
+ */
275
+
276
+ /**
277
+ * 字节单位转换
278
+ * @param _bytes 字节
279
+ * @param _splitStr 值与单位的分割符 默认为一个空格
280
+ * @returns 格式化后的字符串
281
+ */
282
+
283
+ /**
284
+ * 把数字分割为千分位计量的字符串
285
+ * @param _number 数值
286
+ * @example num2split(123456789)
287
+ * @returns 千分位计量的字符串
288
+ */
289
+
290
+ /**
291
+ * 筛选 对象 中指定的key
292
+ * @param _obj 需要筛选的 对象
293
+ * @param _keys 需要筛选哪些字段
294
+ * @example filterKeys(_obj, ['lable', 'name'])
295
+ * @returns 筛选后结果 传入对象返回对象 传入数组返回数组
296
+ */
297
+
298
+ /**
299
+ * 生成高亮字符串的html
300
+ * @param _text 当前文本字符串
301
+ * @param _keys 关键词数组 string[]
302
+ * @example highlight('123456', ['3', '5'])
303
+ * @returns 处理后的 html 字符串
304
+ */
305
+ ```
306
+
307
+ #### 其他
308
+
309
+ ```js
310
+ /**
311
+ * 分段执行
312
+ * @param _cuont 一共执行多少次 最小为 0
313
+ * @param _step 每次执行多少次 最小为 1
314
+ * @example exeStep(98, 7, ()=>{})
315
+ * @returns 筛选后结果 传入对象返回对象 传入数组返回数组
316
+ */
317
+
318
+ /**
319
+ * 延迟执行 强制将请求延长时间以模拟 loading
320
+ * @param _func 需要执行的函数
321
+ * @param _timeout 最小执行时间ms 默认 500ms
322
+ * @example await exeDelayed(()=>{}, 500)
323
+ * @returns
324
+ */
325
+
326
+ /**
327
+ * 检查函数执行消耗时间
328
+ * @param _func 需要执行的函数
329
+ * @example const elapsed = await exeElapsed(()=>{})
330
+ * @returns elapsed 消耗时间 ms
331
+ */
332
+ ```
333
+
334
+ ## 代码仓库
335
+
336
+ [github](https://github.com/breathe97/pr-player)
337
+
338
+ ## 贡献
339
+
340
+ breathe
@@ -0,0 +1,71 @@
1
+ import { ScriptTag, AudioTag, VideoTag } from './demuxer/type';
2
+ interface On {
3
+ demuxer: {
4
+ script?: (_tag: ScriptTag) => void;
5
+ audio?: (_tag: AudioTag) => void;
6
+ video?: (_tag: VideoTag) => void;
7
+ sei?: (_payload: Uint8Array) => void;
8
+ };
9
+ decoder: {
10
+ audio?: (_AudioData: AudioData) => void;
11
+ video?: (_frame: {
12
+ timestamp: number;
13
+ bitmap: ImageBitmap;
14
+ }) => void;
15
+ };
16
+ stream?: (_stream: MediaStream) => void;
17
+ cutStream?: (_key: string, _stream: MediaStream) => void;
18
+ }
19
+ export declare class PrPlayer {
20
+ private prFetch;
21
+ private demuxerWorker;
22
+ private decoderWorker;
23
+ private audioPlayer;
24
+ private videoPlayerWorker;
25
+ private renderBaseTime;
26
+ private cutVideoPlayerWorkers;
27
+ private canvas;
28
+ on: On;
29
+ constructor();
30
+ /**
31
+ * 创建剪切
32
+ */
33
+ createCut: (key: string, cutOption: {
34
+ sx?: number;
35
+ sy?: number;
36
+ sw?: number;
37
+ sh?: number;
38
+ }, canvas?: HTMLCanvasElement, fps?: number) => HTMLCanvasElement;
39
+ /**
40
+ * 初始化
41
+ * @param canvas?: HTMLCanvasElement
42
+ */
43
+ init: (canvas?: HTMLCanvasElement) => void;
44
+ /**
45
+ * 开始播放
46
+ * @param url : string
47
+ */
48
+ start: (url: string) => Promise<void>;
49
+ /**
50
+ * 停止
51
+ */
52
+ stop: () => void;
53
+ /**
54
+ * 是否静音 默认为true
55
+ * @param state?: boolean
56
+ */
57
+ setMute: (state?: boolean) => void | undefined;
58
+ /**
59
+ * 监听媒体 tag
60
+ */
61
+ private onTag;
62
+ /**
63
+ * 初始化分离器
64
+ */
65
+ private initDemuxer;
66
+ /**
67
+ * 初始化渲染器
68
+ */
69
+ private initRender;
70
+ }
71
+ export {};
@@ -0,0 +1,14 @@
1
+ import { PrAudioStream } from 'pr-audio-stream';
2
+ export declare class AudioPlayer {
3
+ prAudioStream: PrAudioStream | undefined;
4
+ audioContext: AudioContext | undefined;
5
+ destination: MediaStreamAudioDestinationNode | undefined;
6
+ stream: MediaStream;
7
+ nextStartTime: number;
8
+ pendingSources: AudioBufferSourceNode[];
9
+ constructor();
10
+ init: (audioContext?: AudioContext) => void;
11
+ push(audioData: AudioData): Promise<void>;
12
+ getStream: () => MediaStream | undefined;
13
+ destroy(): void;
14
+ }
@@ -0,0 +1,22 @@
1
+ import { On } from './type';
2
+ export declare class Decoder {
3
+ private audioDecoderConfig;
4
+ private audioDecoder;
5
+ private videoDecoderConfig;
6
+ private videoDecoder;
7
+ private hasKeyFrame;
8
+ on: On;
9
+ constructor();
10
+ audio: {
11
+ init: (config: AudioDecoderConfig) => void;
12
+ decode: (init: EncodedAudioChunkInit) => void;
13
+ flush: () => void;
14
+ destroy: () => void;
15
+ };
16
+ video: {
17
+ init: (config: VideoDecoderConfig) => void;
18
+ decode: (init: EncodedVideoChunkInit) => void;
19
+ flush: () => void;
20
+ destroy: () => void;
21
+ };
22
+ }
@@ -0,0 +1,18 @@
1
+ import { On } from './type';
2
+ export declare class DecoderWorker {
3
+ worker: Worker;
4
+ on: On;
5
+ constructor();
6
+ audio: {
7
+ init: (config: AudioDecoderConfig) => void;
8
+ decode: (init: EncodedAudioChunkInit) => void;
9
+ flush: () => void;
10
+ destroy: () => void;
11
+ };
12
+ video: {
13
+ init: (config: VideoDecoderConfig) => void;
14
+ decode: (init: EncodedVideoChunkInit) => void;
15
+ flush: () => void;
16
+ destroy: () => void;
17
+ };
18
+ }
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,13 @@
1
+ export interface On {
2
+ audio: {
3
+ decode?: (_AudioData: AudioData) => void;
4
+ error?: (_e: DOMException) => void;
5
+ };
6
+ video: {
7
+ decode?: (_frame: {
8
+ bitmap: ImageBitmap;
9
+ timestamp: number;
10
+ }) => void;
11
+ error?: (_e: DOMException) => void;
12
+ };
13
+ }
@@ -0,0 +1,19 @@
1
+ import { On } from './type';
2
+ export declare class Demuxer {
3
+ private parseSpeed;
4
+ private parseTimer;
5
+ private pushFuncs;
6
+ private payload;
7
+ private offset;
8
+ private is_parsing;
9
+ private header;
10
+ private tag;
11
+ on: On;
12
+ constructor();
13
+ init: () => void;
14
+ push: (payload: Uint8Array) => void;
15
+ destroy: () => void;
16
+ private parse;
17
+ private parseHeader;
18
+ private parseTag;
19
+ }
@@ -0,0 +1,9 @@
1
+ import { On } from './type';
2
+ export declare class DemuxerWorker {
3
+ worker: Worker;
4
+ on: On;
5
+ constructor();
6
+ init: () => void;
7
+ push: (payload: Uint8Array) => void;
8
+ destroy: () => void;
9
+ }