ナビゲーション

Go

Go で動画をレンダリングするBeta

Go SDK で、動画テンプレートを MP4 としてレンダリングし、その進行状況を追います。

動画テンプレートは MP4 としてレンダリングされます。動画の 1 秒が 1 回の生成として数えられ、60 fps では 2 回として数えられます。動画のデザイン方法は、動画の生成で説明しています。

RenderVideo と RenderAndWaitVideo は、コンテキスト、テンプレート ID、modifications を受け取ります。

#templateIDstringrequired

動画テンプレートの ID。たとえば tpl_xxx です。

#modifications[]bannerify.Modification

テンプレートのレイヤーに設定する値。動画レイヤーは Src を、音声トラックは名前と src を受け取ります。各レイヤーが受け付けるフィールドは、Modifications にあります。

レンダリングして URL を待つ

RenderAndWaitVideo は、MP4 の URL を返します。エラーは *ErrorResponse です。

headline := "Summer sale"
url, err := client.RenderAndWaitVideo(ctx, "tpl_xxx", []bannerify.Modification{
	{Name: "headline", Text: &headline},
})
if err != nil {
	log.Fatal(err)
}

fmt.Println(url)

進行状況を追う

RenderVideo は iter.Seq[RenderVideoEvent] を返します。最後のイベントは done または error です。

for event := range client.RenderVideo(ctx, "tpl_xxx", nil) {
	switch event.Type {
	case "queued":
		fmt.Printf("%d videos before this one\n", event.Ahead)
	case "progress":
		fmt.Printf("%.0f%%\n", event.Progress*100)
	case "done":
		fmt.Println(event.URL)
	case "error":
		fmt.Println(event.Error)
	}
}
イベント フィールド
created ID: 動画の ID (例: vid_xxx)
queued Ahead: この動画より前にある動画の数
progress Frame、TotalFrames、0 から 1 までの Progress
done URL: MP4 の URL
error Error: Code、Message、RequestID

無効な API キーやクォータの使い切りなどでリクエストが拒否されると、error イベントが 1 つ返ります。動画は数分間順番を待つことがあるため、クライアントのタイムアウトはレンダリングには適用されません。止めるには ctx をキャンセルします。

接続が切れたときに再接続する

接続が切れても、動画は最後までレンダリングされます。そのあと RenderVideo は、created イベントの ID で同じ動画をもう一度読み取り、イベントを返し続けます。レンダリングのリクエストを再送することはないので、動画 2 本分の料金はかかりません。

試行 試行前の待ち時間
1 1 秒
2 2 秒
3 4 秒
4 8 秒

試行回数を使い切った場合や、created イベントより前に接続が切れた場合は、最後のイベントがコード TIMEOUT_ERROR の error になります。RenderAndWaitVideo も同じように再接続します。

待たずに動画を開始する

CreateVideo は動画を開始し、その ID をすぐに返します。レンダリングの完了は待ちません。そのあと、GetVideo または WatchVideo で動画を読み取ります。エラーは *ErrorResponse です。

created, err := client.CreateVideo(ctx, "tpl_xxxxxxxxx", nil)
if err != nil {
	log.Fatal(err)
}

fmt.Println(created.ID) // vid_xxx, with the status queued

CreateVideo がレンダリングを送信するのは 1 回だけです。もう一度リクエストすると、2 本目の動画がレンダリングされます。

動画をもう一度読み取る

created イベントの ID を保存しておいてください。GetVideo は、動画のステータスを返し、完了していれば URL も返します。エラーは *ErrorResponse です。

video, err := client.GetVideo(ctx, "vid_xxx")
if err != nil {
	log.Fatal(err)
}

fmt.Println(video.Status, video.URL)

Status は queued、rendering、done、failed のいずれかです。失敗した動画には Error の文字列があります。未知の ID を指定すると、コード NOT_FOUND のエラーになります。

WatchVideo は、動画の最新のイベントから、もう一度イベントを返します。created イベントはなく、RenderVideo と同じように再接続します。

for event := range client.WatchVideo(ctx, "vid_xxx") {
	if event.Type == "done" {
		fmt.Println(event.URL)
	}
}