Summarize YouTube Videos

The Generate API suite generates texts such as fine-grained descriptions and summaries based on your videos. Unlike conventional models limited to unimodal interpretations that, for example, summarize videos relying solely on transcriptions, the Generate API suite uses a multimodal approach that analyzes the whole context of a video, including visuals, sounds, spoken words, and texts and their relationship with one another. This method ensures a holistic understanding of your videos, capturing nuances that an unimodal interpretation might miss. Consequently, utilizing its video-to-text generative capabilities, the platform translates this understanding into your desired textual representation.

Simplifying the content creation workflow

The "Summarize YouTube Videos" application addresses the challenge that YouTube influencers face in consistently generating fresh and engaging content. Using the Twelve Labs' Generate API, the application simplifies the content creation workflow by generating summaries, chapters, and highlights for your videos.

Overview

This guide is designed for developers with intermediate knowledge of Node.js and React. After completing this guide, you will learn to integrate the Twelve Labs Video Understanding Platform's summarization functionalities into your web application.

The application consists of a Node.js backend and a React frontend:

  • The backend is an intermediary between the Twelve Labs Video Understanding platform, the frontend, and YouTube. It uses the Express web framework.

  • The frontend is structured as a typical React application, and it uses React Query, a library for fetching, caching, and updating data.

Prerequisites

  • You're familiar with the concepts described on the Platform overview page.
  • Before you begin, sign up for a free account, or if you already have one, sign in.
  • You have a basic understanding of React (components, state, and props) and Node.js.
  • You're familiar with using command line interfaces and GitHub.
  • Node.js is installed on your system.
  • Nodemon is installed on your system.

Run the application

  1. Clone the Summarize YouTube Videos GitHub repository to your computer.

  2. Retrieve your API key by going to the Dashboard page and selecting the Copy icon at the right of your API key.

  3. Retrieve the base URL and the latest version of the API. Using these values, construct the API URL. For instructions, see the Call an endpoint page.

  4. Create a file named .env with the key-value pairs shown below in the root directory of your project, replacing the placeholders surrounded by <> with your values:

    REACT_APP_API_URL=<BASE_URL>/<VERSION>
    REACT_APP_API_KEY=<YOUR_API_KEY>
    REACT_APP_SERVER_URL=<SERVER_URL>
    REACT_APP_PORT_NUMBER=<SERVER_PORT>
    

    Example:

    REACT_APP_API_URL=https://api.twelvelabs.io/v1.2
    REACT_APP_API_KEY=tlk_1KM4EKT25JWQV722BNZZ30GAD1JA
    REACT_APP_SERVER_URL=http://localhost
    REACT_APP_PORT_NUMBER=4000
    
  5. To install the dependencies, open a terminal window, navigate to the root directory of your project, and run the following command:

    npm install
    
  6. To start the backend, enter the following command:

    nodemon server.js
    

    You should see an output similar to this:

    [nodemon] 3.0.1
    [nodemon] to restart at any time, enter `rs`
    [nodemon] watching path(s): *.*
    [nodemon] watching extensions: js,mjs,cjs,json
    [nodemon] starting `node server.js`
    
  7. To start the frontend, open a new terminal window, navigate to the root directory of your project, and enter the following command:

    npm start
    

    This will open a new browser window displaying the application:

  8. Typically, using the application is a two-step process:

    1. Upload a video.
    2. Generate a summary, chapters, or highlights for your video.

Integration with Twelve Labs

This section provides a concise overview of integrating the "Summarize a Youtube Video" application with the Twelve Labs Video Understanding Platform for generating summaries, chapters, and highlights for your videos.

List the videos within an index

To list the videos within an index, the application invokes the GET method of the /indexes/{index-id}/videos/{video-id} endpoint with the following parameters and values:

Parameter and valuePurpose
page_limit = request.query.page_limitThe platform returns the results one page at a time, with a default limit of 10 videos on each page. This parameter overrides the default value, specifying the desired number of videos to return on each page.

Below is the code for reference:

app.get("/indexes/:indexId/videos", async (request, response, next) => {
  const params = {
    page_limit: request.query.page_limit,
  };

  try {
    const options = {
      method: "GET",
      url: `${API_BASE_URL}/indexes/${request.params.indexId}/videos`,
      headers: { ...HEADERS },
      data: { params },
    };
    const apiResponse = await axios.request(options);
    response.json(apiResponse.data);
  } catch (error) {
    const status = error.response?.status || 500;
    const message = error.response?.data?.message || "Error Getting Videos";
    return next({ status, message });
  }
});

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

