Programmatic Usage
Learn how to use the Zapdos SDK programmatically in your JavaScript applications
You can use the Zapdos SDK programmatically in your JavaScript code.
Basic Usage
import ZapdosClient from 'zapdos-js';
// Create a client
const client = new ZapdosClient();
// Index a video file
async function indexVideo() {
try {
for await (const event of client.index("path/to/your/video.mp4")) {
if (event.type === 'done') {
console.log('Indexing completed:', event.value);
}
}
} catch (error) {
console.error('Indexing failed:', error);
}
}
indexVideo();
Handling Indexing Events
The JavaScript SDK uses an async generator pattern to emit events during the indexing process. Some events include a value
field containing relevant data:
import ZapdosClient from 'zapdos-js';
import { IndexingEvent } from 'zapdos-js/dist/utils.js';
// Indexing event structure:
// {
// type: IndexingEvent, // The type of event
// message?: string, // Error message (for ERROR events)
// value?: any // Event-specific data (for certain events)
// }
// Events that include a value field:
// - IndexingEvent.END_STT: Speech-to-text processing results
// - IndexingEvent.END_OBJECT_DETECTION: Object detection results
// - IndexingEvent.END_IMAGE_DESCRIPTION: Image description results
// - IndexingEvent.END_SUMMARIZATION: Summary results
// - IndexingEvent.DONE: Final indexing result
// Create a client
const client = new ZapdosClient();
// Index a video file with progress updates
async function indexVideoWithProgress() {
try {
for await (const event of client.index("path/to/your/video.mp4")) {
switch (event.type) {
case IndexingEvent.START_EXTRACTING_AUDIO:
console.log('Starting audio extraction...');
break;
case IndexingEvent.END_EXTRACTING_AUDIO:
console.log('Audio extraction completed');
break;
case IndexingEvent.START_EXTRACTING_FRAMES:
console.log('Starting frame extraction...');
break;
case IndexingEvent.END_EXTRACTING_FRAMES:
console.log('Frame extraction completed');
if (event.value) {
console.log('Extracted frames:', event.value);
}
break;
case IndexingEvent.START_STT:
console.log('Starting speech-to-text processing...');
break;
case IndexingEvent.END_STT:
console.log('Speech-to-text processing completed');
if (event.value) {
console.log('Transcription result:', event.value);
}
break;
case IndexingEvent.START_OBJECT_DETECTION:
console.log('Starting object detection...');
break;
case IndexingEvent.END_OBJECT_DETECTION:
console.log('Object detection completed');
if (event.value) {
console.log('Object detection results:', event.value);
}
break;
case IndexingEvent.START_IMAGE_DESCRIPTION:
console.log('Starting image description generation...');
break;
case IndexingEvent.END_IMAGE_DESCRIPTION:
console.log('Image description generation completed');
if (event.value) {
console.log('Image descriptions:', event.value);
}
break;
case IndexingEvent.START_SUMMARIZATION:
console.log('Starting summarization...');
break;
case IndexingEvent.END_SUMMARIZATION:
console.log('Summarization completed');
if (event.value) {
console.log('Summary results:', event.value);
}
break;
case IndexingEvent.DONE:
console.log('Indexing completed successfully!');
console.log('Final result:', event.value);
break;
case IndexingEvent.ERROR:
console.error('Error during indexing:', event.message);
break;
}
}
} catch (error) {
console.error('Indexing failed:', error);
}
}
indexVideoWithProgress();
Indexing Options
You can pass options to customize the indexing process:
import ZapdosClient from 'zapdos-js';
const client = new ZapdosClient();
// Index with custom options
async function indexVideoWithOptions() {
try {
// Set custom interval (in seconds) between extracted frames
const options = {
interval: 10, // Extract a frame every 10 seconds
compact: true // Return compact results
};
for await (const event of client.index("path/to/your/video.mp4", options)) {
if (event.type === 'done') {
console.log('Indexing completed:', event.value);
}
}
} catch (error) {
console.error('Indexing failed:', error);
}
}
indexVideoWithOptions();