Skip to content

Support for scene index #16

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jan 22, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,16 @@ export const ApiPath = {
compile: 'compile',
workflow: 'workflow',
delete: 'delete',
describe: 'describe',
scene: 'scene',
scenes: 'scenes',
timeline: 'timeline',
} as const;

export const ResponseStatus = {
processing: 'processing',
in_progress: 'in progress',
success: 'success',
} as const;

export const HttpClientDefaultValues = {
Expand Down
8 changes: 6 additions & 2 deletions src/core/collection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import type {
AudioBase,
ImageBase,
} from '@/interfaces/core';
import { SearchType } from '@/types/search';
import { IndexType, SearchType } from '@/types/search';
import type { FileUploadConfig, URLUploadConfig } from '@/types/collection';
import type {
GetVideos,
Expand All @@ -19,7 +19,8 @@ import type {
import { fromSnakeToCamel } from '@/utils';
import { HttpClient } from '@/utils/httpClient';
import { uploadToServer } from '@/utils/upload';
import { SearchFactory, DefaultSearchType } from './search';
import { DefaultSearchType, DefaultIndexType } from '@/core/config';
import { SearchFactory } from './search';
import { Video } from './video';
import { Audio } from './audio';
import { Image } from './image';
Expand Down Expand Up @@ -219,6 +220,7 @@ export class Collection implements ICollection {
public search = async (
query: string,
searchType?: SearchType,
indexType?: IndexType,
resultThreshold?: number,
scoreThreshold?: number
) => {
Expand All @@ -228,6 +230,8 @@ export class Collection implements ICollection {
const results = await searchFunc.searchInsideCollection({
collectionId: this.meta.id,
query: query,
searchType: searchType ?? DefaultSearchType,
indexType: indexType ?? DefaultIndexType,
resultThreshold: resultThreshold,
scoreThreshold: scoreThreshold,
});
Expand Down
18 changes: 18 additions & 0 deletions src/core/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,3 +69,21 @@ export const TextStyleDefaultValues: TextStyleProps = {
x: '(main_w-text_w)/2',
y: '(main_h-text_h)/2',
};

export enum SearchTypeValues {
keyword = 'keyword',
semantic = 'semantic',
}

export enum IndexTypeValues {
spoken = 'spoken',
scene = 'scene',
}

export const DefaultSearchType = SearchTypeValues.semantic;
export const DefaultIndexType = IndexTypeValues.spoken;

export const SceneExtractionType = {
shotBased: 'shot',
timeBased: 'time',
};
2 changes: 1 addition & 1 deletion src/core/image.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,4 @@ export class Image implements IImage {
this.meta.id,
]);
};
}
}
2 changes: 1 addition & 1 deletion src/core/scene.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@ export class Scene {
this.start = start;
this.end = end;
}
}
}
85 changes: 36 additions & 49 deletions src/core/search/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,66 +5,54 @@ import {
} from '@/constants';
import type { Search } from '@/interfaces/core';
import type { SearchType } from '@/types/search';
import { SearchTypeValues } from '@/core/config';
import type { SearchResponse } from '@/types/response';
import type {
KeywordCollectionSearch,
KeywordVideoSearch,
SemanticCollectionSearch,
SemanticVideoSearch,
SceneCollectionSearch,
SceneVideoSearch,
// SceneCollectionSearch,
// SceneVideoSearch,
} from '@/types/search';
import { HttpClient } from '@/utils/httpClient';
import { SearchResult } from './searchResult';

const { video, search, collection } = ApiPath;

export enum SearchTypeValues {
semantic = 'semantic',
keyword = 'keyword',
scene = 'scene',
}

export enum IndexTypeValues {
semantic = 'semantic',
scene = 'scene',
}

export const DefaultSearchType = SearchTypeValues.semantic
export const DefaultIndexType = IndexTypeValues.semantic;

