Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Latest commit

 

History

History
History
257 lines (211 loc) · 7.49 KB

File metadata and controls

257 lines (211 loc) · 7.49 KB
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
/**
* High-Level API Example: Frame Extraction and Processing
*
* Shows how to extract frames from video and save them as images.
* Demonstrates frame manipulation capabilities of the high-level API.
*
* Usage: tsx examples/api-frame-extract.ts <input> <output>
* Example: tsx examples/api-frame-extract.ts testdata/demux.mp4 examples/.tmp
*/
import { mkdir, writeFile } from 'fs/promises';
import {
AV_LOG_DEBUG,
AV_PIX_FMT_RGB24,
AV_PIX_FMT_RGB8,
Decoder,
Demuxer,
Encoder,
FF_ENCODER_GIF,
FF_ENCODER_MJPEG,
FF_ENCODER_PNG,
FilterAPI,
FilterPreset,
Log,
Muxer,
} from '../src/index.js';
import { prepareTestEnvironment } from './index.js';
const inputFile = process.argv[2];
const outputDir = process.argv[3];
if (!inputFile || !outputDir) {
console.error('Usage: tsx examples/api-frame-extract.ts <input> <output>');
process.exit(1);
}
async function extractFrameAsPNG(frameNumber: number) {
await using input = await Demuxer.open(inputFile);
const videoStream = input.video(0);
if (!videoStream) {
throw new Error('No video stream found');
}
// Create decoder
using decoder = await Decoder.create(videoStream);
// Create filter to convert to RGB24
const filterChain = FilterPreset.chain().format(AV_PIX_FMT_RGB24).build();
using filter = FilterAPI.create(filterChain, {
framerate: videoStream.avgFrameRate,
});
// Create PNG encoder
using pngEncoder = await Encoder.create(FF_ENCODER_PNG, {
decoder,
filter,
});
let currentFrame = 0;
for await (using packet of input.packets(videoStream.index)) {
// Decode packet to frames
for await (using frame of decoder.frames(packet)) {
// Filter frame
for await (using filteredFrame of filter.frames(frame)) {
if (filteredFrame) {
if (currentFrame === frameNumber) {
console.log(`Frame ${frameNumber}: ${filteredFrame.width}x${filteredFrame.height}, PTS: ${filteredFrame.pts}`);
// Encode frame as PNG
for await (using pngPacket of pngEncoder.packets(filteredFrame)) {
if (pngPacket?.data) {
const filename = `${outputDir}/frame_${frameNumber}.png`;
await writeFile(filename, pngPacket.data);
console.log(`Saved to ${filename}`);
}
}
// Flush encoder for single frame
for await (using flushPacket of pngEncoder.packets(null)) {
if (flushPacket?.data) {
const filename = `${outputDir}/frame_${frameNumber}_flush.png`;
await writeFile(filename, flushPacket.data);
console.log(`Saved to ${filename}`);
}
}
return; // Exit function after extracting target frame
}
currentFrame++;
}
}
}
}
}
async function extractFramesAtInterval(intervalSeconds: number, count: number) {
await using input = await Demuxer.open(inputFile);
const videoStream = input.video(0);
if (!videoStream) {
throw new Error('No video stream found');
}
// Get frame rate from the stream
const fps = videoStream.avgFrameRate.toDouble();
const frameInterval = Math.floor(fps * intervalSeconds);
console.log(`Video FPS: ~${fps.toFixed(1)}`);
console.log(`Frame interval: ${frameInterval} frames`);
// Create decoder
using decoder = await Decoder.create(videoStream);
// Create JPEG encoder for thumbnails
using jpegEncoder = await Encoder.create(FF_ENCODER_MJPEG, {
decoder,
bitrate: '2M',
options: { strict: 'experimental' },
});
let currentFrame = 0;
let extractedCount = 0;
for await (using packet of input.packets(videoStream.index)) {
// Decode packet to frames
for await (using frame of decoder.frames(packet)) {
if (frame) {
if (currentFrame % frameInterval === 0 && extractedCount < count) {
console.log(`Extracting frame ${currentFrame} (${(currentFrame / fps).toFixed(1)}s)`);
// Encode frame as JPEG
for await (using jpegPacket of jpegEncoder.packets(frame)) {
if (jpegPacket?.data) {
const filename = `${outputDir}/thumb_${extractedCount}.jpg`;
await writeFile(filename, jpegPacket.data);
console.log(`Saved: ${filename}`);
}
}
extractedCount++;
}
currentFrame++;
if (extractedCount >= count) {
return; // Exit function after extracting all thumbnails
}
}
}
}
console.log(`Extracted ${extractedCount} thumbnails`);
}
async function generateGIF(startTime: number, duration: number) {
await using input = await Demuxer.open(inputFile);
await using output = await Muxer.open(`${outputDir}/output.gif`);
const videoStream = input.video(0);
if (!videoStream) {
throw new Error('No video stream found');
}
// Get frame rate from the stream
const fps = videoStream.avgFrameRate.toDouble();
const startFrame = Math.floor(startTime * fps);
const endFrame = Math.floor((startTime + duration) * fps);
console.log(`Processing frames ${startFrame} to ${endFrame}`);
// Create decoder
using decoder = await Decoder.create(videoStream);
// Create filter to convert to RGB24 (GIF requires this)
const filterChain = FilterPreset.chain().format(AV_PIX_FMT_RGB8).build();
using filter = FilterAPI.create(filterChain, {
framerate: videoStream.avgFrameRate,
});
// Create encoder
using encoder = await Encoder.create(FF_ENCODER_GIF, {
decoder,
filter,
options: {
// GIF specific options can be set here
// e.g., loop count, palette size, etc.
// For simplicity, we use defaults
},
});
const outputStreamIndex = output.addStream(encoder);
let currentFrame = 0;
for await (using packet of input.packets(videoStream.index)) {
// Process both null (EOF) and video packets
if (packet === null || packet.streamIndex === videoStream.index) {
for await (using frame of decoder.frames(packet)) {
if (frame === null) break; // EOF
// Skip frames before start
if (currentFrame < startFrame) {
currentFrame++;
continue;
}
// Stop after end frame
if (currentFrame >= endFrame) {
// Flush remaining frames in pipeline
for await (using filteredFrame of filter.frames(null)) {
if (filteredFrame === null) break;
for await (using encodedPacket of encoder.packets(filteredFrame)) {
if (encodedPacket === null) break;
await output.writePacket(encodedPacket, outputStreamIndex);
}
}
return;
}
// Process frame
for await (using filteredFrame of filter.frames(frame)) {
if (filteredFrame === null) break; // EOF
for await (using encodedPacket of encoder.packets(filteredFrame)) {
if (encodedPacket === null) break; // EOF
await output.writePacket(encodedPacket, outputStreamIndex);
}
}
currentFrame++;
}
}
}
}
prepareTestEnvironment();
Log.setLevel(AV_LOG_DEBUG);
console.log('Input:', inputFile);
console.log('Output Directory:', outputDir);
// Create output directory
await mkdir(outputDir, { recursive: true });
// Extract specific frame
console.log('Extracting specific frame:');
await extractFrameAsPNG(10);
// Extract thumbnails
console.log('Extracting thumbnails:');
await extractFramesAtInterval(2, 5);
// Generate GIF (demo)
console.log('Generating GIF:');
await generateGIF(0, 3);
console.log('Done!');
Morty Proxy This is a proxified and sanitized view of the page, visit original site.