vidnavigator 2.4.0 → 2.5.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.
@@ -7,7 +7,7 @@ export interface VideoSceneJSON {
7
7
  description?: string;
8
8
  on_screen_text?: string;
9
9
  }
10
- /** What a video shows between `start` and `end`. */
10
+ /** What a video, or an uploaded file, shows between `start` and `end`. */
11
11
  export declare class VideoScene {
12
12
  /** Start of the scene, in seconds from the beginning of the video. */
13
13
  start: number;
@@ -20,11 +20,16 @@ export declare class VideoScene {
20
20
  constructor(data: VideoSceneJSON);
21
21
  static fromJSON(json: VideoSceneJSON): VideoScene;
22
22
  }
23
- /** Why a watched video came back without a transcript (e.g. `transcript_not_available`). */
23
+ /** Why a video or a file came back without a transcript (e.g. `transcript_not_available`). */
24
24
  export interface WatchTranscriptError {
25
25
  error?: string;
26
26
  message?: string;
27
27
  }
28
+ /** Why the scenes of a video or a file could not be had (e.g. `frames_not_available`). */
29
+ export interface ScenesError {
30
+ error?: string;
31
+ message?: string;
32
+ }
28
33
  export interface WatchResultJSON {
29
34
  video_info: VideoInfoJSON;
30
35
  transcript?: TranscriptOutputJSON;
@@ -3,7 +3,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.WatchResult = exports.VideoScene = void 0;
4
4
  const VideoInfo_1 = require("./VideoInfo");
5
5
  const TranscriptSegment_1 = require("./TranscriptSegment");
6
- /** What a video shows between `start` and `end`. */
6
+ /** What a video, or an uploaded file, shows between `start` and `end`. */
7
7
  class VideoScene {
8
8
  constructor(data) {
9
9
  this.start = data.start;
@@ -1,6 +1,6 @@
1
1
  import { AsyncJobError } from './models';
2
2
  /** Event types delivered to a `webhook_url` when an async job reaches a terminal state. */
3
- export type WebhookEventType = 'transcribe.completed' | 'transcribe.failed' | 'watch.completed' | 'watch.failed' | 'extract_video.completed' | 'extract_video.failed' | 'tweet_statement.completed' | 'tweet_statement.failed' | 'tiktok_profile.completed' | 'tiktok_profile.failed' | 'tiktok_search.completed' | 'tiktok_search.failed' | 'tiktok_live_search.completed' | 'tiktok_live_search.failed';
3
+ export type WebhookEventType = 'transcribe.completed' | 'transcribe.failed' | 'watch.completed' | 'watch.failed' | 'analyze_video.completed' | 'analyze_video.failed' | 'extract_video.completed' | 'extract_video.failed' | 'tweet_statement.completed' | 'tweet_statement.failed' | 'tiktok_profile.completed' | 'tiktok_profile.failed' | 'tiktok_search.completed' | 'tiktok_search.failed' | 'tiktok_live_search.completed' | 'tiktok_live_search.failed';
4
4
  /** `data.result` of a `tiktok_profile.*` / `tiktok_search.*` / `tiktok_live_search.*` event: a summary, not the items. */
5
5
  export interface TikTokWebhookResult {
6
6
  /** Same fields as the task's `stats` (e.g. `videos_matched`, or `results_count` / `sort_by` for searches). */
@@ -21,8 +21,8 @@ export interface WebhookEvent {
21
21
  job_type?: string;
22
22
  check_status_url?: string;
23
23
  /**
24
- * Raw (unparsed) result of the completed job. For transcription, watch, extraction and tweet jobs
25
- * it is the same data the job's `result()` parses. For TikTok tasks it is a summary only,
24
+ * Raw (unparsed) result of the completed job. For transcription, watch, analysis, extraction and tweet
25
+ * jobs it is the same data the job's `result()` parses. For TikTok tasks it is a summary only,
26
26
  * {@link TikTokWebhookResult} (a scrape can hold thousands of videos); fetch the videos with
27
27
  * `vn.tiktokProfile.resume(task_id).result()`. Omitted when `result_truncated` is true.
28
28
  */
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "vidnavigator",
3
- "version": "2.4.0",
3
+ "version": "2.5.0",
4
4
  "description": "Official JavaScript SDK for the VidNavigator Developer API",
5
5
  "author": "VidNavigator",
6
6
  "license": "Apache-2.0",
package/src/errors.ts CHANGED
@@ -1,6 +1,7 @@
1
1
  /**
2
2
  * Custom exception classes for VidNavigator SDK.
3
3
  */
4
+ import { FileScenesStatus, ScenesError } from './models';
4
5
 
5
6
  /**
6
7
  * Base class for all VidNavigator SDK errors.
@@ -181,6 +182,34 @@ export class TooManyActiveJobsError extends RateLimitExceededError {
181
182
  }
182
183
  }
183
184
 
185
+ /**
186
+ * Raised when `include_scenes` was asked on an uploaded file that has no scenes to read
187
+ * (HTTP 409, `scenes_not_available`). `scenes_status` says where their analysis stands: `none`
188
+ * (never asked for: start it with `analyzeFileScenes(file_id)`), `processing` (call again once
189
+ * `getFile()` reports `completed`) or `failed` (see `scenes_error`).
190
+ */
191
+ export class ScenesNotAvailableError extends VidNavigatorError {
192
+ public readonly scenes_status?: Exclude<FileScenesStatus, 'completed'>;
193
+ /** Present when `scenes_status` is `failed`, e.g. `frames_not_available` for an audio file. */
194
+ public readonly scenes_error?: ScenesError;
195
+
196
+ constructor(
197
+ message: string,
198
+ status_code?: number,
199
+ error_code?: string,
200
+ error_message?: string,
201
+ details?: any,
202
+ scenes_status?: Exclude<FileScenesStatus, 'completed'>,
203
+ scenes_error?: ScenesError
204
+ ) {
205
+ super(message, status_code, error_code, error_message, details);
206
+ this.name = 'ScenesNotAvailableError';
207
+ this.scenes_status = scenes_status;
208
+ this.scenes_error = scenes_error;
209
+ Object.setPrototypeOf(this, ScenesNotAvailableError.prototype);
210
+ }
211
+ }
212
+
184
213
  /**
185
214
  * Raised when waiting for a job exceeds `timeoutMs`. Always carries `task_id`: the job keeps
186
215
  * running server-side and its result stays readable for 1 hour after it finishes, so resume it
package/src/http.ts CHANGED
@@ -15,6 +15,7 @@ import {
15
15
  SystemOverloadError,
16
16
  InsufficientCreditsError,
17
17
  TooManyActiveJobsError,
18
+ ScenesNotAvailableError,
18
19
  } from './errors';
19
20
 
20
21
  export function parseApiErrorPayload(data: any): {
@@ -50,6 +51,19 @@ export function buildApiError(status: number, data: any, fallbackMessage?: strin
50
51
  return new AccessDeniedError(message, status, errorCode, errorMessage, details);
51
52
  case 404:
52
53
  return new NotFoundError(message, status, errorCode, errorMessage, details);
54
+ case 409:
55
+ if (errorCode === 'scenes_not_available') {
56
+ return new ScenesNotAvailableError(
57
+ message,
58
+ status,
59
+ errorCode,
60
+ errorMessage,
61
+ details,
62
+ data?.scenes_status,
63
+ data?.scenes_error
64
+ );
65
+ }
66
+ return new VidNavigatorError(message, status, errorCode, errorMessage, details);
53
67
  case 413:
54
68
  return new StorageQuotaExceededError(message, status, errorCode, errorMessage, details);
55
69
  case 429:
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.4.0';
75
+ export const SDK_VERSION = '2.5.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,6 +145,20 @@ 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
 
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
+
143
162
  /** Input for {@link VidNavigatorClient.extractVideo}: a JSON `schema`, or a JSON/YAML `schemaFilePath`. */
144
163
  export type ExtractVideoInput =
145
164
  | ({
@@ -148,6 +167,11 @@ export type ExtractVideoInput =
148
167
  what_to_extract?: string;
149
168
  /** Speech-to-text the audio when no platform transcript exists. Default true. */
150
169
  transcribe?: boolean;
170
+ /**
171
+ * Default false. `true` also reads what the video shows: the fields are extracted from the
172
+ * transcript and the scenes together, and the result has `frames_analyzed`.
173
+ */
174
+ include_scenes?: boolean;
151
175
  } & WebhookOption)
152
176
  | ({
153
177
  video_url: string;
@@ -155,6 +179,7 @@ export type ExtractVideoInput =
155
179
  schemaFilePath: string;
156
180
  what_to_extract?: string;
157
181
  transcribe?: boolean;
182
+ include_scenes?: boolean;
158
183
  } & WebhookOption);
159
184
 
160
185
  /** Input for {@link VidNavigatorClient.tweetStatement}. A plain string is treated as `tweet_id`. */
@@ -186,6 +211,9 @@ export type TranscribeAsyncJob = AsyncJob<TranscribeVideoResult>;
186
211
  /** Snapshot of a watch task. `result` is a {@link WatchResult}. */
187
212
  export type WatchAsyncJob = AsyncJob<WatchResult>;
188
213
 
214
+ /** Snapshot of a video analysis task. `result` has the same shape as {@link VidNavigatorClient.analyzeVideo}'s return value. */
215
+ export type AnalyzeVideoAsyncJob = AsyncJob<AnalyzeVideoResult>;
216
+
189
217
  /** Snapshot of an extraction task. `result` is the extracted object, shaped like your schema. */
190
218
  export type ExtractVideoAsyncJob = AsyncJob<Record<string, unknown>>;
191
219
 
@@ -208,6 +236,7 @@ export interface TranscribeOperation extends JobOperation<string | TranscribeInp
208
236
  submit(input: string | TranscribeInput, options?: SubmitOptions): Promise<Job<TranscribeAsyncJob, TranscribeVideoResult>>;
209
237
  }
210
238
  export type WatchJob = Job<WatchAsyncJob, WatchResult>;
239
+ export type AnalyzeVideoJob = Job<AnalyzeVideoAsyncJob, AnalyzeVideoResult>;
211
240
  export type ExtractVideoJob = Job<ExtractVideoAsyncJob, ExtractDataResult>;
212
241
  export type TweetStatementJob = Job<TweetStatementAsyncJob, TweetStatement>;
213
242
  /** `result()` resolves to the completed task with **all** pages of `videos` collected. */
@@ -238,10 +267,33 @@ export type UploadFileAcceptedResult = {
238
267
 
239
268
  export type UploadFileResult = UploadFileSuccessResult | UploadFileAcceptedResult;
240
269
 
270
+ /** Answer of {@link VidNavigatorClient.analyzeFileScenes}. */
271
+ export type AnalyzeFileScenesResult = {
272
+ file_id: string;
273
+ /** `processing`: the analysis was started, or is already running. `completed`: the scenes are already there. */
274
+ scenes_status: Extract<FileScenesStatus, 'processing' | 'completed'>;
275
+ message: string;
276
+ };
277
+
278
+ /** Answer of {@link VidNavigatorClient.getFile}. */
279
+ export type GetFileResult = {
280
+ file_info: FileInfo;
281
+ /** Present once the file has a transcript. */
282
+ transcript?: TranscriptOutput;
283
+ /** What the file shows, in chronological order. Present once `file_info.scenes_status` is `completed`. */
284
+ scenes?: VideoScene[];
285
+ };
286
+
241
287
  export interface ExtractDataResult {
242
288
  data: Record<string, unknown>;
243
289
  video_info?: VideoInfo;
244
290
  file_info?: FileInfo;
291
+ /** With `include_scenes: true`: number of frames that were analyzed to produce the scenes. */
292
+ frames_analyzed?: number;
293
+ /** With `include_scenes: true`, when the picture could not be read: the fields were extracted from the transcript alone. */
294
+ scenes_error?: ScenesError;
295
+ /** With `include_scenes: true`, when there is no transcript: the fields were extracted from the scenes alone. */
296
+ transcript_error?: WatchTranscriptError;
245
297
  usage?: UsageBlock;
246
298
  }
247
299
 
@@ -296,6 +348,11 @@ export type SearchFilesResult = {
296
348
  export type AnalyzeVideoPayload = {
297
349
  video_url: string;
298
350
  query?: string;
351
+ /**
352
+ * Default false. `true` also reads what the video shows: the analysis is made from the
353
+ * transcript and the scenes together, and the result has `scenes` and `frames_analyzed`.
354
+ */
355
+ include_scenes?: boolean;
299
356
  transcript_text?: boolean;
300
357
  include_usage?: boolean;
301
358
  };
@@ -303,6 +360,13 @@ export type AnalyzeVideoPayload = {
303
360
  export type AnalyzeFilePayload = {
304
361
  file_id: string;
305
362
  query?: string;
363
+ /**
364
+ * Default false. `true` analyzes the file from its transcript and its scenes together. The
365
+ * scenes are the ones kept with the file: ask for them at the upload (`include_scenes`) or with
366
+ * {@link VidNavigatorClient.analyzeFileScenes}. A file that has none is refused with
367
+ * `ScenesNotAvailableError`.
368
+ */
369
+ include_scenes?: boolean;
306
370
  transcript_text?: boolean;
307
371
  include_usage?: boolean;
308
372
  };
@@ -311,6 +375,17 @@ export type AnalyzeVideoResult = {
311
375
  video_info: VideoInfo;
312
376
  transcript: TranscriptOutput;
313
377
  transcript_analysis: AnalysisResult;
378
+ /**
379
+ * With `include_scenes: true`: what the video shows, in chronological order. Empty when the
380
+ * picture could not be read (see `scenes_error`).
381
+ */
382
+ scenes?: VideoScene[];
383
+ /** With `include_scenes: true`: number of frames that were analyzed to produce the scenes. */
384
+ frames_analyzed?: number;
385
+ /** With `include_scenes: true`, when the picture could not be read: the analysis was made from the transcript alone. */
386
+ scenes_error?: ScenesError;
387
+ /** With `include_scenes: true`, when there is no transcript: the analysis was made from the scenes alone. */
388
+ transcript_error?: WatchTranscriptError;
314
389
  usage?: UsageBlock;
315
390
  };
316
391
 
@@ -318,6 +393,14 @@ export type AnalyzeFileResult = {
318
393
  file_info: FileInfo;
319
394
  transcript: TranscriptOutput;
320
395
  transcript_analysis: AnalysisResult;
396
+ /** With `include_scenes: true`: what the file shows, in chronological order. */
397
+ scenes?: VideoScene[];
398
+ /** With `include_scenes: true`: number of frames that were analyzed to produce the scenes. */
399
+ frames_analyzed?: number;
400
+ /** 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. */
401
+ scenes_error?: ScenesError;
402
+ /** With `include_scenes: true`, when the file has no speech: the analysis was made from the scenes alone. */
403
+ transcript_error?: WatchTranscriptError;
321
404
  usage?: UsageBlock;
322
405
  };
323
406
 
@@ -326,6 +409,11 @@ export type ExtractVideoDataPayload = {
326
409
  schema: ExtractionSchema;
327
410
  what_to_extract?: string;
328
411
  transcribe?: boolean;
412
+ /**
413
+ * Default false. `true` also reads what the video shows: the fields are extracted from the
414
+ * transcript and the scenes together, and the result has `frames_analyzed`.
415
+ */
416
+ include_scenes?: boolean;
329
417
  include_usage?: boolean;
330
418
  } & WebhookOption;
331
419
 
@@ -335,6 +423,7 @@ export type ExtractVideoDataMultipartPayload = {
335
423
  schemaFilePath: string;
336
424
  what_to_extract?: string;
337
425
  transcribe?: boolean;
426
+ include_scenes?: boolean;
338
427
  include_usage?: boolean;
339
428
  } & WebhookOption;
340
429
 
@@ -344,6 +433,13 @@ export type ExtractFileDataPayload = {
344
433
  file_id: string;
345
434
  schema: ExtractionSchema;
346
435
  what_to_extract?: string;
436
+ /**
437
+ * Default false. `true` extracts from the transcript of the file and its scenes together. The
438
+ * scenes are the ones kept with the file: ask for them at the upload (`include_scenes`) or with
439
+ * {@link VidNavigatorClient.analyzeFileScenes}. A file that has none is refused with
440
+ * `ScenesNotAvailableError`.
441
+ */
442
+ include_scenes?: boolean;
347
443
  include_usage?: boolean;
348
444
  };
349
445
 
@@ -352,6 +448,7 @@ export type ExtractFileDataMultipartPayload = {
352
448
  /** Path to a JSON or YAML schema file. The API accepts either a raw schema or a full extraction request object. */
353
449
  schemaFilePath: string;
354
450
  what_to_extract?: string;
451
+ include_scenes?: boolean;
355
452
  include_usage?: boolean;
356
453
  };
357
454
  //endregion
@@ -377,6 +474,50 @@ function parseTranscribeData(
377
474
  };
378
475
  }
379
476
 
477
+ /** What `include_scenes` adds beside the scenes themselves. */
478
+ interface SceneNotes {
479
+ frames_analyzed?: number;
480
+ scenes_error?: ScenesError;
481
+ transcript_error?: WatchTranscriptError;
482
+ }
483
+
484
+ /** The `data` block of an analysis: the same for a video, its background job and an uploaded file. */
485
+ interface AnalysisDataJSON extends SceneNotes {
486
+ transcript: TranscriptSegmentJSON[] | string;
487
+ transcript_analysis: AnalysisResultJSON;
488
+ scenes?: VideoSceneJSON[];
489
+ }
490
+
491
+ // The scene fields are set only when the response carries them, so a result asked without
492
+ // `include_scenes` keeps the shape it always had.
493
+ function pickSceneNotes(raw: SceneNotes): SceneNotes {
494
+ const notes: SceneNotes = {};
495
+ if (raw.frames_analyzed != null) notes.frames_analyzed = raw.frames_analyzed;
496
+ if (raw.scenes_error) notes.scenes_error = raw.scenes_error;
497
+ if (raw.transcript_error) notes.transcript_error = raw.transcript_error;
498
+ return notes;
499
+ }
500
+
501
+ function parseScenes(raw: VideoSceneJSON[] | undefined): { scenes?: VideoScene[] } {
502
+ return raw ? { scenes: raw.map((scene) => VideoScene.fromJSON(scene)) } : {};
503
+ }
504
+
505
+ function parseAnalysisData(raw: AnalysisDataJSON) {
506
+ return {
507
+ transcript: transcriptFromJSON(raw.transcript)!,
508
+ transcript_analysis: AnalysisResult.fromJSON(raw.transcript_analysis),
509
+ ...parseScenes(raw.scenes),
510
+ ...pickSceneNotes(raw),
511
+ };
512
+ }
513
+
514
+ function parseAnalyzeVideoData(
515
+ raw: AnalysisDataJSON & { video_info: VideoInfoJSON },
516
+ usage?: UsageBlock
517
+ ): AnalyzeVideoResult {
518
+ return { video_info: VideoInfo.fromJSON(raw.video_info), ...parseAnalysisData(raw), usage };
519
+ }
520
+
380
521
  function buildAsyncJobParams(options?: SubmitOptions): Record<string, string> | undefined {
381
522
  // Like the TikTok pollers, the async job pollers take include_usage as a query string value.
382
523
  return options?.include_usage ? { include_usage: 'true' } : undefined;
@@ -478,9 +619,19 @@ export class VidNavigatorClient {
478
619
  */
479
620
  readonly watch: JobOperation<string | WatchInput, WatchAsyncJob, WatchResult>;
480
621
 
622
+ /**
623
+ * Analysis of an online video as a background job, built on `POST /analyze/video/async`: the
624
+ * same analysis and the same result as {@link VidNavigatorClient.analyzeVideo}, without holding
625
+ * a connection open while it runs. Use it with `include_scenes: true` on a video longer than
626
+ * about 10 minutes. Same shapes as {@link VidNavigatorClient.transcribe}.
627
+ */
628
+ readonly analyze: JobOperation<string | AnalyzeVideoInput, AnalyzeVideoAsyncJob, AnalyzeVideoResult>;
629
+
481
630
  /**
482
631
  * Structured-data extraction from a video of any length, built on `POST /extract/video/async`.
483
- * The schema is validated when you submit. Same shapes as {@link VidNavigatorClient.transcribe}.
632
+ * The schema is validated when you submit. With `include_scenes: true` the fields are extracted
633
+ * from the transcript and from what the video shows. Same shapes as
634
+ * {@link VidNavigatorClient.transcribe}.
484
635
  */
485
636
  readonly extractVideo: JobOperation<ExtractVideoInput, ExtractVideoAsyncJob, ExtractDataResult>;
486
637
 
@@ -547,6 +698,17 @@ export class VidNavigatorClient {
547
698
  },
548
699
  });
549
700
 
701
+ this.analyze = this.defineOperation({
702
+ jobType: 'analyze_video',
703
+ submit: (input: string | AnalyzeVideoInput) =>
704
+ this.submitAsyncJob('/analyze/video/async', typeof input === 'string' ? { video_url: input } : input),
705
+ fetchTask: (task_id, options) =>
706
+ this.getAsyncJob(`/analyze/video/${encodeURIComponent(task_id)}`, options, (raw) =>
707
+ parseAnalyzeVideoData(raw)
708
+ ),
709
+ toResult: async (task) => ({ ...requireResult(task), usage: task.usage }),
710
+ });
711
+
550
712
  this.extractVideo = this.defineOperation({
551
713
  jobType: 'extract_video',
552
714
  submit: (input: ExtractVideoInput) => this.submitExtractVideo(input),
@@ -556,7 +718,12 @@ export class VidNavigatorClient {
556
718
  options,
557
719
  (raw) => raw as Record<string, unknown>
558
720
  ),
559
- toResult: async (task) => ({ data: requireResult(task), video_info: task.video_info, usage: task.usage }),
721
+ toResult: async (task) => ({
722
+ data: requireResult(task),
723
+ video_info: task.video_info,
724
+ ...pickSceneNotes(task),
725
+ usage: task.usage,
726
+ }),
560
727
  });
561
728
 
562
729
  this.tweetStatement = this.defineOperation({
@@ -659,6 +826,7 @@ export class VidNavigatorClient {
659
826
  form.append('schema', fs.createReadStream(input.schemaFilePath));
660
827
  appendOptionalFormField(form, 'what_to_extract', input.what_to_extract);
661
828
  appendOptionalFormField(form, 'transcribe', input.transcribe);
829
+ appendOptionalFormField(form, 'include_scenes', input.include_scenes);
662
830
  appendOptionalFormField(form, 'webhook_url', input.webhook_url);
663
831
  return this.submitAsyncJob('/extract/video/async', form, form.getHeaders());
664
832
  }
@@ -913,27 +1081,39 @@ export class VidNavigatorClient {
913
1081
  };
914
1082
  }
915
1083
 
916
- async getFile(
917
- file_id: string,
918
- query?: { transcript_text?: boolean }
919
- ): Promise<{ file_info: FileInfo; transcript?: TranscriptOutput }> {
1084
+ /**
1085
+ * A file's metadata, its transcript once it has one, and its scenes once they are analyzed.
1086
+ * `file_info.scenes_status` says where the scene analysis stands; when it is `completed`, the
1087
+ * result has `scenes`, as {@link VidNavigatorClient.watch} returns them for an online video.
1088
+ */
1089
+ async getFile(file_id: string, query?: { transcript_text?: boolean }): Promise<GetFileResult> {
920
1090
  const response = await this.request<
921
1091
  ApiSuccessResponse<{
922
1092
  file_info: FileInfoJSON;
923
1093
  transcript?: TranscriptSegmentJSON[] | string;
1094
+ scenes?: VideoSceneJSON[];
924
1095
  }>
925
1096
  >('GET', `/file/${file_id}`, undefined, query);
926
1097
 
927
1098
  return {
928
1099
  file_info: FileInfo.fromJSON(response.data.file_info),
929
1100
  transcript: transcriptFromJSON(response.data.transcript),
1101
+ ...parseScenes(response.data.scenes),
930
1102
  };
931
1103
  }
932
1104
 
1105
+ /**
1106
+ * Upload an audio or video file. With `include_scenes: true` the picture of a video file is
1107
+ * analyzed as well, next to its transcription: follow `file_info.scenes_status` with
1108
+ * {@link VidNavigatorClient.getFile}, which returns the scenes once it is `completed`. An audio
1109
+ * file has no picture: its `scenes_status` is `failed`, with `frames_not_available`.
1110
+ */
933
1111
  async uploadFile(options: {
934
1112
  filePath: string;
935
1113
  wait_for_completion?: boolean;
936
1114
  namespace_ids?: string[];
1115
+ /** Default false. `true` also analyzes the scenes of the file (video files only). */
1116
+ include_scenes?: boolean;
937
1117
  }): Promise<UploadFileResult> {
938
1118
  const form = new FormData();
939
1119
  form.append('file', fs.createReadStream(options.filePath));
@@ -943,6 +1123,7 @@ export class VidNavigatorClient {
943
1123
  if (options.namespace_ids?.length) {
944
1124
  form.append('namespace_ids', JSON.stringify(options.namespace_ids));
945
1125
  }
1126
+ appendOptionalFormField(form, 'include_scenes', options.include_scenes);
946
1127
 
947
1128
  try {
948
1129
  const response = await this.client.post<{
@@ -1012,6 +1193,26 @@ export class VidNavigatorClient {
1012
1193
  return response.data;
1013
1194
  }
1014
1195
 
1196
+ /**
1197
+ * Start the scene analysis of a file already uploaded: what its picture shows, scene by scene,
1198
+ * with the text readable on screen. The scenes of a file are analyzed once and kept with it.
1199
+ *
1200
+ * The answer has `scenes_status` `processing` when the analysis was started or is already
1201
+ * running, and `completed` when the scenes are already there (nothing new is started). Follow
1202
+ * `file_info.scenes_status` with {@link VidNavigatorClient.getFile}, which returns the scenes
1203
+ * once it is `completed`; `analyzeFile` and `extractFileData` read them with `include_scenes: true`.
1204
+ *
1205
+ * An audio file has no picture: the call throws `BadRequestError` with `error_code`
1206
+ * `frames_not_available`.
1207
+ */
1208
+ async analyzeFileScenes(file_id: string): Promise<AnalyzeFileScenesResult> {
1209
+ const response = await this.request<ApiSuccessResponse<AnalyzeFileScenesResult>>(
1210
+ 'POST',
1211
+ `/file/${file_id}/scenes`
1212
+ );
1213
+ return response.data;
1214
+ }
1215
+
1015
1216
  async cancelFileUpload(
1016
1217
  file_id: string
1017
1218
  ): Promise<{ file_id: string; file_name: string; message: string }> {
@@ -1068,36 +1269,45 @@ export class VidNavigatorClient {
1068
1269
  //endregion
1069
1270
 
1070
1271
  //region --- Analysis ---
1272
+ /**
1273
+ * Analyze an online video: a summary, the people, places and key subjects, and the answer to
1274
+ * an optional `query`. With `include_scenes: true` the analysis is made from the transcript and
1275
+ * from what the video shows, and the result also has `scenes` and `frames_analyzed`, as
1276
+ * {@link VidNavigatorClient.watch} returns them. When the picture cannot be read, the analysis
1277
+ * is made from the transcript alone and `scenes_error` says why; a video without a transcript
1278
+ * is analyzed from its scenes, and `transcript_error` says why the transcript is missing.
1279
+ *
1280
+ * This call waits on one HTTP request. With `include_scenes` on a video longer than about
1281
+ * 10 minutes, use {@link VidNavigatorClient.analyze}, which runs the same analysis as a
1282
+ * background job.
1283
+ */
1071
1284
  async analyzeVideo(payload: AnalyzeVideoPayload): Promise<AnalyzeVideoResult> {
1072
- const response = await this.request<
1073
- ApiSuccessResponse<{
1074
- video_info: VideoInfoJSON;
1075
- transcript: TranscriptSegmentJSON[] | string;
1076
- transcript_analysis: AnalysisResultJSON;
1077
- }>
1078
- >('POST', '/analyze/video', payload);
1285
+ const response = await this.request<ApiSuccessResponse<AnalysisDataJSON & { video_info: VideoInfoJSON }>>(
1286
+ 'POST',
1287
+ '/analyze/video',
1288
+ payload
1289
+ );
1079
1290
 
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
- };
1291
+ return parseAnalyzeVideoData(response.data, response.usage ? UsageBlock.fromJSON(response.usage) : undefined);
1086
1292
  }
1087
1293
 
1294
+ /**
1295
+ * Analyze an uploaded file. With `include_scenes: true` the analysis is made from the transcript
1296
+ * of the file and its scenes together, and the result also has `scenes` and `frames_analyzed`.
1297
+ * The scenes are the ones kept with the file: ask for them at the upload (`include_scenes`) or
1298
+ * with {@link VidNavigatorClient.analyzeFileScenes}. A file that has none is refused with
1299
+ * `ScenesNotAvailableError` (HTTP 409), whose `scenes_status` says where their analysis stands.
1300
+ */
1088
1301
  async analyzeFile(payload: AnalyzeFilePayload): Promise<AnalyzeFileResult> {
1089
- const response = await this.request<
1090
- ApiSuccessResponse<{
1091
- file_info: FileInfoJSON;
1092
- transcript: TranscriptSegmentJSON[] | string;
1093
- transcript_analysis: AnalysisResultJSON;
1094
- }>
1095
- >('POST', '/analyze/file', payload);
1302
+ const response = await this.request<ApiSuccessResponse<AnalysisDataJSON & { file_info: FileInfoJSON }>>(
1303
+ 'POST',
1304
+ '/analyze/file',
1305
+ payload
1306
+ );
1096
1307
 
1097
1308
  return {
1098
1309
  file_info: FileInfo.fromJSON(response.data.file_info),
1099
- transcript: transcriptFromJSON(response.data.transcript)!,
1100
- transcript_analysis: AnalysisResult.fromJSON(response.data.transcript_analysis),
1310
+ ...parseAnalysisData(response.data),
1101
1311
  usage: response.usage ? UsageBlock.fromJSON(response.usage) : undefined,
1102
1312
  };
1103
1313
  }
@@ -1119,6 +1329,8 @@ export class VidNavigatorClient {
1119
1329
  * Structured-data extraction from an online video. Blocking convenience wrapper around
1120
1330
  * {@link VidNavigatorClient.extractVideo}: submits to `POST /extract/video/async` and polls, so
1121
1331
  * media of any length works. The result carries the extracted `data` and the video's `video_info`.
1332
+ * With `include_scenes: true` the fields are extracted from the transcript and from what the video
1333
+ * shows, and the result also has `frames_analyzed`.
1122
1334
  */
1123
1335
  async extractVideoData(
1124
1336
  payload: ExtractVideoDataPayload | ExtractVideoDataMultipartPayload,
@@ -1128,38 +1340,40 @@ export class VidNavigatorClient {
1128
1340
  return this.extractVideo(input as ExtractVideoInput, { ...options, include_usage });
1129
1341
  }
1130
1342
 
1343
+ /**
1344
+ * Structured-data extraction from an uploaded file. With `include_scenes: true` the fields are
1345
+ * extracted from the transcript of the file and its scenes together, and the result has
1346
+ * `frames_analyzed`. The scenes are the ones kept with the file: ask for them at the upload
1347
+ * (`include_scenes`) or with {@link VidNavigatorClient.analyzeFileScenes}. A file that has none
1348
+ * is refused with `ScenesNotAvailableError` (HTTP 409).
1349
+ */
1131
1350
  async extractFileData(
1132
1351
  payload: ExtractFileDataPayload | ExtractFileDataMultipartPayload
1133
1352
  ): Promise<ExtractDataResult> {
1353
+ type ExtractFileBody = {
1354
+ status: 'success';
1355
+ data: Record<string, unknown>;
1356
+ file_info?: FileInfoJSON;
1357
+ usage?: UsageBlockJSON;
1358
+ } & SceneNotes;
1359
+ let body: ExtractFileBody;
1360
+
1134
1361
  if ('schemaFilePath' in payload) {
1135
1362
  const form = new FormData();
1136
1363
  form.append('file_id', payload.file_id);
1137
1364
  form.append('schema', fs.createReadStream(payload.schemaFilePath));
1138
1365
  appendOptionalFormField(form, 'what_to_extract', payload.what_to_extract);
1366
+ appendOptionalFormField(form, 'include_scenes', payload.include_scenes);
1139
1367
  appendOptionalFormField(form, 'include_usage', payload.include_usage);
1140
-
1141
- const body = await this.request<{
1142
- status: 'success';
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
- };
1368
+ body = await this.request<ExtractFileBody>('POST', '/extract/file', form, undefined, form.getHeaders());
1369
+ } else {
1370
+ body = await this.request<ExtractFileBody>('POST', '/extract/file', payload);
1152
1371
  }
1153
1372
 
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
1373
  return {
1161
1374
  data: body.data,
1162
1375
  file_info: body.file_info ? FileInfo.fromJSON(body.file_info) : undefined,
1376
+ ...pickSceneNotes(body),
1163
1377
  usage: body.usage ? UsageBlock.fromJSON(body.usage) : undefined,
1164
1378
  };
1165
1379
  }