class SceneSearch implements Search<SceneVideoSearch, SceneCollectionSearch> {
#vhttp: HttpClient;
constructor(http: HttpClient) {
this.#vhttp = http;
}

private getRequestData = (data: SceneVideoSearch | SceneCollectionSearch) => {
return {
index_type: SearchTypeValues.scene,
query: data.query,
score_threshold:
data.scoreThreshold ?? SemanticSearchDefaultValues.scoreThreshold,
result_threshold:
data.resultThreshold ?? SemanticSearchDefaultValues.resultThreshold,
};
};

searchInsideVideo = async (data: SemanticVideoSearch) => {
const reqData = this.getRequestData(data);
const res = await this.#vhttp.post<SearchResponse, typeof reqData>(
[video, data.videoId, search],
reqData
);
return new SearchResult(this.#vhttp, res.data);
};

searchInsideCollection = async (data: SceneCollectionSearch) => {
throw new Error(
'Method not implemented. Scene search is not supported for Collection'
);
};
}
// class SceneSearch implements Search<SceneVideoSearch, SceneCollectionSearch> {
// #vhttp: HttpClient;
// constructor(http: HttpClient) {
// this.#vhttp = http;
// }

// private getRequestData = (data: SceneVideoSearch | SceneCollectionSearch) => {
// return {
// index_type: data.indexType ?? DefaultIndexType,
// search_type: data.searchType ?? DefaultSearchType,
// query: data.query,
// score_threshold:
// data.scoreThreshold ?? SemanticSearchDefaultValues.scoreThreshold,
// result_threshold:
// data.resultThreshold ?? SemanticSearchDefaultValues.resultThreshold,
// };
// };

// searchInsideVideo = async (data: SemanticVideoSearch) => {
// const reqData = this.getRequestData(data);
// const res = await this.#vhttp.post<SearchResponse, typeof reqData>(
// [video, data.videoId, search],
// reqData
// );
// return new SearchResult(this.#vhttp, res.data);
// };

// searchInsideCollection = async (data: SceneCollectionSearch) => {
// throw new Error(
// 'Method not implemented. Scene search is not supported for Collection'
// );
// };
// }

class SemanticSearch
implements Search<SemanticVideoSearch, SemanticCollectionSearch>
Expand Down Expand Up @@ -146,7 +134,6 @@ class KeywordSearch
const searchType = {
semantic: SemanticSearch,
keyword: KeywordSearch,
scene: SceneSearch,
};

