Using DeepFakeAI API to generate videos with NodeJS

Get API Token

You will need to an API token to start using FakeAI API. You can get one from Dashboard

Get characters

You can get a list of default characters by calling the https://os01.fakeai.io/api/characters endpoint.

You can also get a list of community made characters by calling the https://os01.fakeai.io/api/custom-explore endpoint.

Please note that the API endpoints do not require an API token and responds an array of characters in different structures.

For the /characters endpoint, the response is an array of object with the following strutcure.

[
  {
    "name": "Character Name",
    "image": "image.png",
    "code": "character_code"
  }
]

For the /custom-explore endpoint, the response is an object with the following structure.

{
  "characters": [
    {
      "name": "Character Name",
      "video_path": "video_path.mp4",
      "usable_name": "character_code",
      "training_data": {
            "hours": 0,
            "minutes": 0
      },
      "preview_video_url": "preview_video_url.mp4",
      "thumbnail_url": "thumbnail_url.png",
      "creator": "creator",
      "audio_hours": {
            "hours": 0,
            "minutes": 0
      },
      "upvotes": 0,
      "downvotes": 0
    }
 ],
 "success": true
}

The main focus in the both of the responses is the `code` or `usable_name` field. This field is used to identify the character in the API.

Generate Videos

Once you have the API token, you can start using the API to generate videos.

The API endpoint to generate videos is `https://os01.fakeai.io/api/generate`. The endpoint requires the following parameters.

  • key: Your API token
  • character: The character code
  • query: The text to be spoken by the character or query to be answered by the character
  • type: The type of the video to be generated. The value can be say or ask

Example Request

const generateVideo = async () => {

    const url = new URL("https://os01.fakeai.io/api/generate");

    const params = {
        key: "JaIOmyJifdIWonKby3YogiD3mKBZwQVQ",
        character: "tate",
        query: "Hello world!",
        type: "say",
    };

    Object.keys(params).forEach((key) => url.searchParams.append(key, params[key]));

    const request = await fetch(url.toString(), {
        method: "GET",
    });

    const response = await request.json();

    return response;

};

generateVideo().then(console.log);

The response is an API is an object with the following structure.

{
 "id": "video_id"
}

Polling for Video Status

Once you have the video ID, you can poll the API to get the status of the video. The API endpoint to get the status of the video is `https://os01.fakeai.io/api/status/{id}`.

const poll = async (id) => {

    const url = `https://os01.fakeai.io/api/check/${id}`;

    const request = await fetch(url, {
        method: "GET",
    });

    const response = await request.json();

    return response;

};

poll("video_id").then(console.log);

The response is an object with the following structure.

{
 "video": null,
 "status": "processing",
 "message": "Generating video... (May take upto 15 mins)"
}

Note: The first request to the API may respond with an error status 404 (video not found). This is because the video may not have been sent to the processing queue. You may add a delay of 2 seconds before polling the API.

The status field can have the following values.

  • processing: The video is being generated
  • success: The video has been generated
  • error: There was an error generating the video, check the message field for more information.

Get Video

Once the video is generated, you will get a response with the video URL. You can use the URL to download the video.

{
 "video": "video_url",
 "status": "success",
 "message": "Video generated successfully"
}

Putting it all together

Here’s an example of generating a video using the API and polling for the status of the video.

We use Andrew Tate as the character and make him say “Hello world!”. Dont forget to replace `your_api_token` with your API token.

const generateVideo = async () => {

    const url = new URL("https://os01.fakeai.io/api/generate");

    const params = {
        key: "your_api_token",
        character: "tate",
        query: "Hello world!",
        type: "say",
    };

    Object.keys(params).forEach((key) => url.searchParams.append(key, params[key]));

    const request = await fetch(url.toString(), {
        method: "GET",
    });

    const response = await request.json();

    return response;

};

const poll = async (id) => {

    const url = `https://os01.fakeai.io/api/check/${id}`;

    const request = await fetch(url, {
        method: "GET",
    });

    const response = await request.json();

    return response;

};

const main = async () => {

    const video = await generateVideo();

    let response = await poll(video.id);

    while (response.status !== "success") {
        response = await poll(video.id);
        await new Promise((resolve) => setTimeout(resolve, 1000));
    }

    console.log(response.video);

};

main();

Conclusion

This is a simple example of how to use the FakeAI API to generate videos. You can use the API to generate videos with different characters and queries. You can also use the API to generate videos with custom characters. The API is easy to use and can be integrated into any application that requires video generation.

For more information on the API, please do not hesitate to ask questions in the telegram group.


Comments

Leave a Reply

Your email address will not be published. Required fields are marked *