-
Notifications
You must be signed in to change notification settings - Fork 4
/
events.ts
140 lines (120 loc) · 3.34 KB
/
events.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
import type { Encoding } from "./encoding.ts";
import type { MediaInfo } from "./media_info.ts";
export type EncodingEventType = "info" | "start" | "progress" | "end" | "error";
export type EncodingEventListener =
| EncodingInfoEventListener
| EncodingStartEventListener
| EncodingProgressEventListener
| EncodingEndEventListener
| EncodingErrorEventListener;
export type EncodingEvent =
| EncodingInfoEvent
| EncodingStartEvent
| EncodingProgressEvent
| EncodingEndEvent
| EncodingErrorEvent;
abstract class AbstractEncodingEvent<T extends EncodingEventType> {
readonly type: T;
readonly #encoding: Encoding;
protected constructor(
name: T,
encoding: Encoding,
) {
this.type = name;
this.#encoding = encoding;
}
get encoding(): Encoding {
return this.#encoding;
}
}
export type EncodingInfoEventListener = (
event: EncodingInfoEvent,
) => void;
export class EncodingInfoEvent extends AbstractEncodingEvent<"info"> {
readonly #info: MediaInfo;
constructor(encoding: Encoding, info: MediaInfo) {
super("info", encoding);
this.#info = info;
}
get info(): MediaInfo {
return this.#info;
}
}
export type EncodingStartEventListener = (
event: EncodingStartEvent,
) => void;
export class EncodingStartEvent extends AbstractEncodingEvent<"start"> {
constructor(encoding: Encoding) {
super("start", encoding);
}
}
export type EncodingProgressEventListener = (
event: EncodingProgressEvent,
) => void;
export class EncodingProgressEvent extends AbstractEncodingEvent<"progress"> {
readonly frame: number;
readonly fps: number;
readonly bitrate: string;
readonly totalSize: number;
readonly outTimeMs: number;
readonly outTime: string;
readonly dupFrames: number;
readonly dropFrames: number;
readonly speed: number;
readonly progress: number;
readonly done: boolean;
constructor(
encoding: Encoding,
progress: number,
info: ProgressInfo,
) {
super("progress", encoding);
this.frame = parseInt(info.frame);
this.fps = parseFloat(info.fps);
this.bitrate = info.bitrate;
this.totalSize = parseInt(info.total_size);
this.outTimeMs = parseInt(info.out_time_ms);
this.outTime = info.out_time;
this.dupFrames = parseInt(info.dup_frames);
this.dropFrames = parseInt(info.drop_frames);
this.speed = parseFloat(info.speed);
this.progress = progress;
this.done = info.progress === "end";
}
}
// FFmpegProgressInfo
export interface ProgressInfo {
frame: string;
fps: string;
// deno-lint-ignore camelcase
stream_0_0_q: string;
bitrate: string;
// deno-lint-ignore camelcase
total_size: string;
// deno-lint-ignore camelcase
out_time_ms: string;
// deno-lint-ignore camelcase
out_time: string;
// deno-lint-ignore camelcase
dup_frames: string;
// deno-lint-ignore camelcase
drop_frames: string;
speed: string;
progress: "continue" | "end";
}
export type EncodingEndEventListener = (
event: EncodingEndEvent,
) => void;
export class EncodingEndEvent extends AbstractEncodingEvent<"end"> {
constructor(encoding: Encoding) {
super("end", encoding);
}
}
export class EncodingErrorEvent extends AbstractEncodingEvent<"error"> {
constructor(encoding: Encoding, readonly error: Error) {
super("error", encoding);
}
}
export type EncodingErrorEventListener = (
event: EncodingErrorEvent,
) => void;