スレートを作成する

スレートとは、ライブ ストリームの広告ブレークの中で、動的配信の広告を表示できるだけの時間が残っていない場合に配信できるコンテンツのことです。そのような時間にある間、スレートを作成してコンテンツを配信します。

もっと見る

このコードサンプルを含む詳細なドキュメントについては、以下をご覧ください。

コードサンプル

C#

このサンプルを試す前に、クライアント ライブラリを使用した Video Stitcher API クイックスタートにある C# の設定手順を実施してください。詳細については、Video Stitcher API C# API リファレンス ドキュメントをご覧ください。

Video Stitcher API への認証を行うには、アプリケーションのデフォルト認証情報を設定します。 詳細については、ローカル開発環境の認証の設定をご覧ください。


using Google.Api.Gax.ResourceNames;
using Google.Cloud.Video.Stitcher.V1;
using Google.LongRunning;
using System.Threading.Tasks;

public class CreateSlateSample
{
    public async Task<Slate> CreateSlateAsync(
        string projectId, string location, string slateId, string slateUri)
    {
        // Create the client.
        VideoStitcherServiceClient client = VideoStitcherServiceClient.Create();

        CreateSlateRequest request = new CreateSlateRequest
        {
            ParentAsLocationName = LocationName.FromProjectLocation(projectId, location),
            SlateId = slateId,
            Slate = new Slate
            {
                Uri = slateUri
            }
        };

        // Make the request.
        Operation<Slate, OperationMetadata> response = await client.CreateSlateAsync(request);

        // Poll until the returned long-running operation is complete.
        Operation<Slate, OperationMetadata> completedResponse = await response.PollUntilCompletedAsync();

        // Retrieve the operation result.
        return completedResponse.Result;
    }
}

Go

このサンプルを試す前に、クライアント ライブラリを使用した Video Stitcher API クイックスタートにある Go の設定手順を実施してください。詳細については、Video Stitcher API Go API リファレンス ドキュメントをご覧ください。

Video Stitcher API への認証を行うには、アプリケーションのデフォルト認証情報を設定します。 詳細については、ローカル開発環境の認証の設定をご覧ください。

import (
	"context"
	"fmt"
	"io"

	stitcher "cloud.google.com/go/video/stitcher/apiv1"
	stitcherstreampb "cloud.google.com/go/video/stitcher/apiv1/stitcherpb"
)

// createSlate creates a slate. A slate is displayed when ads are not available.
func createSlate(w io.Writer, projectID, slateID, slateURI string) error {
	// projectID := "my-project-id"
	// slateID := "my-slate-id"
	// slateURI := "https://my-slate-uri/test.mp4"
	location := "us-central1"
	ctx := context.Background()
	client, err := stitcher.NewVideoStitcherClient(ctx)
	if err != nil {
		return fmt.Errorf("stitcher.NewVideoStitcherClient: %w", err)
	}
	defer client.Close()

	req := &stitcherstreampb.CreateSlateRequest{
		Parent:  fmt.Sprintf("projects/%s/locations/%s", projectID, location),
		SlateId: slateID,
		Slate: &stitcherstreampb.Slate{
			Uri: slateURI,
		},
	}
	// Creates the slate.
	op, err := client.CreateSlate(ctx, req)
	if err != nil {
		return fmt.Errorf("client.CreateSlate: %w", err)
	}
	response, err := op.Wait(ctx)
	if err != nil {
		return err
	}

	fmt.Fprintf(w, "Slate: %v", response.GetName())
	return nil
}

Java

このサンプルを試す前に、クライアント ライブラリを使用した Video Stitcher API クイックスタートにある Java の設定手順を実施してください。詳細については、Video Stitcher API Java API リファレンス ドキュメントをご覧ください。

Video Stitcher API への認証を行うには、アプリケーションのデフォルト認証情報を設定します。 詳細については、ローカル開発環境の認証の設定をご覧ください。


