创建实时配置以应用于实时会话。
深入探索
如需查看包含此代码示例的详细文档,请参阅以下内容:
代码示例
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 CreateLiveConfigSample
{
public async Task<LiveConfig> CreateLiveConfigAsync(
string projectId, string location, string liveConfigId, string sourceUri, string adTagUri, string slateId)
{
// Create the client.
VideoStitcherServiceClient client = VideoStitcherServiceClient.Create();
CreateLiveConfigRequest request = new CreateLiveConfigRequest
{
ParentAsLocationName = LocationName.FromProjectLocation(projectId, location),
LiveConfigId = liveConfigId,
LiveConfig = new LiveConfig
{
SourceUri = sourceUri,
AdTagUri = adTagUri,
DefaultSlate = SlateName.FormatProjectLocationSlate(projectId, location, slateId),
AdTracking = AdTracking.Server,
StitchingPolicy = LiveConfig.Types.StitchingPolicy.CutCurrent
}
};
// Make the request.
Operation<LiveConfig, OperationMetadata> response = await client.CreateLiveConfigAsync(request);
// Poll until the returned long-running operation is complete.
Operation<LiveConfig, 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"
)
// createLiveConfig creates a live config. Live configs are used to configure live sessions.
func createLiveConfig(w io.Writer, projectID, liveConfigID, sourceURI, slateID string) error {
// projectID := "my-project-id"
// liveConfigID := "my-live-config-id"
// Uri of the media to stitch; this URI must reference either an MPEG-DASH
// manifest (.mpd) file or an M3U playlist manifest (.m3u8) file.
// sourceURI := "https://storage.googleapis.com/my-bucket/main.mpd"
// See https://cloud.google.com/video-stitcher/docs/concepts for information
// on ad tags and ad metadata. This sample uses an ad tag URL that displays
// a Single Inline Linear ad
// (https://developers.google.com/interactive-media-ads/docs/sdks/html5/client-side/tags).
adTagURI := "https://pubads.g.doubleclick.net/gampad/ads?iu=/21775744923/external/single_ad_samples&sz=640x480&cust_params=sample_ct%3Dlinear&ciu_szs=300x250%2C728x90&gdfp_req=1&output=vast&unviewed_position_start=1&env=vp&impl=s&correlator="
// slateID := "my-slate-id"
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.CreateLiveConfigRequest{
Parent: fmt.Sprintf("projects/%s/locations/%s", projectID, location),
LiveConfigId: liveConfigID,
LiveConfig: &stitcherstreampb.LiveConfig{
SourceUri: sourceURI,
AdTagUri: adTagURI,
AdTracking: stitcherstreampb.AdTracking_SERVER,
StitchingPolicy: stitcherstreampb.LiveConfig_CUT_CURRENT,
DefaultSlate: fmt.Sprintf("projects/%s/locations/%s/slates/%s", projectID, location, slateID),
},
}
// Creates the live config.
op, err := client.CreateLiveConfig(ctx, req)
if err != nil {
return fmt.Errorf("client.createLiveConfig: %w", err)
}
response, err := op.Wait(ctx)
if err != nil {
return err
}
fmt.Fprintf(w, "Live config: %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.AdTracking;
import com.google.cloud.video.stitcher.v1.CreateLiveConfigRequest;
import com.google.cloud.video.stitcher.v1.LiveConfig;
import com.google.cloud.video.stitcher.v1.LiveConfig.StitchingPolicy;
import com.google.cloud.video.stitcher.v1.LocationName;
import com.google.cloud.video.stitcher.v1.SlateName;
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 CreateLiveConfig {
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 liveConfigId = "my-live-config-id";
// Uri of the live stream to stitch; this URI must reference either an MPEG-DASH
// manifest (.mpd) file or an M3U playlist manifest (.m3u8) file.
String sourceUri = "https://storage.googleapis.com/my-bucket/main.mpd";
// See Single Inline Linear
// (https://developers.google.com/interactive-media-ads/docs/sdks/html5/client-side/tags)
String adTagUri = "https://pubads.g.doubleclick.net/gampad/ads...";
String slateId = "my-slate-id";
createLiveConfig(projectId, location, liveConfigId, sourceUri, adTagUri, slateId);
}
public static void createLiveConfig(
String projectId,
String location,
String liveConfigId,
String sourceUri,
String adTagUri,
String slateId)
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();
CreateLiveConfigRequest createLiveConfigRequest =
CreateLiveConfigRequest.newBuilder()
.setParent(LocationName.of(projectId, location).toString())
.setLiveConfigId(liveConfigId)
.setLiveConfig(
LiveConfig.newBuilder()
.setSourceUri(sourceUri)
.setAdTagUri(adTagUri)
.setDefaultSlate(SlateName.format(projectId, location, slateId))
.setAdTracking(AdTracking.SERVER)
.setStitchingPolicy(StitchingPolicy.CUT_CURRENT)
.build())
.build();
LiveConfig response =
videoStitcherServiceClient
.createLiveConfigAsync(createLiveConfigRequest)
.get(TIMEOUT_IN_MINUTES, TimeUnit.MINUTES);
System.out.println("Created new live config: " + 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';
// liveConfigId = 'my-live-config-id';
// sourceUri = 'https://storage.googleapis.com/my-bucket/main.mpd';
// See Single Inline Linear
// (https://developers.google.com/interactive-media-ads/docs/sdks/html5/client-side/tags)
// adTagUri = 'https://pubads.g.doubleclick.net/gampad/ads...';
// slateId = 'my-slate';
// Imports the Video Stitcher library
const {VideoStitcherServiceClient} =
require('@google-cloud/video-stitcher').v1;
// Instantiates a client
const stitcherClient = new VideoStitcherServiceClient();
async function createLiveConfig() {
// Construct request
const request = {
parent: stitcherClient.locationPath(projectId, location),
liveConfig: {
sourceUri: sourceUri,
adTagUri: adTagUri,
adTracking: 'SERVER',
stitchingPolicy: 'CUT_CURRENT',
defaultSlate: stitcherClient.slatePath(projectId, location, slateId),
},
liveConfigId: liveConfigId,
};
const [operation] = await stitcherClient.createLiveConfig(request);
const [response] = await operation.promise();
console.log(`response.name: ${response.name}`);
}
createLiveConfig();
PHP
在试用此示例之前,请按照 Video Stitcher API 快速入门:使用客户端库中的 PHP 设置说明进行操作。 如需了解详情,请参阅 Video Stitcher API PHP API 参考文档。
如需向 Video Stitcher API 进行身份验证,请设置应用默认凭据。如需了解详情,请参阅为本地开发环境设置身份验证。
use Google\Cloud\Video\Stitcher\V1\AdTracking;
use Google\Cloud\Video\Stitcher\V1\Client\VideoStitcherServiceClient;
use Google\Cloud\Video\Stitcher\V1\CreateLiveConfigRequest;
use Google\Cloud\Video\Stitcher\V1\LiveConfig;
/**
* Creates a live config. Live configs are used to configure live sessions.
*
* @param string $callingProjectId The project ID to run the API call under
* @param string $location The location of the live config
* @param string $liveConfigId The name of the live config to be created
* @param string $sourceUri Uri of the media to stitch; this URI must
* reference either an MPEG-DASH manifest
* (.mpd) file or an M3U playlist manifest
* (.m3u8) file.
* @param string $adTagUri The Uri of the ad tag
* @param string $slateId The user-defined slate ID of the default
* slate to use when no slates are specified
* in an ad break's message
*/
function create_live_config(
string $callingProjectId,
string $location,
string $liveConfigId,
string $sourceUri,
string $adTagUri,
string $slateId
): void {
// Instantiate a client.
$stitcherClient = new VideoStitcherServiceClient();
$parent = $stitcherClient->locationName($callingProjectId, $location);
$defaultSlate = $stitcherClient->slateName($callingProjectId, $location, $slateId);
$liveConfig = (new LiveConfig())
->setSourceUri($sourceUri)
->setAdTagUri($adTagUri)
->setAdTracking(AdTracking::SERVER)
->setStitchingPolicy(LiveConfig\StitchingPolicy::CUT_CURRENT)
->setDefaultSlate($defaultSlate);
// Run live config creation request
$request = (new CreateLiveConfigRequest())
->setParent($parent)
->setLiveConfigId($liveConfigId)
->setLiveConfig($liveConfig);
$operationResponse = $stitcherClient->createLiveConfig($request);
$operationResponse->pollUntilComplete();
if ($operationResponse->operationSucceeded()) {
$result = $operationResponse->getResult();
// Print results
printf('Live config: %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_live_config(
project_id: str,
location: str,
live_config_id: str,
live_stream_uri: str,
ad_tag_uri: str,
slate_id: str,
) -> stitcher_v1.types.LiveConfig:
"""Creates a live config.
Args:
project_id: The GCP project ID.
location: The location in which to create the live config.
live_config_id: The user-defined live config ID.
live_stream_uri: Uri of the livestream to stitch; this URI must reference either an MPEG-DASH
manifest (.mpd) file or an M3U playlist manifest (.m3u8) file.
ad_tag_uri: Uri of the ad tag.
slate_id: The user-defined slate ID of the default slate to use when no slates are specified in an ad break's message.
Returns:
The live config resource.
"""
client = VideoStitcherServiceClient()
parent = f"projects/{project_id}/locations/{location}"
default_slate = f"projects/{project_id}/locations/{location}/slates/{slate_id}"
live_config = stitcher_v1.types.LiveConfig(
source_uri=live_stream_uri,
ad_tag_uri=ad_tag_uri,
ad_tracking="SERVER",
stitching_policy="CUT_CURRENT",
default_slate=default_slate,
)
operation = client.create_live_config(
parent=parent, live_config_id=live_config_id, live_config=live_config
)
response = operation.result()
print(f"Live config: {response.name}")
return response
Ruby
在试用此示例之前,请按照 Video Stitcher API 快速入门:使用客户端库中的 Ruby 设置说明进行操作。 如需了解详情,请参阅 Video Stitcher API Ruby API 参考文档。
如需向 Video Stitcher API 进行身份验证,请设置应用默认凭据。如需了解详情,请参阅为本地开发环境设置身份验证。
require "google/cloud/video/stitcher"
##
# Create a live config
#
# @param project_id [String] Your Google Cloud project (e.g. "my-project")
# @param location [String] The location (e.g. "us-central1")
# @param live_config_id [String] Your live config name (e.g. "my-live-config")
# @param source_uri [String] Uri of the live stream to stitch
# (e.g. "https://storage.googleapis.com/my-bucket/main.mpd")
# @param ad_tag_uri [String] Uri of the ad tag
# (e.g. "https://pubads.g.doubleclick.net/gampad/ads...")
# @param slate_id [String] The default slate ID to use when no slates are
# specified in an ad break's message (e.g. "my-slate-id")
#
def create_live_config project_id:, location:, live_config_id:, source_uri:,
ad_tag_uri:, slate_id:
# 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
# Build the resource name of the default slate.
slate_name = client.slate_path project: project_id, location: location,
slate: slate_id
# Set the live config fields.
new_live_config = {
source_uri: source_uri,
ad_tag_uri: ad_tag_uri,
ad_tracking: Google::Cloud::Video::Stitcher::V1::AdTracking::SERVER,
stitching_policy: Google::Cloud::Video::Stitcher::V1::LiveConfig::StitchingPolicy::CUT_CURRENT,
default_slate: slate_name
}
operation = client.create_live_config parent: parent,
live_config_id: live_config_id,
live_config: new_live_config
# 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 live config name.
puts "Live config: #{operation.response.name}"
end
后续步骤
如需搜索和过滤其他 Google Cloud 产品的代码示例,请参阅 Google Cloud 示例浏览器。