👻

【Luma AI】Video-to-3D AI APIをNodeJSで使う方法

2023/03/28に公開

こんにちは!
本記事ではLuma AIが提供している「Video-to-3D AI」の API の使用方法をメモがてら記載します。

2023 年 3 月 38 日(日本時間)Luma AI の公式 Twitter より API が公開されました。

Luma AI API Key の発行方法

まずは API Key を発行する必要があります。
こちらにアクセスし、以下の画像の場所に API Key があります。
この Key を保存しましょう。

Luma AI API の使い方

公式サイトのチュートリアルにも記載がありますが、3D モデル化の手順はこちらです。

  1. Create Capture.
  2. Upload Video File.
  3. Trigger Capture.

一つ一つソースコードを使って紹介します。

1. Create Capture.

まずは、キャプチャーを作成します。

const resp = await axios.post(
  "https://webapp.engineeringlumalabs.com/api/v2/capture",
  stringify({
    title: "キャプチャーのタイトル",
  }),
  {
    headers: {
      Authorization: "luma-api-key={API Key}",
    },
  }
);

return resp.data;

こちらのタイトルは任意に決めることができ、覚えやすいタイトルにすると良いでしょう。
resp.data内に、capture.slugsignedUrls.sourceフィールドがあり、こちらは今後必要になります。

2. Upload Video File.

次は、作成したキャプチャーが生成したsignedUrls.source(Cloud Storage URL)データを上書き処理をします。

const resp = await axios.put({signedUrls.source}, {bufferData}, {
  headers: {
    "Content-Type": "text/plain",
  },
  maxBodyLength: Infinity,
});

bufferDataには Buffer 形式のデータを渡しましょう。

3. Trigger Capture.

最後に、キャプチャをトリガー処理します。

const resp = await axios.post(
  `https://webapp.engineeringlumalabs.com/api/v2/capture${slug}`,
  {},
  {
    maxBodyLength: Infinity,
    headers: {
      Authorization: "luma-api-key={API Key}",
    },
  }
);

slugには、1 のキャプチャー作成時に返ってきた値を活用します。

以上 3 つの処理で 3D 変換が開始されます。

ソースコード全体

Class 化して使いやすくしたものを以下に記載します。

import "dotenv/config";
import axios from "axios";
import { stringify } from "querystring";

const LUMA_API_KEY = String(process.env.LUMA_API_KEY);

interface Credit {
  remaining: number;
  used: number;
  total: number;
}

interface CreateCaptureProps {
  title: string;
}

interface CreateCapture {
  signedUrls: {
    source: string;
  };
  capture: Capture;
}

interface UploadProps {
  url: string;
  bufferData: Buffer;
}

interface TriggerCaptureProps {
  slug: string;
}

interface GetCaptureProps {
  slug: string;
}

interface Capture {
  title: string;
  type: string;
  location: string | null;
  date: string;
  username: string;
  status: string; // 'uploading' | 'completed'
  slug: string;
}

class LumaClient {
  private BASE_URL = "https://webapp.engineeringlumalabs.com/api/v2";

  constructor(private apiKey: string) {}

  private getHeaders() {
    return {
      Authorization: `luma-api-key=${this.apiKey}`,
    };
  }

  async getCredit(): Promise<Credit> {
    const resp = await axios.get(this.BASE_URL + "/capture/credits", {
      headers: this.getHeaders(),
    });

    return resp.data;
  }

  async createCapture(props: CreateCaptureProps): Promise<CreateCapture> {
    const resp = await axios.post(
      this.BASE_URL + "/capture",
      stringify({
        title: props.title,
      }),
      {
        headers: this.getHeaders(),
      }
    );

    if (resp.status !== 200) {
      throw new Error("status is not 200.");
    }

    return resp.data;
  }

  async upload(props: UploadProps) {
    const resp = await axios.put(props.url, props.bufferData, {
      headers: {
        "Content-Type": "text/plain",
      },
      maxBodyLength: Infinity,
    });

    if (resp.status !== 200) {
      throw new Error("status is not 200.");
    }
  }

  async triggerCapture(props: TriggerCaptureProps): Promise<Capture> {
    const resp = await axios.post(
      this.BASE_URL + `/capture/${props.slug}`,
      {},
      { maxBodyLength: Infinity, headers: this.getHeaders() }
    );

    if (resp.status !== 200) {
      throw new Error("status is not 200.");
    }

    return resp.data;
  }

  async getCapture(props: GetCaptureProps): Promise<Capture> {
    const resp = await axios.get(this.BASE_URL + `/capture/${props.slug}`, {
      maxBodyLength: Infinity,
      headers: this.getHeaders(),
    });

    if (resp.status !== 200) {
      throw new Error("status is not 200.");
    }

    return resp.data;
  }
}

余談

この API を活用して、LINE Bot アプリを作りました。
リリース予定はありません。

まとめ

今回は Luma AI API を NodeJS で利用する方法を紹介しました。
本記事が、皆さんのビジネスに役立つことを祈っております!

GitHubで編集を提案

Discussion