import com.google.cloud.video.stitcher.v1.CreateSlateRequest;
import com.google.cloud.video.stitcher.v1.LocationName;
import com.google.cloud.video.stitcher.v1.Slate;
import com.google.cloud.video.stitcher.v1.VideoStitcherServiceClient;
import java.io.IOException;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

public class CreateSlate {

  private static final int TIMEOUT_IN_MINUTES = 2;

  public static void main(String[] args) throws Exception {
    // TODO(developer): Replace these variables before running the sample.
    String projectId = "my-project-id";
    String location = "us-central1";
    String slateId = "my-slate-id";
    String slateUri =
        "https://my-slate-uri/test.mp4"; // URI of an MP4 video with at least one audio track

    createSlate(projectId, location, slateId, slateUri);
  }

  public static void createSlate(String projectId, String location, String slateId, String slateUri)
      throws IOException, ExecutionException, InterruptedException, TimeoutException {
    // Initialize client that will be used to send requests. This client only needs to be created
    // once, and can be reused for multiple requests. After completing all of your requests, call
    // the "close" method on the client to safely clean up any remaining background resources.
    VideoStitcherServiceClient videoStitcherServiceClient = VideoStitcherServiceClient.create();
    CreateSlateRequest createSlateRequest =
        CreateSlateRequest.newBuilder()
            .setParent(LocationName.of(projectId, location).toString())
            .setSlateId(slateId)
            .setSlate(Slate.newBuilder().setUri(slateUri).build())
            .build();

    Slate response =
        videoStitcherServiceClient
            .createSlateAsync(createSlateRequest)
            .get(TIMEOUT_IN_MINUTES, TimeUnit.MINUTES);
    System.out.println("Created new slate: " + response.getName());
    videoStitcherServiceClient.close();
  }
}

Node.js

このサンプルを試す前に、クライアント ライブラリを使用した Video Stitcher API クイックスタートにある Node.js の設定手順を実施してください。詳細については、Video Stitcher API Node.js API リファレンス ドキュメントをご覧ください。

Video Stitcher API への認証を行うには、アプリケーションのデフォルト認証情報を設定します。 詳細については、ローカル開発環境の認証の設定をご覧ください。

/**
 * TODO(developer): Uncomment these variables before running the sample.
 */
// projectId = 'my-project-id';
// location = 'us-central1';
// slateId = 'my-slate';
// slateUri = 'https://my-slate-uri/test.mp4';

// Imports the Video Stitcher library
const {VideoStitcherServiceClient} =
  require('@google-cloud/video-stitcher').v1;
// Instantiates a client
const stitcherClient = new VideoStitcherServiceClient();

async function createSlate() {
  // Construct request
  const request = {
    parent: stitcherClient.locationPath(projectId, location),
    slate: {
      uri: slateUri,
    },
    slateId: slateId,
  };
  const [operation] = await stitcherClient.createSlate(request);
  const [response] = await operation.promise();
  console.log(`response.name: ${response.name}`);
}

createSlate();

PHP

このサンプルを試す前に、クライアント ライブラリを使用した Video Stitcher API クイックスタートにある PHP の設定手順を実施してください。詳細については、Video Stitcher API PHP API リファレンス ドキュメントをご覧ください。

Video Stitcher API への認証を行うには、アプリケーションのデフォルト認証情報を設定します。 詳細については、ローカル開発環境の認証の設定をご覧ください。

use Google\Cloud\Video\Stitcher\V1\Client\VideoStitcherServiceClient;
use Google\Cloud\Video\Stitcher\V1\CreateSlateRequest;
use Google\Cloud\Video\Stitcher\V1\Slate;

/**
 * Creates a slate. A slate is displayed when ads are not available.
 *
 * @param string $callingProjectId     The project ID to run the API call under
 * @param string $location             The location of the slate
 * @param string $slateId              The name of the slate to be created
 * @param string $slateUri             The public URI for an MP4 video with at least one audio track
 */
