Manage videos

The Resources.Video class provides methods to manage the videos you've uploaded to the platform.

Methods

Retrieve video information

Description: This method retrieves information about the specified video.

Function signature and example:

async retrieve(indexId: string, id: string, options: RequestOptions = {}): Promise<Models.Video>
const video = await client.index.video.retrieve(
  "<YOUR_INDEX_ID>",
  "<YOUR_VIDEO_ID>"
);
console.log(`ID: ${video.id}`);
console.log(`Created At: ${video.createdAt}`);
console.log(`Updated At: ${video.updatedAt || "N/A"}`);
console.log(`Indexed At: ${video.indexedAt || "N/A"}`);
console.log("Metadata:");
console.log(`  Filename: ${video.metadata.filename}`);
console.log(`  Duration: ${video.metadata.duration}`);
console.log(`  FPS: ${video.metadata.fps}`);
console.log(`  Width: ${video.metadata.width}`);
console.log(`  Height: ${video.metadata.height}`);
console.log(`  Size: ${video.metadata.size}`);
const additionalMetadata = Object.keys(video.metadata).filter(
(key) =>
    !["filename", "duration", "fps", "width", "height", "size"].includes(key)
);
if (additionalMetadata.length > 0) {
additionalMetadata.forEach((key) => {
    console.log(`  ${key}: ${video.metadata[key]}`);
});
}
if (video.hls) {
console.log("HLS:");
console.log(`  Video URL: ${video.hls.videoUrl || "N/A"}`);
console.log("  Thumbnail URLs:");
(video.hls.thumbnailUrls || []).forEach((url) => {
    console.log(`    ${url}`);
});
console.log(`  Status: ${video.hls.status || "N/A"}`);
console.log(`  Updated At: ${video.hls.updatedAt}`);
}
if (video.source) {
console.log("Source:");
console.log(`  Type: ${video.source.type}`);
console.log(`  Name: ${video.source.name}`);
console.log(`  URL: ${video.source.url || "N/A"}`);
}

Parameters:

NameTypeRequiredDescription
indexIdstringYesThe unique identifier of the index to which the video has been uploaded.
idstringYesThe unique identifier of the video to retrieve.
optionsRequestOptionsNoAdditional options for the request. Defaults to {}.

Return value: Returns a Promise that resolves to a Models.Video instance.

API Reference: For a description of each field in the request and response, see the Retrieve video information page.

List videos with direct pagination

Description: This method returns a paginated list of the videos in the specified index based on the provided parameters. Choose this method mainly when the total number of items is manageable, or you must fetch a single page of results. By default, the platform returns your videos sorted by their upload date, with the newest at the top of the list.

Function signature and example:

async list(
  indexId: string,
  {
    id,
    size,
    width,
    height,
    duration,
    fps,
    createdAt,
    updatedAt,
    indexedAt,
    ...restParams
  }: ListVideoParams = {},
  options: RequestOptions = {},
): Promise<Models.Video[]>
const listParams = {
id: "66ea61c40679760c1fc6a114",
filename: "example_video.mp4",
size: 1024,
width: 1280,
height: 720,  // Example: 720p to 1080p
duration: 20,  // Example: 1 minute to 5 minutes
fps: 24,
metadata: {
    category: "nature"
},
createdAt: "2024-09-17T07:53:46.365Z",
updatedAt: "2024-09-17T07:53:46.365Z",
indexedAt: "2024-09-17T07:55:22.125Z",
page: 1,
pageLimit: 5,
sortBy: "created_at",
sortOption: "desc"
};

const videos = await client.index.video.list("66e9358a808d95368f6f7a7c", listParams)
videos.forEach(video => {
console.log(`ID: ${video.id}`);
console.log(`  Created at: ${video.createdAt}`);
console.log(`  Updated at: ${video.updatedAt || "N/A"}`);
console.log(`  Indexed at: ${video.indexedAt || "N/A"}`);
console.log("  Metadata:");
console.log(`    Filename: ${video.metadata.filename}`);
console.log(`    Duration: ${video.metadata.duration}`);
console.log(`    FPS: ${video.metadata.fps}`);
console.log(`    Width: ${video.metadata.width}`);
console.log(`    Height: ${video.metadata.height}`);
console.log(`    Size: ${video.metadata.size}`);
if (video.hls) {
    console.log("  HLS:");
    console.log(`    Video URL: ${video.hls.videoUrl || "N/A"}`);
    console.log("    Thumbnail URLs:");
    (video.hls.thumbnailUrls || []).forEach((url) => {
    console.log(`      ${url}`);
    });
    console.log(`    Status: ${video.hls.status || "N/A"}`);
    console.log(`    Updated At: ${video.hls.updatedAt}`);
}
if (video.source) {
    console.log("  Source:");
    console.log(`    Type: ${video.source.type}`);
    console.log(`    Name: ${video.source.name}`);
    console.log(`    URL: ${video.source.url || "N/A"}`);
}
});

