vidnavigator 2.4.0 → 2.6.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +185 -40
- package/dist/errors.d.ts +13 -0
- package/dist/errors.js +17 -4
- package/dist/http.js +5 -0
- package/dist/index.d.ts +169 -11
- package/dist/index.js +122 -17
- package/dist/models/AsyncJob.d.ts +18 -2
- package/dist/models/AsyncJob.js +4 -0
- package/dist/models/Extraction.d.ts +29 -6
- package/dist/models/FileInfo.d.ts +15 -0
- package/dist/models/FileInfo.js +3 -0
- package/dist/models/Watch.d.ts +7 -2
- package/dist/models/Watch.js +1 -1
- package/dist/webhooks.d.ts +5 -3
- package/package.json +1 -1
- package/src/errors.ts +29 -0
- package/src/http.ts +14 -0
- package/src/index.ts +293 -51
- package/src/models/AsyncJob.ts +22 -2
- package/src/models/Extraction.ts +68 -52
- package/src/models/FileInfo.ts +19 -0
- package/src/models/Watch.ts +8 -2
- package/src/webhooks.ts +6 -2
package/src/index.ts
CHANGED
|
@@ -50,6 +50,11 @@ import {
|
|
|
50
50
|
TweetStatement,
|
|
51
51
|
WatchResult,
|
|
52
52
|
WatchResultJSON,
|
|
53
|
+
WatchTranscriptError,
|
|
54
|
+
VideoScene,
|
|
55
|
+
VideoSceneJSON,
|
|
56
|
+
ScenesError,
|
|
57
|
+
FileScenesStatus,
|
|
53
58
|
AsyncJob,
|
|
54
59
|
AsyncJobJSON,
|
|
55
60
|
AsyncJobAccepted,
|
|
@@ -67,7 +72,7 @@ export { Job } from './jobs';
|
|
|
67
72
|
export type { TaskStatus, PollableTask, PollOptions, JobOperation, SubmitOptions, RunOptions, JobInfo } from './jobs';
|
|
68
73
|
|
|
69
74
|
/** SDK version (keep in sync with package.json) */
|
|
70
|
-
export const SDK_VERSION = '2.
|
|
75
|
+
export const SDK_VERSION = '2.6.0';
|
|
71
76
|
|
|
72
77
|
/**
|
|
73
78
|
* A LIVE room usually answers in 1-5 s, up to ~30 s when the API has to open a TikTok page, and two
|
|
@@ -140,14 +145,39 @@ export type WatchInput = {
|
|
|
140
145
|
/** Payload of {@link VidNavigatorClient.watchVideo}: a {@link WatchInput} with `include_usage`. */
|
|
141
146
|
export type WatchVideoPayload = WatchInput & { include_usage?: boolean };
|
|
142
147
|
|
|
143
|
-
/** Input for {@link VidNavigatorClient.
|
|
148
|
+
/** Input for {@link VidNavigatorClient.analyze}. A plain string is treated as `video_url`. */
|
|
149
|
+
export type AnalyzeVideoInput = {
|
|
150
|
+
video_url: string;
|
|
151
|
+
/** Optional question about the video. */
|
|
152
|
+
query?: string;
|
|
153
|
+
/**
|
|
154
|
+
* Default false. `true` also reads what the video shows: the analysis is made from the
|
|
155
|
+
* transcript and the scenes together, and the result has `scenes` and `frames_analyzed`.
|
|
156
|
+
*/
|
|
157
|
+
include_scenes?: boolean;
|
|
158
|
+
/** Return the transcript as a single string instead of segments. */
|
|
159
|
+
transcript_text?: boolean;
|
|
160
|
+
} & WebhookOption;
|
|
161
|
+
|
|
162
|
+
/**
|
|
163
|
+
* Input for {@link VidNavigatorClient.extractVideo}: a JSON Schema `schema`, or a JSON/YAML
|
|
164
|
+
* `schemaFilePath`, or neither with `what_to_extract`: the schema is then written from that text
|
|
165
|
+
* and comes back as `schema` on the result.
|
|
166
|
+
*/
|
|
144
167
|
export type ExtractVideoInput =
|
|
145
168
|
| ({
|
|
146
169
|
video_url: string;
|
|
147
|
-
|
|
170
|
+
/** The fields to extract, as a standard JSON Schema. Optional when `what_to_extract` is given. */
|
|
171
|
+
schema?: ExtractionSchema;
|
|
172
|
+
/** What to extract, in plain words. Guides the extraction, or alone, writes the schema. */
|
|
148
173
|
what_to_extract?: string;
|
|
149
174
|
/** Speech-to-text the audio when no platform transcript exists. Default true. */
|
|
150
175
|
transcribe?: boolean;
|
|
176
|
+
/**
|
|
177
|
+
* Default false. `true` also reads what the video shows: the fields are extracted from the
|
|
178
|
+
* transcript and the scenes together, and the result has `frames_analyzed`.
|
|
179
|
+
*/
|
|
180
|
+
include_scenes?: boolean;
|
|
151
181
|
} & WebhookOption)
|
|
152
182
|
| ({
|
|
153
183
|
video_url: string;
|
|
@@ -155,6 +185,7 @@ export type ExtractVideoInput =
|
|
|
155
185
|
schemaFilePath: string;
|
|
156
186
|
what_to_extract?: string;
|
|
157
187
|
transcribe?: boolean;
|
|
188
|
+
include_scenes?: boolean;
|
|
158
189
|
} & WebhookOption);
|
|
159
190
|
|
|
160
191
|
/** Input for {@link VidNavigatorClient.tweetStatement}. A plain string is treated as `tweet_id`. */
|
|
@@ -186,6 +217,9 @@ export type TranscribeAsyncJob = AsyncJob<TranscribeVideoResult>;
|
|
|
186
217
|
/** Snapshot of a watch task. `result` is a {@link WatchResult}. */
|
|
187
218
|
export type WatchAsyncJob = AsyncJob<WatchResult>;
|
|
188
219
|
|
|
220
|
+
/** Snapshot of a video analysis task. `result` has the same shape as {@link VidNavigatorClient.analyzeVideo}'s return value. */
|
|
221
|
+
export type AnalyzeVideoAsyncJob = AsyncJob<AnalyzeVideoResult>;
|
|
222
|
+
|
|
189
223
|
/** Snapshot of an extraction task. `result` is the extracted object, shaped like your schema. */
|
|
190
224
|
export type ExtractVideoAsyncJob = AsyncJob<Record<string, unknown>>;
|
|
191
225
|
|
|
@@ -208,6 +242,7 @@ export interface TranscribeOperation extends JobOperation<string | TranscribeInp
|
|
|
208
242
|
submit(input: string | TranscribeInput, options?: SubmitOptions): Promise<Job<TranscribeAsyncJob, TranscribeVideoResult>>;
|
|
209
243
|
}
|
|
210
244
|
export type WatchJob = Job<WatchAsyncJob, WatchResult>;
|
|
245
|
+
export type AnalyzeVideoJob = Job<AnalyzeVideoAsyncJob, AnalyzeVideoResult>;
|
|
211
246
|
export type ExtractVideoJob = Job<ExtractVideoAsyncJob, ExtractDataResult>;
|
|
212
247
|
export type TweetStatementJob = Job<TweetStatementAsyncJob, TweetStatement>;
|
|
213
248
|
/** `result()` resolves to the completed task with **all** pages of `videos` collected. */
|
|
@@ -238,10 +273,35 @@ export type UploadFileAcceptedResult = {
|
|
|
238
273
|
|
|
239
274
|
export type UploadFileResult = UploadFileSuccessResult | UploadFileAcceptedResult;
|
|
240
275
|
|
|
276
|
+
/** Answer of {@link VidNavigatorClient.analyzeFileScenes}. */
|
|
277
|
+
export type AnalyzeFileScenesResult = {
|
|
278
|
+
file_id: string;
|
|
279
|
+
/** `processing`: the analysis was started, or is already running. `completed`: the scenes are already there. */
|
|
280
|
+
scenes_status: Extract<FileScenesStatus, 'processing' | 'completed'>;
|
|
281
|
+
message: string;
|
|
282
|
+
};
|
|
283
|
+
|
|
284
|
+
/** Answer of {@link VidNavigatorClient.getFile}. */
|
|
285
|
+
export type GetFileResult = {
|
|
286
|
+
file_info: FileInfo;
|
|
287
|
+
/** Present once the file has a transcript. */
|
|
288
|
+
transcript?: TranscriptOutput;
|
|
289
|
+
/** What the file shows, in chronological order. Present once `file_info.scenes_status` is `completed`. */
|
|
290
|
+
scenes?: VideoScene[];
|
|
291
|
+
};
|
|
292
|
+
|
|
241
293
|
export interface ExtractDataResult {
|
|
242
294
|
data: Record<string, unknown>;
|
|
295
|
+
/** When the request had no schema: the schema written from `what_to_extract`, to pass as `schema` next time. */
|
|
296
|
+
schema?: ExtractionSchema;
|
|
243
297
|
video_info?: VideoInfo;
|
|
244
298
|
file_info?: FileInfo;
|
|
299
|
+
/** With `include_scenes: true`: number of frames that were analyzed to produce the scenes. */
|
|
300
|
+
frames_analyzed?: number;
|
|
301
|
+
/** With `include_scenes: true`, when the picture could not be read: the fields were extracted from the transcript alone. */
|
|
302
|
+
scenes_error?: ScenesError;
|
|
303
|
+
/** With `include_scenes: true`, when there is no transcript: the fields were extracted from the scenes alone. */
|
|
304
|
+
transcript_error?: WatchTranscriptError;
|
|
245
305
|
usage?: UsageBlock;
|
|
246
306
|
}
|
|
247
307
|
|
|
@@ -296,6 +356,11 @@ export type SearchFilesResult = {
|
|
|
296
356
|
export type AnalyzeVideoPayload = {
|
|
297
357
|
video_url: string;
|
|
298
358
|
query?: string;
|
|
359
|
+
/**
|
|
360
|
+
* Default false. `true` also reads what the video shows: the analysis is made from the
|
|
361
|
+
* transcript and the scenes together, and the result has `scenes` and `frames_analyzed`.
|
|
362
|
+
*/
|
|
363
|
+
include_scenes?: boolean;
|
|
299
364
|
transcript_text?: boolean;
|
|
300
365
|
include_usage?: boolean;
|
|
301
366
|
};
|
|
@@ -303,6 +368,13 @@ export type AnalyzeVideoPayload = {
|
|
|
303
368
|
export type AnalyzeFilePayload = {
|
|
304
369
|
file_id: string;
|
|
305
370
|
query?: string;
|
|
371
|
+
/**
|
|
372
|
+
* Default false. `true` analyzes the file from its transcript and its scenes together. The
|
|
373
|
+
* scenes are the ones kept with the file: ask for them at the upload (`include_scenes`) or with
|
|
374
|
+
* {@link VidNavigatorClient.analyzeFileScenes}. A file that has none is refused with
|
|
375
|
+
* `ScenesNotAvailableError`.
|
|
376
|
+
*/
|
|
377
|
+
include_scenes?: boolean;
|
|
306
378
|
transcript_text?: boolean;
|
|
307
379
|
include_usage?: boolean;
|
|
308
380
|
};
|
|
@@ -311,6 +383,17 @@ export type AnalyzeVideoResult = {
|
|
|
311
383
|
video_info: VideoInfo;
|
|
312
384
|
transcript: TranscriptOutput;
|
|
313
385
|
transcript_analysis: AnalysisResult;
|
|
386
|
+
/**
|
|
387
|
+
* With `include_scenes: true`: what the video shows, in chronological order. Empty when the
|
|
388
|
+
* picture could not be read (see `scenes_error`).
|
|
389
|
+
*/
|
|
390
|
+
scenes?: VideoScene[];
|
|
391
|
+
/** With `include_scenes: true`: number of frames that were analyzed to produce the scenes. */
|
|
392
|
+
frames_analyzed?: number;
|
|
393
|
+
/** With `include_scenes: true`, when the picture could not be read: the analysis was made from the transcript alone. */
|
|
394
|
+
scenes_error?: ScenesError;
|
|
395
|
+
/** With `include_scenes: true`, when there is no transcript: the analysis was made from the scenes alone. */
|
|
396
|
+
transcript_error?: WatchTranscriptError;
|
|
314
397
|
usage?: UsageBlock;
|
|
315
398
|
};
|
|
316
399
|
|
|
@@ -318,14 +401,28 @@ export type AnalyzeFileResult = {
|
|
|
318
401
|
file_info: FileInfo;
|
|
319
402
|
transcript: TranscriptOutput;
|
|
320
403
|
transcript_analysis: AnalysisResult;
|
|
404
|
+
/** With `include_scenes: true`: what the file shows, in chronological order. */
|
|
405
|
+
scenes?: VideoScene[];
|
|
406
|
+
/** With `include_scenes: true`: number of frames that were analyzed to produce the scenes. */
|
|
407
|
+
frames_analyzed?: number;
|
|
408
|
+
/** Why the scenes could not be read, when the answer carries it. A file that has no scenes at all is refused with `ScenesNotAvailableError` instead. */
|
|
409
|
+
scenes_error?: ScenesError;
|
|
410
|
+
/** With `include_scenes: true`, when the file has no speech: the analysis was made from the scenes alone. */
|
|
411
|
+
transcript_error?: WatchTranscriptError;
|
|
321
412
|
usage?: UsageBlock;
|
|
322
413
|
};
|
|
323
414
|
|
|
324
415
|
export type ExtractVideoDataPayload = {
|
|
325
416
|
video_url: string;
|
|
326
|
-
|
|
417
|
+
/** A standard JSON Schema. Optional when `what_to_extract` is given. */
|
|
418
|
+
schema?: ExtractionSchema;
|
|
327
419
|
what_to_extract?: string;
|
|
328
420
|
transcribe?: boolean;
|
|
421
|
+
/**
|
|
422
|
+
* Default false. `true` also reads what the video shows: the fields are extracted from the
|
|
423
|
+
* transcript and the scenes together, and the result has `frames_analyzed`.
|
|
424
|
+
*/
|
|
425
|
+
include_scenes?: boolean;
|
|
329
426
|
include_usage?: boolean;
|
|
330
427
|
} & WebhookOption;
|
|
331
428
|
|
|
@@ -335,6 +432,7 @@ export type ExtractVideoDataMultipartPayload = {
|
|
|
335
432
|
schemaFilePath: string;
|
|
336
433
|
what_to_extract?: string;
|
|
337
434
|
transcribe?: boolean;
|
|
435
|
+
include_scenes?: boolean;
|
|
338
436
|
include_usage?: boolean;
|
|
339
437
|
} & WebhookOption;
|
|
340
438
|
|
|
@@ -342,8 +440,16 @@ export type TweetStatementPayload = { tweet_id: string } & WebhookOption;
|
|
|
342
440
|
|
|
343
441
|
export type ExtractFileDataPayload = {
|
|
344
442
|
file_id: string;
|
|
345
|
-
|
|
443
|
+
/** A standard JSON Schema. Optional when `what_to_extract` is given. */
|
|
444
|
+
schema?: ExtractionSchema;
|
|
346
445
|
what_to_extract?: string;
|
|
446
|
+
/**
|
|
447
|
+
* Default false. `true` extracts from the transcript of the file and its scenes together. The
|
|
448
|
+
* scenes are the ones kept with the file: ask for them at the upload (`include_scenes`) or with
|
|
449
|
+
* {@link VidNavigatorClient.analyzeFileScenes}. A file that has none is refused with
|
|
450
|
+
* `ScenesNotAvailableError`.
|
|
451
|
+
*/
|
|
452
|
+
include_scenes?: boolean;
|
|
347
453
|
include_usage?: boolean;
|
|
348
454
|
};
|
|
349
455
|
|
|
@@ -352,6 +458,7 @@ export type ExtractFileDataMultipartPayload = {
|
|
|
352
458
|
/** Path to a JSON or YAML schema file. The API accepts either a raw schema or a full extraction request object. */
|
|
353
459
|
schemaFilePath: string;
|
|
354
460
|
what_to_extract?: string;
|
|
461
|
+
include_scenes?: boolean;
|
|
355
462
|
include_usage?: boolean;
|
|
356
463
|
};
|
|
357
464
|
//endregion
|
|
@@ -377,6 +484,63 @@ function parseTranscribeData(
|
|
|
377
484
|
};
|
|
378
485
|
}
|
|
379
486
|
|
|
487
|
+
/**
|
|
488
|
+
* An extraction needs a schema (`schema` or `schemaFilePath`) or, without one, `what_to_extract`,
|
|
489
|
+
* from which the API writes the schema. Checked before anything is sent.
|
|
490
|
+
*/
|
|
491
|
+
function checkSchemaInput(input: { schema?: unknown; schemaFilePath?: string; what_to_extract?: string }): void {
|
|
492
|
+
if (input.schema !== undefined && input.schemaFilePath !== undefined) {
|
|
493
|
+
throw new Error('Pass at most one of schema or schemaFilePath.');
|
|
494
|
+
}
|
|
495
|
+
if (input.schema === undefined && input.schemaFilePath === undefined && !input.what_to_extract?.trim()) {
|
|
496
|
+
throw new Error('Pass schema, schemaFilePath or what_to_extract.');
|
|
497
|
+
}
|
|
498
|
+
}
|
|
499
|
+
|
|
500
|
+
/** What `include_scenes` adds beside the scenes themselves. */
|
|
501
|
+
interface SceneNotes {
|
|
502
|
+
frames_analyzed?: number;
|
|
503
|
+
scenes_error?: ScenesError;
|
|
504
|
+
transcript_error?: WatchTranscriptError;
|
|
505
|
+
}
|
|
506
|
+
|
|
507
|
+
/** The `data` block of an analysis: the same for a video, its background job and an uploaded file. */
|
|
508
|
+
interface AnalysisDataJSON extends SceneNotes {
|
|
509
|
+
transcript: TranscriptSegmentJSON[] | string;
|
|
510
|
+
transcript_analysis: AnalysisResultJSON;
|
|
511
|
+
scenes?: VideoSceneJSON[];
|
|
512
|
+
}
|
|
513
|
+
|
|
514
|
+
// The scene fields are set only when the response carries them, so a result asked without
|
|
515
|
+
// `include_scenes` keeps the shape it always had.
|
|
516
|
+
function pickSceneNotes(raw: SceneNotes): SceneNotes {
|
|
517
|
+
const notes: SceneNotes = {};
|
|
518
|
+
if (raw.frames_analyzed != null) notes.frames_analyzed = raw.frames_analyzed;
|
|
519
|
+
if (raw.scenes_error) notes.scenes_error = raw.scenes_error;
|
|
520
|
+
if (raw.transcript_error) notes.transcript_error = raw.transcript_error;
|
|
521
|
+
return notes;
|
|
522
|
+
}
|
|
523
|
+
|
|
524
|
+
function parseScenes(raw: VideoSceneJSON[] | undefined): { scenes?: VideoScene[] } {
|
|
525
|
+
return raw ? { scenes: raw.map((scene) => VideoScene.fromJSON(scene)) } : {};
|
|
526
|
+
}
|
|
527
|
+
|
|
528
|
+
function parseAnalysisData(raw: AnalysisDataJSON) {
|
|
529
|
+
return {
|
|
530
|
+
transcript: transcriptFromJSON(raw.transcript)!,
|
|
531
|
+
transcript_analysis: AnalysisResult.fromJSON(raw.transcript_analysis),
|
|
532
|
+
...parseScenes(raw.scenes),
|
|
533
|
+
...pickSceneNotes(raw),
|
|
534
|
+
};
|
|
535
|
+
}
|
|
536
|
+
|
|
537
|
+
function parseAnalyzeVideoData(
|
|
538
|
+
raw: AnalysisDataJSON & { video_info: VideoInfoJSON },
|
|
539
|
+
usage?: UsageBlock
|
|
540
|
+
): AnalyzeVideoResult {
|
|
541
|
+
return { video_info: VideoInfo.fromJSON(raw.video_info), ...parseAnalysisData(raw), usage };
|
|
542
|
+
}
|
|
543
|
+
|
|
380
544
|
function buildAsyncJobParams(options?: SubmitOptions): Record<string, string> | undefined {
|
|
381
545
|
// Like the TikTok pollers, the async job pollers take include_usage as a query string value.
|
|
382
546
|
return options?.include_usage ? { include_usage: 'true' } : undefined;
|
|
@@ -478,9 +642,19 @@ export class VidNavigatorClient {
|
|
|
478
642
|
*/
|
|
479
643
|
readonly watch: JobOperation<string | WatchInput, WatchAsyncJob, WatchResult>;
|
|
480
644
|
|
|
645
|
+
/**
|
|
646
|
+
* Analysis of an online video as a background job, built on `POST /analyze/video/async`: the
|
|
647
|
+
* same analysis and the same result as {@link VidNavigatorClient.analyzeVideo}, without holding
|
|
648
|
+
* a connection open while it runs. Use it with `include_scenes: true` on a video longer than
|
|
649
|
+
* about 10 minutes. Same shapes as {@link VidNavigatorClient.transcribe}.
|
|
650
|
+
*/
|
|
651
|
+
readonly analyze: JobOperation<string | AnalyzeVideoInput, AnalyzeVideoAsyncJob, AnalyzeVideoResult>;
|
|
652
|
+
|
|
481
653
|
/**
|
|
482
654
|
* Structured-data extraction from a video of any length, built on `POST /extract/video/async`.
|
|
483
|
-
* The schema is validated when you submit.
|
|
655
|
+
* The schema is validated when you submit. With `include_scenes: true` the fields are extracted
|
|
656
|
+
* from the transcript and from what the video shows. Same shapes as
|
|
657
|
+
* {@link VidNavigatorClient.transcribe}.
|
|
484
658
|
*/
|
|
485
659
|
readonly extractVideo: JobOperation<ExtractVideoInput, ExtractVideoAsyncJob, ExtractDataResult>;
|
|
486
660
|
|
|
@@ -547,6 +721,17 @@ export class VidNavigatorClient {
|
|
|
547
721
|
},
|
|
548
722
|
});
|
|
549
723
|
|
|
724
|
+
this.analyze = this.defineOperation({
|
|
725
|
+
jobType: 'analyze_video',
|
|
726
|
+
submit: (input: string | AnalyzeVideoInput) =>
|
|
727
|
+
this.submitAsyncJob('/analyze/video/async', typeof input === 'string' ? { video_url: input } : input),
|
|
728
|
+
fetchTask: (task_id, options) =>
|
|
729
|
+
this.getAsyncJob(`/analyze/video/${encodeURIComponent(task_id)}`, options, (raw) =>
|
|
730
|
+
parseAnalyzeVideoData(raw)
|
|
731
|
+
),
|
|
732
|
+
toResult: async (task) => ({ ...requireResult(task), usage: task.usage }),
|
|
733
|
+
});
|
|
734
|
+
|
|
550
735
|
this.extractVideo = this.defineOperation({
|
|
551
736
|
jobType: 'extract_video',
|
|
552
737
|
submit: (input: ExtractVideoInput) => this.submitExtractVideo(input),
|
|
@@ -556,7 +741,13 @@ export class VidNavigatorClient {
|
|
|
556
741
|
options,
|
|
557
742
|
(raw) => raw as Record<string, unknown>
|
|
558
743
|
),
|
|
559
|
-
toResult: async (task) => ({
|
|
744
|
+
toResult: async (task) => ({
|
|
745
|
+
data: requireResult(task),
|
|
746
|
+
video_info: task.video_info,
|
|
747
|
+
...pickSceneNotes(task),
|
|
748
|
+
...(task.schema ? { schema: task.schema } : {}),
|
|
749
|
+
usage: task.usage,
|
|
750
|
+
}),
|
|
560
751
|
});
|
|
561
752
|
|
|
562
753
|
this.tweetStatement = this.defineOperation({
|
|
@@ -653,12 +844,14 @@ export class VidNavigatorClient {
|
|
|
653
844
|
}
|
|
654
845
|
|
|
655
846
|
private submitExtractVideo(input: ExtractVideoInput): Promise<JobInfo> {
|
|
847
|
+
checkSchemaInput(input);
|
|
656
848
|
if ('schemaFilePath' in input) {
|
|
657
849
|
const form = new FormData();
|
|
658
850
|
form.append('video_url', input.video_url);
|
|
659
851
|
form.append('schema', fs.createReadStream(input.schemaFilePath));
|
|
660
852
|
appendOptionalFormField(form, 'what_to_extract', input.what_to_extract);
|
|
661
853
|
appendOptionalFormField(form, 'transcribe', input.transcribe);
|
|
854
|
+
appendOptionalFormField(form, 'include_scenes', input.include_scenes);
|
|
662
855
|
appendOptionalFormField(form, 'webhook_url', input.webhook_url);
|
|
663
856
|
return this.submitAsyncJob('/extract/video/async', form, form.getHeaders());
|
|
664
857
|
}
|
|
@@ -913,27 +1106,39 @@ export class VidNavigatorClient {
|
|
|
913
1106
|
};
|
|
914
1107
|
}
|
|
915
1108
|
|
|
916
|
-
|
|
917
|
-
|
|
918
|
-
|
|
919
|
-
|
|
1109
|
+
/**
|
|
1110
|
+
* A file's metadata, its transcript once it has one, and its scenes once they are analyzed.
|
|
1111
|
+
* `file_info.scenes_status` says where the scene analysis stands; when it is `completed`, the
|
|
1112
|
+
* result has `scenes`, as {@link VidNavigatorClient.watch} returns them for an online video.
|
|
1113
|
+
*/
|
|
1114
|
+
async getFile(file_id: string, query?: { transcript_text?: boolean }): Promise<GetFileResult> {
|
|
920
1115
|
const response = await this.request<
|
|
921
1116
|
ApiSuccessResponse<{
|
|
922
1117
|
file_info: FileInfoJSON;
|
|
923
1118
|
transcript?: TranscriptSegmentJSON[] | string;
|
|
1119
|
+
scenes?: VideoSceneJSON[];
|
|
924
1120
|
}>
|
|
925
1121
|
>('GET', `/file/${file_id}`, undefined, query);
|
|
926
1122
|
|
|
927
1123
|
return {
|
|
928
1124
|
file_info: FileInfo.fromJSON(response.data.file_info),
|
|
929
1125
|
transcript: transcriptFromJSON(response.data.transcript),
|
|
1126
|
+
...parseScenes(response.data.scenes),
|
|
930
1127
|
};
|
|
931
1128
|
}
|
|
932
1129
|
|
|
1130
|
+
/**
|
|
1131
|
+
* Upload an audio or video file. With `include_scenes: true` the picture of a video file is
|
|
1132
|
+
* analyzed as well, next to its transcription: follow `file_info.scenes_status` with
|
|
1133
|
+
* {@link VidNavigatorClient.getFile}, which returns the scenes once it is `completed`. An audio
|
|
1134
|
+
* file has no picture: its `scenes_status` is `failed`, with `frames_not_available`.
|
|
1135
|
+
*/
|
|
933
1136
|
async uploadFile(options: {
|
|
934
1137
|
filePath: string;
|
|
935
1138
|
wait_for_completion?: boolean;
|
|
936
1139
|
namespace_ids?: string[];
|
|
1140
|
+
/** Default false. `true` also analyzes the scenes of the file (video files only). */
|
|
1141
|
+
include_scenes?: boolean;
|
|
937
1142
|
}): Promise<UploadFileResult> {
|
|
938
1143
|
const form = new FormData();
|
|
939
1144
|
form.append('file', fs.createReadStream(options.filePath));
|
|
@@ -943,6 +1148,7 @@ export class VidNavigatorClient {
|
|
|
943
1148
|
if (options.namespace_ids?.length) {
|
|
944
1149
|
form.append('namespace_ids', JSON.stringify(options.namespace_ids));
|
|
945
1150
|
}
|
|
1151
|
+
appendOptionalFormField(form, 'include_scenes', options.include_scenes);
|
|
946
1152
|
|
|
947
1153
|
try {
|
|
948
1154
|
const response = await this.client.post<{
|
|
@@ -1012,6 +1218,26 @@ export class VidNavigatorClient {
|
|
|
1012
1218
|
return response.data;
|
|
1013
1219
|
}
|
|
1014
1220
|
|
|
1221
|
+
/**
|
|
1222
|
+
* Start the scene analysis of a file already uploaded: what its picture shows, scene by scene,
|
|
1223
|
+
* with the text readable on screen. The scenes of a file are analyzed once and kept with it.
|
|
1224
|
+
*
|
|
1225
|
+
* The answer has `scenes_status` `processing` when the analysis was started or is already
|
|
1226
|
+
* running, and `completed` when the scenes are already there (nothing new is started). Follow
|
|
1227
|
+
* `file_info.scenes_status` with {@link VidNavigatorClient.getFile}, which returns the scenes
|
|
1228
|
+
* once it is `completed`; `analyzeFile` and `extractFileData` read them with `include_scenes: true`.
|
|
1229
|
+
*
|
|
1230
|
+
* An audio file has no picture: the call throws `BadRequestError` with `error_code`
|
|
1231
|
+
* `frames_not_available`.
|
|
1232
|
+
*/
|
|
1233
|
+
async analyzeFileScenes(file_id: string): Promise<AnalyzeFileScenesResult> {
|
|
1234
|
+
const response = await this.request<ApiSuccessResponse<AnalyzeFileScenesResult>>(
|
|
1235
|
+
'POST',
|
|
1236
|
+
`/file/${file_id}/scenes`
|
|
1237
|
+
);
|
|
1238
|
+
return response.data;
|
|
1239
|
+
}
|
|
1240
|
+
|
|
1015
1241
|
async cancelFileUpload(
|
|
1016
1242
|
file_id: string
|
|
1017
1243
|
): Promise<{ file_id: string; file_name: string; message: string }> {
|
|
@@ -1068,36 +1294,45 @@ export class VidNavigatorClient {
|
|
|
1068
1294
|
//endregion
|
|
1069
1295
|
|
|
1070
1296
|
//region --- Analysis ---
|
|
1297
|
+
/**
|
|
1298
|
+
* Analyze an online video: a summary, the people, places and key subjects, and the answer to
|
|
1299
|
+
* an optional `query`. With `include_scenes: true` the analysis is made from the transcript and
|
|
1300
|
+
* from what the video shows, and the result also has `scenes` and `frames_analyzed`, as
|
|
1301
|
+
* {@link VidNavigatorClient.watch} returns them. When the picture cannot be read, the analysis
|
|
1302
|
+
* is made from the transcript alone and `scenes_error` says why; a video without a transcript
|
|
1303
|
+
* is analyzed from its scenes, and `transcript_error` says why the transcript is missing.
|
|
1304
|
+
*
|
|
1305
|
+
* This call waits on one HTTP request. With `include_scenes` on a video longer than about
|
|
1306
|
+
* 10 minutes, use {@link VidNavigatorClient.analyze}, which runs the same analysis as a
|
|
1307
|
+
* background job.
|
|
1308
|
+
*/
|
|
1071
1309
|
async analyzeVideo(payload: AnalyzeVideoPayload): Promise<AnalyzeVideoResult> {
|
|
1072
|
-
const response = await this.request<
|
|
1073
|
-
|
|
1074
|
-
|
|
1075
|
-
|
|
1076
|
-
|
|
1077
|
-
}>
|
|
1078
|
-
>('POST', '/analyze/video', payload);
|
|
1310
|
+
const response = await this.request<ApiSuccessResponse<AnalysisDataJSON & { video_info: VideoInfoJSON }>>(
|
|
1311
|
+
'POST',
|
|
1312
|
+
'/analyze/video',
|
|
1313
|
+
payload
|
|
1314
|
+
);
|
|
1079
1315
|
|
|
1080
|
-
return
|
|
1081
|
-
video_info: VideoInfo.fromJSON(response.data.video_info),
|
|
1082
|
-
transcript: transcriptFromJSON(response.data.transcript)!,
|
|
1083
|
-
transcript_analysis: AnalysisResult.fromJSON(response.data.transcript_analysis),
|
|
1084
|
-
usage: response.usage ? UsageBlock.fromJSON(response.usage) : undefined,
|
|
1085
|
-
};
|
|
1316
|
+
return parseAnalyzeVideoData(response.data, response.usage ? UsageBlock.fromJSON(response.usage) : undefined);
|
|
1086
1317
|
}
|
|
1087
1318
|
|
|
1319
|
+
/**
|
|
1320
|
+
* Analyze an uploaded file. With `include_scenes: true` the analysis is made from the transcript
|
|
1321
|
+
* of the file and its scenes together, and the result also has `scenes` and `frames_analyzed`.
|
|
1322
|
+
* The scenes are the ones kept with the file: ask for them at the upload (`include_scenes`) or
|
|
1323
|
+
* with {@link VidNavigatorClient.analyzeFileScenes}. A file that has none is refused with
|
|
1324
|
+
* `ScenesNotAvailableError` (HTTP 409), whose `scenes_status` says where their analysis stands.
|
|
1325
|
+
*/
|
|
1088
1326
|
async analyzeFile(payload: AnalyzeFilePayload): Promise<AnalyzeFileResult> {
|
|
1089
|
-
const response = await this.request<
|
|
1090
|
-
|
|
1091
|
-
|
|
1092
|
-
|
|
1093
|
-
|
|
1094
|
-
}>
|
|
1095
|
-
>('POST', '/analyze/file', payload);
|
|
1327
|
+
const response = await this.request<ApiSuccessResponse<AnalysisDataJSON & { file_info: FileInfoJSON }>>(
|
|
1328
|
+
'POST',
|
|
1329
|
+
'/analyze/file',
|
|
1330
|
+
payload
|
|
1331
|
+
);
|
|
1096
1332
|
|
|
1097
1333
|
return {
|
|
1098
1334
|
file_info: FileInfo.fromJSON(response.data.file_info),
|
|
1099
|
-
|
|
1100
|
-
transcript_analysis: AnalysisResult.fromJSON(response.data.transcript_analysis),
|
|
1335
|
+
...parseAnalysisData(response.data),
|
|
1101
1336
|
usage: response.usage ? UsageBlock.fromJSON(response.usage) : undefined,
|
|
1102
1337
|
};
|
|
1103
1338
|
}
|
|
@@ -1119,6 +1354,8 @@ export class VidNavigatorClient {
|
|
|
1119
1354
|
* Structured-data extraction from an online video. Blocking convenience wrapper around
|
|
1120
1355
|
* {@link VidNavigatorClient.extractVideo}: submits to `POST /extract/video/async` and polls, so
|
|
1121
1356
|
* media of any length works. The result carries the extracted `data` and the video's `video_info`.
|
|
1357
|
+
* With `include_scenes: true` the fields are extracted from the transcript and from what the video
|
|
1358
|
+
* shows, and the result also has `frames_analyzed`.
|
|
1122
1359
|
*/
|
|
1123
1360
|
async extractVideoData(
|
|
1124
1361
|
payload: ExtractVideoDataPayload | ExtractVideoDataMultipartPayload,
|
|
@@ -1128,38 +1365,43 @@ export class VidNavigatorClient {
|
|
|
1128
1365
|
return this.extractVideo(input as ExtractVideoInput, { ...options, include_usage });
|
|
1129
1366
|
}
|
|
1130
1367
|
|
|
1368
|
+
/**
|
|
1369
|
+
* Structured-data extraction from an uploaded file. With `include_scenes: true` the fields are
|
|
1370
|
+
* extracted from the transcript of the file and its scenes together, and the result has
|
|
1371
|
+
* `frames_analyzed`. The scenes are the ones kept with the file: ask for them at the upload
|
|
1372
|
+
* (`include_scenes`) or with {@link VidNavigatorClient.analyzeFileScenes}. A file that has none
|
|
1373
|
+
* is refused with `ScenesNotAvailableError` (HTTP 409).
|
|
1374
|
+
*/
|
|
1131
1375
|
async extractFileData(
|
|
1132
1376
|
payload: ExtractFileDataPayload | ExtractFileDataMultipartPayload
|
|
1133
1377
|
): Promise<ExtractDataResult> {
|
|
1378
|
+
checkSchemaInput(payload);
|
|
1379
|
+
type ExtractFileBody = {
|
|
1380
|
+
status: 'success';
|
|
1381
|
+
data: Record<string, unknown>;
|
|
1382
|
+
file_info?: FileInfoJSON;
|
|
1383
|
+
schema?: ExtractionSchema;
|
|
1384
|
+
usage?: UsageBlockJSON;
|
|
1385
|
+
} & SceneNotes;
|
|
1386
|
+
let body: ExtractFileBody;
|
|
1387
|
+
|
|
1134
1388
|
if ('schemaFilePath' in payload) {
|
|
1135
1389
|
const form = new FormData();
|
|
1136
1390
|
form.append('file_id', payload.file_id);
|
|
1137
1391
|
form.append('schema', fs.createReadStream(payload.schemaFilePath));
|
|
1138
1392
|
appendOptionalFormField(form, 'what_to_extract', payload.what_to_extract);
|
|
1393
|
+
appendOptionalFormField(form, 'include_scenes', payload.include_scenes);
|
|
1139
1394
|
appendOptionalFormField(form, 'include_usage', payload.include_usage);
|
|
1140
|
-
|
|
1141
|
-
|
|
1142
|
-
|
|
1143
|
-
data: Record<string, unknown>;
|
|
1144
|
-
file_info?: FileInfoJSON;
|
|
1145
|
-
usage?: UsageBlockJSON;
|
|
1146
|
-
}>('POST', '/extract/file', form, undefined, form.getHeaders());
|
|
1147
|
-
return {
|
|
1148
|
-
data: body.data,
|
|
1149
|
-
file_info: body.file_info ? FileInfo.fromJSON(body.file_info) : undefined,
|
|
1150
|
-
usage: body.usage ? UsageBlock.fromJSON(body.usage) : undefined,
|
|
1151
|
-
};
|
|
1395
|
+
body = await this.request<ExtractFileBody>('POST', '/extract/file', form, undefined, form.getHeaders());
|
|
1396
|
+
} else {
|
|
1397
|
+
body = await this.request<ExtractFileBody>('POST', '/extract/file', payload);
|
|
1152
1398
|
}
|
|
1153
1399
|
|
|
1154
|
-
const body = await this.request<{
|
|
1155
|
-
status: 'success';
|
|
1156
|
-
data: Record<string, unknown>;
|
|
1157
|
-
file_info?: FileInfoJSON;
|
|
1158
|
-
usage?: UsageBlockJSON;
|
|
1159
|
-
}>('POST', '/extract/file', payload);
|
|
1160
1400
|
return {
|
|
1161
1401
|
data: body.data,
|
|
1162
1402
|
file_info: body.file_info ? FileInfo.fromJSON(body.file_info) : undefined,
|
|
1403
|
+
...pickSceneNotes(body),
|
|
1404
|
+
...(body.schema ? { schema: body.schema } : {}),
|
|
1163
1405
|
usage: body.usage ? UsageBlock.fromJSON(body.usage) : undefined,
|
|
1164
1406
|
};
|
|
1165
1407
|
}
|
package/src/models/AsyncJob.ts
CHANGED
|
@@ -1,8 +1,10 @@
|
|
|
1
|
+
import type { ExtractionSchema } from './Extraction';
|
|
1
2
|
import { UsageBlock } from './Usage';
|
|
2
3
|
import { VideoInfo, VideoInfoJSON } from './VideoInfo';
|
|
4
|
+
import { ScenesError, WatchTranscriptError } from './Watch';
|
|
3
5
|
|
|
4
|
-
/** Async job kinds returned by the `/transcribe/async`, `/watch/async`, `/extract/video/async` and `/tweet/statement/async` endpoints. */
|
|
5
|
-
export type AsyncJobType = 'transcribe' | 'watch' | 'extract_video' | 'tweet_statement';
|
|
6
|
+
/** Async job kinds returned by the `/transcribe/async`, `/watch/async`, `/analyze/video/async`, `/extract/video/async` and `/tweet/statement/async` endpoints. */
|
|
7
|
+
export type AsyncJobType = 'transcribe' | 'watch' | 'analyze_video' | 'extract_video' | 'tweet_statement';
|
|
6
8
|
|
|
7
9
|
/** `processing` = accepted and being worked on. `completed` and `failed` are terminal. */
|
|
8
10
|
export type AsyncTaskStatus = 'processing' | 'completed' | 'failed';
|
|
@@ -86,6 +88,12 @@ export interface AsyncJobJSON {
|
|
|
86
88
|
result?: any;
|
|
87
89
|
/** `extract_video` jobs, once completed: the `video_info` the synchronous endpoint returns. */
|
|
88
90
|
video_info?: VideoInfoJSON;
|
|
91
|
+
/** `extract_video` jobs, once completed with `include_scenes`: the `frames_analyzed` the synchronous endpoint returns. */
|
|
92
|
+
frames_analyzed?: number;
|
|
93
|
+
scenes_error?: ScenesError;
|
|
94
|
+
transcript_error?: WatchTranscriptError;
|
|
95
|
+
/** `extract_video` jobs, once completed, when the request had no schema: the schema written from `what_to_extract`. */
|
|
96
|
+
schema?: ExtractionSchema;
|
|
89
97
|
error?: AsyncJobError | null;
|
|
90
98
|
}
|
|
91
99
|
|
|
@@ -110,6 +118,14 @@ export class AsyncJob<TResult> {
|
|
|
110
118
|
result: TResult | null;
|
|
111
119
|
/** `extract_video` jobs, once completed: metadata of the video the data was extracted from. */
|
|
112
120
|
video_info?: VideoInfo;
|
|
121
|
+
/** `extract_video` jobs, once completed with `include_scenes`: number of frames that were analyzed to produce the scenes. */
|
|
122
|
+
frames_analyzed?: number;
|
|
123
|
+
/** `extract_video` jobs with `include_scenes`: why the picture could not be read. The fields were then extracted from the transcript alone. */
|
|
124
|
+
scenes_error?: ScenesError;
|
|
125
|
+
/** `extract_video` jobs with `include_scenes`: why there is no transcript. The fields were then extracted from the scenes alone. */
|
|
126
|
+
transcript_error?: WatchTranscriptError;
|
|
127
|
+
/** `extract_video` jobs, once completed, when the request had no schema: the schema written from `what_to_extract`, to pass as `schema` next time. */
|
|
128
|
+
schema?: ExtractionSchema;
|
|
113
129
|
error: AsyncJobError | null;
|
|
114
130
|
/** Per-call usage, populated only when polled with `include_usage: true` and `task_status === 'completed'`. */
|
|
115
131
|
usage?: UsageBlock;
|
|
@@ -127,6 +143,10 @@ export class AsyncJob<TResult> {
|
|
|
127
143
|
this.webhook = data.webhook;
|
|
128
144
|
this.result = data.result !== undefined && data.result !== null ? parseResult(data.result) : null;
|
|
129
145
|
this.video_info = data.video_info ? VideoInfo.fromJSON(data.video_info) : undefined;
|
|
146
|
+
this.frames_analyzed = data.frames_analyzed;
|
|
147
|
+
this.scenes_error = data.scenes_error;
|
|
148
|
+
this.transcript_error = data.transcript_error;
|
|
149
|
+
this.schema = data.schema;
|
|
130
150
|
this.error = data.error ?? null;
|
|
131
151
|
}
|
|
132
152
|
|