Retrieve information about a video

To retrieve information about a video uploaded to the platform, the application invokes GET method of the /indexes/{index-id}/videos/{video-id} endpoint.

Below is the code for reference:

app.get(
  "/indexes/:indexId/videos/:videoId",
  async (request, response, next) => {
    const indexId = request.params.indexId;
    const videoId = request.params.videoId;

    try {
      const options = {
        method: "GET",
        url: `${API_BASE_URL}/indexes/${indexId}/videos/${videoId}`,
        headers: { ...HEADERS },
      };
      const apiResponse = await axios.request(options);
      response.json(apiResponse.data);
    } catch (error) {
      const status = error.response?.status || 500;
      const message = error.response?.data?.message || "Error Getting a Video";
      return next({ status, message });
    }
  }
);

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

Upload a video

To upload videos, the application invokes the POST method of the /tasks/external-provider endpoint with the following parameters and values:

Parameter and valuePurpose
index_id = indexSpecifies the index to which the video is being uploaded.
url = taskVideo.video_urlRepresents the URL of the video to be uploaded.

Below is the code for reference:

async function indexYouTubeVideo() {
  if (taskVideo) {
    try {
      const data = {
        index_id: index,
        url: taskVideo.video_url,
      };
      const response = await axios.post(INDEX_VIDEO.toString(), {
        headers: {
          "content-type": "application/json",
        },
        body: data,
      });
      const taskId = response.data._id;
      setTaskId(taskId);
    } catch (error) {
      setError(error.message);
    }
  }
}

For a description of each field in the request and response, see the API Reference > Create a video indexing task page.

Check the status of a video indexing task

A video indexing task represents a request to upload and index a video. To check the status of a video indexing task, the application invokes the GET method of the /tasks/{task_id} endpoint.

Below is the code for reference:

app.get("/tasks/:taskId", async (request, response, next) => {
  const taskId = request.params.taskId;

  try {
    const options = {
      method: "GET",
      url: `${API_BASE_URL}/tasks/${taskId}`,
      headers: { ...HEADERS },
    };
    const apiResponse = await axios.request(options);
    response.json(apiResponse.data);
  } catch (error) {
    const status = error.response?.status || 500;
    const message = error.response?.data?.message || "Error getting a task";
    return next({ status, message });
  }
});

For a description of each field in the request and response, see the API Reference > Retrieve a video indexing task page.

Generate summaries, chapters, or highlights

To generate summaries, chapters, or highlights based on a video, the application invokes the POST method of the /summarize endpoint with the following parameters and values:

Parameter and valuePurpose
type = request.body.dataSpecifies the type of text the platform must generate. The following values are supported:
- summary
- chapter
- highlight.
See the API Reference > Summaries, chapters, or highlights page for details.
video_id = videoIdThe unique identifier of the video.

Below is the code for reference:

/** Summarize a video */
app.post("/videos/:videoId/summarize", async (request, response, next) => {
  const videoId = request.params.videoId;
  let type = request.body.data;

  try {
    const options = {
      method: "POST",
      url: `${API_BASE_URL}/summarize`,
      headers: { ...HEADERS, accept: "application/json" },
      data: { ...type, video_id: videoId },
    };
    const apiResponse = await axios.request(options);
    response.json(apiResponse.data);
  } catch (error) {
    const status = error.response?.status || 500;
    const message =
      error.response?.data?.message || "Error Summarizing a Video";
    return next({ status, message });
  }
});

For a description of each field in the request and response, see the API Reference > Generate summaries, chapters, or highlights page.

Next steps

After you have completed this tutorial, you have several options:

  • Use the application as-is: Inspect the source code to understand the platform's features and start using the application immediately.
  • Customize and enhance the application: This application is a foundation for various projects. Modify the code to meet your specific needs, from adjusting the user interface to adding new features or integrating more endpoints.
  • Explore further: Try the applications built by the community or the partner integrations to get more insights into the Twelve Labs Video Understanding Platform's diverse capabilities and learn more about integrating the platform into your applications.