Parameters:

NameTypeRequiredDescription
indexIdstringYesThe unique identifier of the index to which the video has been uploaded.
paramsListVideoParamsNoParameters for retrieving the list of videos. Defaults to {}. If you don't specify the page parameter, the platform returns the first page of results.
optionsRequestOptionsNoAdditional options for the request. Defaults to {}.

The ListVideoParams interface extends the PageOptions interface and defines the parameters for listing videos:

NameTypeRequiredDescription
idstringNoFilter by the unique identifier of a video.
filenamestringNoFilter by filename.
sizenumber | Record<string, number>NoFilter by the size of the video file. This field can be a specific number or an object for range queries.
widthnumber | Record<string, number>NoFilter by the width of the video. This field can be a specific number or an object for range queries.
heightnumber | Record<string, number>NoFilter by the height of the video. This field can be a specific number or an object for range queries.
durationnumber | Record<string, number>NoFilter by the duration of the video. This field can be a specific number or an object for range queries.
fpsnumber | Record<string, number>NoFilter by the frames per second of the video. This field can be a specific number or an object for range queries.
metadataRecord<string, any>NoFilter by metadata associated with the video. This field can contain any key-value pairs.
createdAtstring | Record<string, string>NoFilter by the creation date of the video. This field can be a string or an object for range queries.
updatedAtstring | Record<string, string>NoFilter by the last update date of the video. This field can be a string or an object for range queries.
indexedAtstring | Record<string, string>NoFilter by the date when the video was indexed. This field can be a string or an object for range queries.

The following properties are inherited from the PageOptions interface:

NameTypeRequiredDescription
pagenumberNoThe page number for pagination.
pageLimitnumberNoThe number of items per page.
sortBy'created_at' | 'updated_at'NoSpecifies the field to sort by. This parameter must be either 'created_at' or 'updated_at'.
sortOption'asc' | 'desc'NoSpecifies the sort order. This parameter must be either 'asc' (ascending) or 'desc' (descending).

Return value: Returns a Promise that resolves to an array of Models.Video instances.

API Reference: For a description of each field in the request and response, see the List videos page.

Related guides:

List videos with iterative pagination

Description: This method iterates through a paginated list of the videos in the specified index based on the provided parameters. Choose this method mainly when your application must retrieve a large number of items. By default, the API returns your videos sorted by creation date, with the newest at the top of the list.

Function signature and example:

async listPagination(
  indexId: string,
  {
    id,
    size,
    width,
    height,
    duration,
    fps,
    createdAt,
    updatedAt,
    indexedAt,
    ...restParams
  }: ListVideoParams = {},
  options: RequestOptions = {},
): Promise<Models.VideoListWithPagination>
function printPage(page) {
  page.forEach(video => {
    console.log(video)
    console.log(`ID: ${video.id}`);
    console.log(`  Created at: ${video.createdAt}`);
    console.log(`  Updated at: ${video.updatedAt || "N/A"}`);
    console.log(`  Indexed at: ${video.indexedAt || "N/A"}`);
    console.log("  Metadata:");
    console.log(`    Filename: ${video.metadata.filename}`);
    console.log(`    Duration: ${video.metadata.duration}`);
    console.log(`    FPS: ${video.metadata.fps}`);
    console.log(`    Width: ${video.metadata.width}`);
    console.log(`    Height: ${video.metadata.height}`);
    console.log(`    Size: ${video.metadata.size}`);
    if (video.hls) {
      console.log("  HLS:");
      console.log(`    Video URL: ${video.hls.videoUrl || "N/A"}`);
      console.log("    Thumbnail URLs:");
      (video.hls.thumbnailUrls || []).forEach((url) => {
        console.log(`      ${url}`);
      });
      console.log(`    Status: ${video.hls.status || "N/A"}`);
      console.log(`    Updated At: ${video.hls.updatedAt}`);
    }
    if (video.source) {
      console.log("  Source:");
      console.log(`    Type: ${video.source.type}`);
      console.log(`    Name: ${video.source.name}`);
      console.log(`    URL: ${video.source.url || "N/A"}`);
    }
  });
}