export class SearchFactory {
Expand Down
108 changes: 76 additions & 32 deletions src/core/video.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,27 @@
import { fromSnakeToCamel } from './../utils/index';
import { ApiPath, Workflows } from '@/constants';
import type { IVideo, VideoBase } from '@/interfaces/core';
import { GetScenes, type GenerateStreamResponse } from '@/types/response';
import {
ListSceneIndex,
IndexScenesResponse,
type GenerateStreamResponse,
} from '@/types/response';
import type { Timeline, Transcript } from '@/types/video';
import { fromCamelToSnake, playStream } from '@/utils';
import { HttpClient } from '@/utils/httpClient';
import { IndexJob, TranscriptJob } from '@/utils/job';
import { SearchFactory, IndexTypeValues, DefaultSearchType } from './search';
import {
DefaultIndexType,
DefaultSearchType,
IndexTypeValues,
SceneExtractionType,
} from '@/core/config';
import { IndexJob, TranscriptJob, SceneIndexJob } from '@/utils/job';
import { SearchFactory } from './search';
import { IndexSceneConfig, SubtitleStyleProps } from '@/types/config';
import { SearchType } from '@/types/search';
import { Scene } from './scene';
import { SearchType, IndexType } from '@/types/search';
import { SceneIndexRecords, SceneIndexes } from '@/types';

const { video, stream, thumbnail, workflow, index } = ApiPath;
const { video, stream, thumbnail, workflow, index, scene, scenes } = ApiPath;

/**
* The base Video class
Expand All @@ -35,12 +46,14 @@ export class Video implements IVideo {
/**
* @param query - Search query
* @param searchType- [optional] Type of search to be performed
* @param indexType- [optional] Index Type
* @param resultThreshold - [optional] Result Threshold
* @param scoreThreshold - [optional] Score Threshold
*/
public search = async (
query: string,
searchType?: SearchType,
indexType?: IndexType,
resultThreshold?: number,
scoreThreshold?: number
) => {
Expand All @@ -49,6 +62,8 @@ export class Video implements IVideo {
const results = await searchFunc.searchInsideVideo({
videoId: this.meta.id,
query: query,
searchType: searchType ?? DefaultSearchType,
indexType: indexType ?? DefaultIndexType,
resultThreshold: resultThreshold,
scoreThreshold: scoreThreshold,
});
Expand Down Expand Up @@ -129,7 +144,7 @@ export class Video implements IVideo {
const indexJob = new IndexJob(
this.#vhttp,
this.meta.id,
IndexTypeValues.semantic
IndexTypeValues.spoken
);
return indexJob;
};
Expand All @@ -139,37 +154,66 @@ export class Video implements IVideo {
* @returns an awaited boolean signifying whether the process
* was successful or not
*/
public indexScenes = (config: Partial<IndexSceneConfig> = {}) => {
const indexJob = new IndexJob(
this.#vhttp,
this.meta.id,
IndexTypeValues.scene,
config
public indexScenes = async (config: Partial<IndexSceneConfig> = {}) => {
const defaultConfig = {
extractionType: SceneExtractionType.shotBased,
extractionConfig: {},
};
const indexScenesPayload = fromCamelToSnake(
Object.assign({}, defaultConfig, config)
);
return indexJob;
const res = await this.#vhttp.post<IndexScenesResponse, object>(
[video, this.meta.id, index, scene],
indexScenesPayload
);
if (res.data) {
return res.data.scene_index_id;
}
};

public getScenes = async () => {
const res = await this.#vhttp.get<GetScenes>([video, this.meta.id, index], {
params: {
index_type: IndexTypeValues.scene,
},
});
const scenes: Scene[] = [];
for (const scene of res.data) {
scenes.push(new Scene(scene.response, scene.start, scene.end));
}
return scenes;
public listSceneIndex = async () => {
const res = await this.#vhttp.get<ListSceneIndex>([
video,
this.meta.id,
index,
scene,
]);
const transformed = fromSnakeToCamel({
array: res.data.scene_indexes,
}).array;
return transformed as SceneIndexes;
};

public deleteSceneIndex = async () => {
const deleteScenesPayload = fromCamelToSnake({
indexType: IndexTypeValues.scene,
});
const res = await this.#vhttp.post<object, object>(
[video, this.meta.id, index, ApiPath.delete],
deleteScenesPayload
public getSceneIndex = async (sceneIndexId: string) => {
const sceneIndexJob = new SceneIndexJob(
this.#vhttp,
this.meta.id,
sceneIndexId
);
return new Promise<SceneIndexRecords>((resolve, reject) => {
sceneIndexJob.on('success', data => {
resolve(data);
});
sceneIndexJob.on('error', err => {
reject(err);
});
sceneIndexJob
.start()
.then(() => {})
.catch(err => {
reject(err);
});
});
};

public deleteSceneIndex = async (sceneIndexId: string) => {
const res = await this.#vhttp.delete([
video,
this.meta.id,
index,
scene,
sceneIndexId,
]);
return res;
};

Expand Down
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ export { Video } from './core/video';
export { Audio } from './core/audio';
export { Image } from './core/image';
export { VideoAsset, AudioAsset, ImageAsset, TextAsset } from './core/asset';
export { SceneExtractionType } from './core/config';
export {
ImageAssetConfig,
TextAssetConfig,
Expand Down
Loading
Loading