function create_slate(
    string $callingProjectId,
    string $location,
    string $slateId,
    string $slateUri
): void {
    // Instantiate a client.
    $stitcherClient = new VideoStitcherServiceClient();

    $parent = $stitcherClient->locationName($callingProjectId, $location);
    $slate = new Slate();
    $slate->setUri($slateUri);

    // Run slate creation request
    $request = (new CreateSlateRequest())
        ->setParent($parent)
        ->setSlateId($slateId)
        ->setSlate($slate);
    $operationResponse = $stitcherClient->createSlate($request);
    $operationResponse->pollUntilComplete();
    if ($operationResponse->operationSucceeded()) {
        $result = $operationResponse->getResult();
        // Print results
        printf('Slate: %s' . PHP_EOL, $result->getName());
    } else {
        $error = $operationResponse->getError();
        // handleError($error)
    }
}

Python

このサンプルを試す前に、クライアント ライブラリを使用した Video Stitcher API クイックスタートにある Python の設定手順を実施してください。詳細については、Video Stitcher API Python API リファレンス ドキュメントをご覧ください。

Video Stitcher API への認証を行うには、アプリケーションのデフォルト認証情報を設定します。 詳細については、ローカル開発環境の認証の設定をご覧ください。


import argparse

from google.cloud.video import stitcher_v1
from google.cloud.video.stitcher_v1.services.video_stitcher_service import (
    VideoStitcherServiceClient,
)

def create_slate(
    project_id: str, location: str, slate_id: str, slate_uri: str
) -> stitcher_v1.types.Slate:
    """Creates a slate.
    Args:
        project_id: The GCP project ID.
        location: The location in which to create the slate.
        slate_id: The user-defined slate ID.
        slate_uri: Uri of the video slate; must be an MP4 video with at least one audio track.

    Returns:
        The slate resource.
    """

    client = VideoStitcherServiceClient()

    parent = f"projects/{project_id}/locations/{location}"

    slate = stitcher_v1.types.Slate(
        uri=slate_uri,
    )

    operation = client.create_slate(parent=parent, slate_id=slate_id, slate=slate)
    response = operation.result()
    print(f"Slate: {response.name}")
    return response

Ruby

このサンプルを試す前に、クライアント ライブラリを使用した Video Stitcher API クイックスタートにある Ruby の設定手順を実施してください。詳細については、Video Stitcher API Ruby API リファレンス ドキュメントをご覧ください。

Video Stitcher API への認証を行うには、アプリケーションのデフォルト認証情報を設定します。 詳細については、ローカル開発環境の認証の設定をご覧ください。

require "google/cloud/video/stitcher"

##
# Create a slate
#
# @param project_id [String] Your Google Cloud project (e.g. "my-project")
# @param location [String] The location (e.g. "us-central1")
# @param slate_id [String] Your slate name (e.g. "my-slate")
# @param slate_uri [String] The URI of an MP4 video with at least one audio
#   track (e.g. "https://my-slate-uri/test.mp4")
#
def create_slate project_id:, location:, slate_id:, slate_uri:
  # Create a Video Stitcher client.
  client = Google::Cloud::Video::Stitcher.video_stitcher_service

  # Build the resource name of the parent.
  parent = client.location_path project: project_id, location: location

  # Set the slate fields.
  new_slate = {
    uri: slate_uri
  }

  operation = client.create_slate parent: parent, slate_id: slate_id,
                                  slate: new_slate

  # The returned object is of type Gapic::Operation. You can use this
  # object to check the status of an operation, cancel it, or wait
  # for results. Here is how to block until completion:
  operation.wait_until_done!

  # Print the slate name.
  puts "Slate: #{operation.response.name}"
end

次のステップ

他の Google Cloud プロダクトに関連するコードサンプルの検索およびフィルタ検索を行うには、Google Cloud のサンプルをご覧ください。