const listParams = {
id: "66ea61c40679760c1fc6a114",
filename: "example_video.mp4",
size: 1024,
width: 1280,
height: 720,  // Example: 720p to 1080p
duration: 20,  // Example: 1 minute to 5 minutes
fps: 24,
metadata: {
    category: "nature"
},
createdAt: "2024-09-17T07:53:46.365Z",
updatedAt: "2024-09-17T07:53:46.365Z",
indexedAt: "2024-09-17T07:55:22.125Z",
page: 1,
pageLimit: 5,
sortBy: "created_at",
sortOption: "desc"
};

// Fetch the initial page of results
const videoPaginator = await client.index.video.listPagination("66e9358a808d95368f6f7a7c", listParams);

// Print the first page of results
printPage(videoPaginator.data);

// Iterate through subsequent pages
while (true) {
const nextPage = await videoPaginator.next();
if (!nextPage) {
    break;
}
printPage(nextPage);
}

Parameters:

NameTypeRequiredDescription
paramsListVideoParamsNoParameters for retrieving the list of videos. Defaults to {}. If you don't specify the page parameter, the platform returns the first page of results.
optionsRequestOptionsNoAdditional options for the request. Defaults to {}.

The ListVideoParams interface extends the PageOptions interface and defines the parameters for listing videos:

NameTypeRequiredDescription
idstringNoFilter by the unique identifier of a video.
filenamestringNoFilter by filename.
sizenumber | Record<string, number>NoFilter by the size of the video file. This field can be a specific number or an object for range queries.
widthnumber | Record<string, number>NoFilter by the width of the video. This field can be a specific number or an object for range queries.
heightnumber | Record<string, number>NoFilter by the height of the video. This field can be a specific number or an object for range queries.
durationnumber | Record<string, number>NoFilter by the duration of the video. This field can be a specific number or an object for range queries.
fpsnumber | Record<string, number>NoFilter by the frames per second of the video. This field can be a specific number or an object for range queries.
metadataRecord<string, any>NoFilter by metadata associated with the video. This field can contain any key-value pairs.
createdAtstring | Record<string, string>NoFilter by the creation date of the video. This field can be a string or an object for range queries.
updatedAtstring | Record<string, string>NoFilter by the last update date of the video. This field can be a string or an object for range queries.
indexedAtstring | Record<string, string>NoFilter by the date when the video was indexed. This field can be a string or an object for range queries.

The following properties are inherited from the PageOptions interface:

NameTypeRequiredDescription
pagenumberNoThe page number for pagination.
pageLimitnumberNoThe number of items per page.
sortBy'created_at' | 'updated_at'NoSpecifies the field to sort by. This parameter must be either 'created_at' or 'updated_at'.
sortOption'asc' | 'desc'NoSpecifies the sort order. This parameter must be either 'asc' (ascending) or 'desc' (descending).

Return value: Returns a Promise that resolves to an array of VideoListWithPagination instances.

📘

Note:

To retrieve subsequent pages of results, use the async iterator protocol:

  1. Invoke the nextmethod of the VideoListWithPagination object.
  2. Repeat this call until the method returns a promise that resolves tonull, indicating no more pages exist.

API Reference: For a description of each field in the request and response, see the List videos page.

Related guides:

Update video information

Description: This method updates the title and the metadata of a video.

Function signature and example:

async update(
  indexId: string,
  id: string,
  { title, metadata }: UpdateVideoParams,
  options: RequestOptions = {},
): Promise<void>
await client.index.video.update('<YOUR_INDEX_ID>', '<YOUR_VIDEO_ID>', { metadata: { from_sdk: true } });

Parameters:

NameTypeRequiredDescription
indexIdstringYesThe unique identifier of the index to which the video has been uploaded.
idstringYesThe unique identifier of the video.
paramsUpdateVideoParamsYesParameters for updating the video information.
optionsRequestOptionsNoAdditional options for the request. Defaults to {}.

The UpdateVideoParams interface defines the parameters for updating a video's information:

NameTypeRequiredDescription
titlestringNoThe new title for the video.
metadataRecord<string, any>NoThe metadata you want to update or add.. This parameter can contain any key-value pairs. Note that only the provided properties are modified; the omitted properties remain unchanged.

Return value: Returns a Promise that resolves to void.

API Reference: For a description of each field in the request, see the Update video information page.

Delete video information

Description: This method deletes all the information about the specified video. This action cannot be undone.

Function signature and example:

async delete(indexId: string, id: string, options: RequestOptions = {}): Promise<void> 
await client.index.video.delete('YOUR_INDEX_ID');

Parameters:

NameTypeRequiredDescription
indexIdstringYesThe unique identifier of the index to which the video has been uploaded.
idstringYesThe unique identifier of the video to delete.
optionsRequestOptionsNoAdditional options for the request. Defaults to {}.

Return value: Returns a Promise that resolves to void.

API Reference: Delete video information.