-
Notifications
You must be signed in to change notification settings - Fork 4
/
encoding_event_stream_test.ts
143 lines (128 loc) · 4.39 KB
/
encoding_event_stream_test.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
141
142
143
import { assertInstanceOf } from "./_assertions.ts";
import {
assertEquals,
dirname,
ensureDir,
exists,
fromFileUrl,
} from "./dev_deps.ts";
import { Encoding } from "./encoding.ts";
import { EncodingProcess } from "./encoding_process.ts";
import { EncodingEventStream } from "./encoding_event_stream.ts";
import {
EncodingEndEvent,
EncodingErrorEvent,
EncodingInfoEvent,
EncodingStartEvent,
} from "./events.ts";
import { EncodingProgressEvent } from "./events.ts";
import { FFmpeg } from "./ffmpeg.ts";
const rootDir: string = dirname(fromFileUrl(import.meta.url));
const inputPath = `${rootDir}/fixtures/sample.mp4`;
Deno.test({
name: "custom encoding event stream",
async fn() {
const outputPath = `${rootDir}/.tmp/custom encoding event stream.mp4`;
await ensureDir(`${rootDir}/.tmp`);
const encoding = new Encoding();
encoding.input = inputPath;
encoding.output = outputPath;
encoding.override = true;
encoding.width = 1600;
const encodingProcess = new EncodingProcess(encoding);
const eventStream = new EncodingEventStream(encodingProcess);
encodingProcess.run();
let startEvent: EncodingStartEvent | undefined;
let infoEvent: EncodingInfoEvent | undefined;
let progressEvent: EncodingProgressEvent | undefined;
let endEvent: EncodingEndEvent | undefined;
let errorEvent: EncodingErrorEvent | undefined;
for await (const event of eventStream) {
switch (event.type) {
case "start":
startEvent = event;
break;
case "info":
infoEvent = event;
break;
case "progress":
progressEvent = event;
break;
case "end":
endEvent = event;
break;
case "error":
errorEvent = event;
break;
}
}
const outputFileExists: boolean = await exists(outputPath);
assertEquals(outputFileExists, true);
assertInstanceOf(startEvent, EncodingStartEvent);
assertInstanceOf(infoEvent, EncodingInfoEvent);
assertInstanceOf(progressEvent, EncodingProgressEvent);
assertInstanceOf(endEvent, EncodingEndEvent);
assertEquals(errorEvent, undefined);
assertEquals(progressEvent?.progress, 100);
const status = await encodingProcess.status();
assertEquals(status.code, 0);
assertEquals(status.success, true);
const output = await encodingProcess.output();
assertInstanceOf(output, Uint8Array);
const errorOutput = await encodingProcess.stderrOutput();
assertInstanceOf(errorOutput, Uint8Array);
encodingProcess.close();
},
});
Deno.test({
name: "multi encoding event stream",
async fn() {
const outputPath1 = `${rootDir}/.tmp/multi encoding event stream 1.mp4`;
const outputPath2 = `${rootDir}/.tmp/multi encoding event stream 2.mp4`;
const encoder = new FFmpeg()
.input(inputPath)
.override(true)
.width(200)
.output(outputPath1)
.output(outputPath2);
let startEvent: EncodingStartEvent | undefined;
let infoEvent: EncodingInfoEvent | undefined;
let progressEvent: EncodingProgressEvent | undefined;
let endEvent: EncodingEndEvent | undefined;
for await (const encodingProcess of encoder) {
const events = new EncodingEventStream(encodingProcess);
encodingProcess.run();
for await (const event of events) {
switch (event.type) {
case "start":
startEvent = event;
break;
case "info":
infoEvent = event;
break;
case "progress":
progressEvent = event;
break;
case "end":
endEvent = event;
break;
case "error":
throw event.error;
}
}
const status = await encodingProcess.status();
encodingProcess.close();
assertEquals(status.code, 0);
assertEquals(status.success, true);
}
const outputFile1Exists: boolean = await exists(outputPath1);
assertEquals(outputFile1Exists, true, "Missing output file 1.");
const outputFile2Exists: boolean = await exists(outputPath2);
assertEquals(outputFile2Exists, true, "Missing output file 2.");
assertInstanceOf(startEvent, EncodingStartEvent);
assertInstanceOf(infoEvent, EncodingInfoEvent);
assertInstanceOf(progressEvent, EncodingProgressEvent);
assertInstanceOf(endEvent, EncodingEndEvent);
assertEquals(progressEvent?.progress, 100);
},
});