-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhardware-encoding.ts
More file actions
231 lines (195 loc) · 6.48 KB
/
hardware-encoding.ts
File metadata and controls
231 lines (195 loc) · 6.48 KB
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
/**
* Hardware Accelerated Video Encoding Example
*
* Demonstrates GPU-accelerated encoding with VAAPI, NVENC, or QSV.
*
* Run: npx tsx examples/hardware-encoding.ts
*/
import { VideoEncoder, VideoFrame, EncodedVideoChunk } from '../src/index.js';
import {
detectHardwareAcceleration,
getHardwareAccelerationSummary,
getBestEncoder,
} from '../src/index.js';
interface BenchmarkResult {
mode: string;
encoder: string;
encodeTime: number;
totalBytes: number;
fps: number;
}
async function benchmark(
width: number,
height: number,
frameCount: number,
framerate: number,
hardwareAcceleration: 'no-preference' | 'prefer-hardware' | 'prefer-software'
): Promise<BenchmarkResult> {
const chunks: EncodedVideoChunk[] = [];
const encoder = new VideoEncoder({
output: (chunk) => chunks.push(chunk),
error: console.error,
});
encoder.configure({
codec: 'avc1.42001E',
width,
height,
bitrate: 5_000_000,
framerate,
hardwareAcceleration,
});
// Pre-generate all frame data to exclude from timing
const frames: Uint8Array[] = [];
for (let i = 0; i < frameCount; i++) {
const rgba = new Uint8Array(width * height * 4);
// Create complex pattern for realistic encoding load
for (let y = 0; y < height; y++) {
for (let x = 0; x < width; x++) {
const idx = (y * width + x) * 4;
// Animated noise pattern
rgba[idx] = (x * y + i * 17) % 256;
rgba[idx + 1] = (x + y * 2 + i * 23) % 256;
rgba[idx + 2] = ((x - y) * 3 + i * 31) % 256;
rgba[idx + 3] = 255;
}
}
frames.push(rgba);
}
// Time the encoding
const startTime = Date.now();
for (let i = 0; i < frameCount; i++) {
const frame = new VideoFrame(frames[i], {
format: 'RGBA',
codedWidth: width,
codedHeight: height,
timestamp: (i * 1_000_000) / framerate,
});
encoder.encode(frame, { keyFrame: i % framerate === 0 });
frame.close();
}
await encoder.flush();
encoder.close();
const encodeTime = Date.now() - startTime;
const totalBytes = chunks.reduce((sum, c) => sum + c.byteLength, 0);
return {
mode: hardwareAcceleration,
encoder: hardwareAcceleration === 'prefer-hardware' ? 'GPU (if available)' : 'CPU',
encodeTime,
totalBytes,
fps: (frameCount * 1000) / encodeTime,
};
}
async function main() {
console.log('=== Hardware Accelerated Video Encoding ===\n');
// Detect available hardware acceleration
console.log('Detecting hardware acceleration...\n');
const summary = await getHardwareAccelerationSummary();
console.log(summary);
console.log('');
// Get detailed capabilities
const capabilities = await detectHardwareAcceleration();
if (capabilities.methods.length > 0) {
console.log('Available acceleration methods:');
for (const method of capabilities.methods) {
console.log(` - ${method}`);
}
console.log('');
}
if (capabilities.encoders.length > 0) {
console.log('Hardware encoders:');
for (const enc of capabilities.encoders) {
console.log(` - ${enc}`);
}
console.log('');
}
// Get best encoder for H.264
console.log('Finding best H.264 encoder...');
const bestH264 = await getBestEncoder('h264', 'prefer-hardware');
console.log(` Best encoder: ${bestH264.encoder}`);
console.log(` Hardware: ${bestH264.isHardware}`);
console.log('');
// Benchmark parameters
const width = 1280;
const height = 720;
const frameCount = 60;
const framerate = 30;
console.log(`\nBenchmarking ${width}x${height} @ ${framerate}fps (${frameCount} frames)...\n`);
// Benchmark software encoding
console.log('Testing software encoding...');
const softwareResult = await benchmark(
width,
height,
frameCount,
framerate,
'prefer-software'
);
// Benchmark hardware encoding
console.log('Testing hardware encoding...');
const hardwareResult = await benchmark(
width,
height,
frameCount,
framerate,
'prefer-hardware'
);
// Display results
console.log('\n=== Benchmark Results ===\n');
console.log('Software Encoding (CPU):');
console.log(` Encode time: ${softwareResult.encodeTime}ms`);
console.log(` Throughput: ${softwareResult.fps.toFixed(1)} fps`);
console.log(` Output size: ${(softwareResult.totalBytes / 1024).toFixed(2)} KB`);
console.log(
` Bitrate: ${((softwareResult.totalBytes * 8 * framerate) / frameCount / 1000).toFixed(0)} kbps`
);
console.log('\nHardware Encoding (GPU):');
console.log(` Encode time: ${hardwareResult.encodeTime}ms`);
console.log(` Throughput: ${hardwareResult.fps.toFixed(1)} fps`);
console.log(` Output size: ${(hardwareResult.totalBytes / 1024).toFixed(2)} KB`);
console.log(
` Bitrate: ${((hardwareResult.totalBytes * 8 * framerate) / frameCount / 1000).toFixed(0)} kbps`
);
// Comparison
console.log('\n=== Comparison ===\n');
const speedup = softwareResult.encodeTime / hardwareResult.encodeTime;
const sizeRatio = hardwareResult.totalBytes / softwareResult.totalBytes;
if (speedup > 1.1) {
console.log(`Hardware encoding is ${speedup.toFixed(2)}x faster`);
} else if (speedup < 0.9) {
console.log(`Software encoding is ${(1 / speedup).toFixed(2)}x faster`);
console.log('(Hardware acceleration may not be available or configured)');
} else {
console.log('Performance is similar (hardware may not be available)');
}
console.log(
`File size difference: ${((sizeRatio - 1) * 100).toFixed(1)}% ` +
`(${sizeRatio > 1 ? 'larger' : 'smaller'} with hardware)`
);
// Usage recommendations
console.log('\n=== When to Use Hardware Acceleration ===\n');
console.log('Use prefer-hardware when:');
console.log(' - Encoding HD/4K video');
console.log(' - Real-time streaming');
console.log(' - Batch processing many videos');
console.log(' - CPU resources are constrained');
console.log('\nUse prefer-software when:');
console.log(' - Maximum compression quality needed');
console.log(' - Archival purposes');
console.log(' - No compatible GPU available');
console.log(' - Encoding small/low-res content');
// Example configuration
console.log('\n=== Example Configuration ===\n');
console.log(`const encoder = new VideoEncoder({
output: handleChunk,
error: handleError,
});
encoder.configure({
codec: 'avc1.42001E',
width: 1920,
height: 1080,
bitrate: 5_000_000,
framerate: 30,
hardwareAcceleration: 'prefer-hardware',
latencyMode: 'realtime', // Combine with low-latency for streaming
});`);
}
main().catch(console.error);