本页介绍了如何将重叠式广告插入转码后的视频。重叠式广告由输出视频上方插入的 JPEG 图片组成,可以在指定时间段内选择性地淡入或淡出。要插入重叠式广告,请在 JobConfig
模板中使用 overlays
数组。
将图片上传到 Cloud Storage
如需开始操作,请执行以下操作,将重叠式广告图片上传到您的 Cloud Storage 存储桶:
- 在 Google Cloud 控制台中,转到 Cloud Storage 浏览器页面。
转到“Cloud Storage 浏览器”页面
- 点击存储桶的名称将其打开。
- 点击上传文件。
- 选择要从本地机器上传的 JPEG 文件。
创建重叠式广告
您可以创建两种重叠式广告:静态重叠式广告或动画重叠式广告。这两种类型的重叠式广告都使用静态图片。您可以显示或隐藏静态重叠式广告。动画重叠式广告支持图片的淡入和淡出动画。
您可以在单个输出视频中插入多个重叠式广告。
创建静态重叠式广告
在 image
对象中,使用 uri
字段指定 Cloud Storage 中的重叠式广告图片。在 resolution
对象中,设置 x 和 y 值(从 0 到 1.0)。如果值为 0,则系统会保持该尺寸的源图片分辨率;如果值为 1.0,则系统会缩放图片,使其与输出视频的尺寸相匹配。例如,使用值 x: 1
和 y:
0.5
将重叠式广告图片缩放到输出视频的完整宽度和一半高度。
在 animations
数组中,使用 x 和 y 坐标(从 0 到 1.0)创建一个 animationStatic
对象。这些坐标基于输出视频的分辨率。使用值 x: 0
和 y: 0
将叠加层的左上角放置在输出视频的左上角。使用 startTimeOffset
字段指定叠加层何时应出现在输出视频时间轴中。
要移除静态动画,请创建一个 animationEnd
对象。使用 startTimeOffset
字段指定动画应在输出视频时间轴的哪个时间点结束(即消失)。
您可以将此配置添加到作业模板中,也可以将其添加到临时作业配置中。
REST
在使用任何请求数据之前,请先进行以下替换:
- PROJECT_ID:IAM 设置中列出的 Google Cloud 项目 ID。
- LOCATION:运行作业的位置。请使用其中一个受支持的区域:
us-central1
us-west1
us-west2
us-east1
us-east4
southamerica-east1
asia-east1
asia-south1
asia-southeast1
europe-west1
europe-west2
europe-west4
- STORAGE_BUCKET_NAME:您创建的 Cloud Storage 存储桶的名称。
- STORAGE_INPUT_VIDEO:您要对其进行转码的 Cloud Storage 存储桶中的视频的名称,例如
my-vid.mp4
。此字段应考虑您在存储桶中创建的任何文件夹(例如 input/my-vid.mp4
)。
- STORAGE_INPUT_OVERLAY:您的 Cloud Storage 存储桶中用于重叠式广告的 JPEG 图片的名称,例如
my-overlay.jpg
。此字段应考虑您在存储桶中创建的任何文件夹(例如 input/my-overlay.jpg
)。
- STORAGE_OUTPUT_FOLDER:您要在其中保存已编码视频输出的 Cloud Storage 文件夹名称。
请求 JSON 正文:
{
"config": {
"inputs": [
{
"key": "input0",
"uri": "gs://STORAGE_BUCKET_NAME/STORAGE_INPUT_VIDEO"
}
],
"elementaryStreams": [
{
"key": "video-stream0",
"videoStream": {
"h264": {
"heightPixels": 360,
"widthPixels": 640,
"bitrateBps": 550000,
"frameRate": 60
}
}
},
{
"key": "audio-stream0",
"audioStream": {
"codec": "aac",
"bitrateBps": 64000
}
}
],
"muxStreams": [
{
"key": "sd",
"container": "mp4",
"elementaryStreams": [
"video-stream0",
"audio-stream0"
]
}
],
"output": {
"uri": "gs://STORAGE_BUCKET_NAME/STORAGE_OUTPUT_FOLDER/"
},
"overlays": [
{
"image": {
"uri": "gs://STORAGE_BUCKET_NAME/STORAGE_INPUT_OVERLAY",
"resolution": {
"x": 1,
"y": 0.5
},
"alpha": 1
},
"animations": [
{
"animationStatic": {
"xy": {
"x": 0,
"y": 0
},
"startTimeOffset": "0s"
}
},
{
"animationEnd": {
"startTimeOffset": "10s"
}
}
]
}
]
}
}
如需发送您的请求,请展开以下选项之一:
curl(Linux、macOS 或 Cloud Shell)
将请求正文保存在名为 request.json
的文件中。
在终端中运行以下命令,在当前目录中创建或覆盖此文件:
cat > request.json << 'EOF'
{
"config": {
"inputs": [
{
"key": "input0",
"uri": "gs://STORAGE_BUCKET_NAME/STORAGE_INPUT_VIDEO"
}
],
"elementaryStreams": [
{
"key": "video-stream0",
"videoStream": {
"h264": {
"heightPixels": 360,
"widthPixels": 640,
"bitrateBps": 550000,
"frameRate": 60
}
}
},
{
"key": "audio-stream0",
"audioStream": {
"codec": "aac",
"bitrateBps": 64000
}
}
],
"muxStreams": [
{
"key": "sd",
"container": "mp4",
"elementaryStreams": [
"video-stream0",
"audio-stream0"
]
}
],
"output": {
"uri": "gs://STORAGE_BUCKET_NAME/STORAGE_OUTPUT_FOLDER/"
},
"overlays": [
{
"image": {
"uri": "gs://STORAGE_BUCKET_NAME/STORAGE_INPUT_OVERLAY",
"resolution": {
"x": 1,
"y": 0.5
},
"alpha": 1
},
"animations": [
{
"animationStatic": {
"xy": {
"x": 0,
"y": 0
},
"startTimeOffset": "0s"
}
},
{
"animationEnd": {
"startTimeOffset": "10s"
}
}
]
}
]
}
}
EOF
然后,执行以下命令以发送 REST 请求:
curl -X POST \
-H "Authorization: Bearer $(gcloud auth print-access-token)" \
-H "Content-Type: application/json; charset=utf-8" \
-d @request.json \
"https://transcoder.googleapis.com/v1/projects/PROJECT_ID/locations/LOCATION/jobs"
PowerShell (Windows)
将请求正文保存在名为 request.json
的文件中。
在终端中运行以下命令,在当前目录中创建或覆盖此文件:
@'
{
"config": {
"inputs": [
{
"key": "input0",
"uri": "gs://STORAGE_BUCKET_NAME/STORAGE_INPUT_VIDEO"
}
],
"elementaryStreams": [
{
"key": "video-stream0",
"videoStream": {
"h264": {
"heightPixels": 360,
"widthPixels": 640,
"bitrateBps": 550000,
"frameRate": 60
}
}
},
{
"key": "audio-stream0",
"audioStream": {
"codec": "aac",
"bitrateBps": 64000
}
}
],
"muxStreams": [
{
"key": "sd",
"container": "mp4",
"elementaryStreams": [
"video-stream0",
"audio-stream0"
]
}
],
"output": {
"uri": "gs://STORAGE_BUCKET_NAME/STORAGE_OUTPUT_FOLDER/"
},
"overlays": [
{
"image": {
"uri": "gs://STORAGE_BUCKET_NAME/STORAGE_INPUT_OVERLAY",
"resolution": {
"x": 1,
"y": 0.5
},
"alpha": 1
},
"animations": [
{
"animationStatic": {
"xy": {
"x": 0,
"y": 0
},
"startTimeOffset": "0s"
}
},
{
"animationEnd": {
"startTimeOffset": "10s"
}
}
]
}
]
}
}
'@ | Out-File -FilePath request.json -Encoding utf8
然后,执行以下命令以发送 REST 请求:
$cred = gcloud auth print-access-token
$headers = @{ "Authorization" = "Bearer $cred" }
Invoke-WebRequest `
-Method POST `
-Headers $headers `
-ContentType: "application/json; charset=utf-8" `
-InFile request.json `
-Uri "https://transcoder.googleapis.com/v1/projects/PROJECT_ID/locations/LOCATION/jobs" | Select-Object -Expand Content
您应该收到类似以下内容的 JSON 响应:
{
"name": "projects/PROJECT_NUMBER/locations/LOCATION/jobs/JOB_ID",
"config": {
...
},
"state": "PENDING",
"createTime": CREATE_TIME,
"ttlAfterCompletionDays": 30
}
gcloud CLI
- 创建一个定义作业字段的
request.json
文件。 对 gcloud
命令进行以下替换:- STORAGE_BUCKET_NAME:您创建的 Cloud Storage 存储桶的名称。
- STORAGE_INPUT_VIDEO:您要对其进行转码的 Cloud Storage 存储桶中的视频的名称,例如
my-vid.mp4
。此字段应考虑您在存储桶中创建的任何文件夹(例如 input/my-vid.mp4
)。
- STORAGE_INPUT_OVERLAY:您的 Cloud Storage 存储桶中用于重叠式广告的 JPEG 图片的名称,例如
my-overlay.jpg
。此字段应考虑您在存储桶中创建的任何文件夹(例如 input/my-overlay.jpg
)。
- LOCATION:运行作业的位置。请使用以下列表中的某个位置:
us-central1
us-west1
us-west2
us-east1
us-east4
southamerica-east1
asia-east1
asia-south1
asia-southeast1
europe-west1
europe-west2
europe-west4
- STORAGE_OUTPUT_FOLDER:您要在其中保存已编码视频输出的 Cloud Storage 文件夹名称。
{
"config": {
"inputs": [
{
"key": "input0",
"uri": "gs://STORAGE_BUCKET_NAME/STORAGE_INPUT_VIDEO"
}
],
"elementaryStreams": [
{
"key": "video-stream0",
"videoStream": {
"h264": {
"heightPixels": 360,
"widthPixels": 640,
"bitrateBps": 550000,
"frameRate": 60
}
}
},
{
"key": "audio-stream0",
"audioStream": {
"codec": "aac",
"bitrateBps": 64000
}
}
],
"muxStreams": [
{
"key": "sd",
"container": "mp4",
"elementaryStreams": [
"video-stream0",
"audio-stream0"
]
}
],
"output": {
"uri": "gs://STORAGE_BUCKET_NAME/STORAGE_OUTPUT_FOLDER/"
},
"overlays": [
{
"image": {
"uri": "gs://STORAGE_BUCKET_NAME/STORAGE_INPUT_OVERLAY",
"resolution": {
"x": 1,
"y": 0.5
},
"alpha": 1
},
"animations": [
{
"animationStatic": {
"xy": {
"x": 0,
"y": 0
},
"startTimeOffset": "0s"
}
},
{
"animationEnd": {
"startTimeOffset": "10s"
}
}
]
}
]
}
}
- 运行以下命令:
gcloud transcoder jobs create --location=LOCATION --file="request.json"
您应该会看到如下所示的响应:
{
"name": "projects/PROJECT_NUMBER/locations/LOCATION/jobs/JOB_ID",
"config": {
...
},
"state": "PENDING",
"createTime": CREATE_TIME,
"ttlAfterCompletionDays": 30
}
Go
在尝试此示例之前,请按照使用客户端库的 Transcoder API 快速入门中的 Go 设置说明进行操作。
如需了解详情,请参阅 Transcoder API Go API 参考文档。
import (
"context"
"fmt"
"io"
"github.com/golang/protobuf/ptypes/duration"
transcoder "cloud.google.com/go/video/transcoder/apiv1"
"cloud.google.com/go/video/transcoder/apiv1/transcoderpb"
)
// createJobWithStaticOverlay creates a job based on a given configuration that
// includes a static overlay. See
// https://cloud.google.com/transcoder/docs/how-to/create-overlays#create-static-overlay
// for more information.
func createJobWithStaticOverlay(w io.Writer, projectID string, location string, inputURI string, overlayImageURI string, outputURI string) error {
// projectID := "my-project-id"
// location := "us-central1"
// inputURI := "gs://my-bucket/my-video-file"
// overlayImageURI := "gs://my-bucket/my-overlay-image-file" - Must be a JPEG
// outputURI := "gs://my-bucket/my-output-folder/"
ctx := context.Background()
client, err := transcoder.NewClient(ctx)
if err != nil {
return fmt.Errorf("NewClient: %v", err)
}
defer client.Close()
req := &transcoderpb.CreateJobRequest{
Parent: fmt.Sprintf("projects/%s/locations/%s", projectID, location),
Job: &transcoderpb.Job{
InputUri: inputURI,
OutputUri: outputURI,
JobConfig: &transcoderpb.Job_Config{
Config: &transcoderpb.JobConfig{
ElementaryStreams: []*transcoderpb.ElementaryStream{
{
Key: "video_stream0",
ElementaryStream: &transcoderpb.ElementaryStream_VideoStream{
VideoStream: &transcoderpb.VideoStream{
CodecSettings: &transcoderpb.VideoStream_H264{
H264: &transcoderpb.VideoStream_H264CodecSettings{
BitrateBps: 550000,
FrameRate: 60,
HeightPixels: 360,
WidthPixels: 640,
},
},
},
},
},
{
Key: "audio_stream0",
ElementaryStream: &transcoderpb.ElementaryStream_AudioStream{
AudioStream: &transcoderpb.AudioStream{
Codec: "aac",
BitrateBps: 64000,
},
},
},
},
MuxStreams: []*transcoderpb.MuxStream{
{
Key: "sd",
Container: "mp4",
ElementaryStreams: []string{"video_stream0", "audio_stream0"},
},
},
Overlays: []*transcoderpb.Overlay{
{
Image: &transcoderpb.Overlay_Image{
Uri: overlayImageURI,
Resolution: &transcoderpb.Overlay_NormalizedCoordinate{
X: 1,
Y: 0.5,
},
Alpha: 1,
},
Animations: []*transcoderpb.Overlay_Animation{
{
AnimationType: &transcoderpb.Overlay_Animation_AnimationStatic{
AnimationStatic: &transcoderpb.Overlay_AnimationStatic{
Xy: &transcoderpb.Overlay_NormalizedCoordinate{
X: 0,
Y: 0,
},
StartTimeOffset: &duration.Duration{
Seconds: 0,
},
},
},
},
{
AnimationType: &transcoderpb.Overlay_Animation_AnimationEnd{
AnimationEnd: &transcoderpb.Overlay_AnimationEnd{
StartTimeOffset: &duration.Duration{
Seconds: 10,
},
},
},
},
},
},
},
},
},
},
}
// Creates the job. Jobs take a variable amount of time to run.
// You can query for the job state; see getJob() in get_job.go.
response, err := client.CreateJob(ctx, req)
if err != nil {
return fmt.Errorf("createJobWithStaticOverlay: %v", err)
}
fmt.Fprintf(w, "Job: %v", response.GetName())
return nil
}
Node.js
在尝试此示例之前,请按照使用客户端库的 Transcoder API 快速入门中的 Node.js 设置说明进行操作。
如需了解详情,请参阅 Transcoder API Node.js API 参考文档。
/**
* TODO(developer): Uncomment these variables before running the sample.
*/
// projectId = 'my-project-id';
// location = 'us-central1';
// inputUri = 'gs://my-bucket/my-video-file';
// overlayImageUri = 'gs://my-bucket/my-overlay-image-file'; // Must be a JPEG
// outputUri = 'gs://my-bucket/my-output-folder/';
// Imports the Transcoder library
const {TranscoderServiceClient} =
require('@google-cloud/video-transcoder').v1;
// Instantiates a client
const transcoderServiceClient = new TranscoderServiceClient();
async function createJobFromStaticOverlay() {
// Construct request
const request = {
parent: transcoderServiceClient.locationPath(projectId, location),
job: {
inputUri: inputUri,
outputUri: outputUri,
config: {
elementaryStreams: [
{
key: 'video-stream0',
videoStream: {
h264: {
heightPixels: 360,
widthPixels: 640,
bitrateBps: 550000,
frameRate: 60,
},
},
},
{
key: 'audio-stream0',
audioStream: {
codec: 'aac',
bitrateBps: 64000,
},
},
],
muxStreams: [
{
key: 'sd',
container: 'mp4',
elementaryStreams: ['video-stream0', 'audio-stream0'],
},
],
overlays: [
{
image: {
uri: overlayImageUri,
resolution: {
x: 1,
y: 0.5,
},
alpha: 1.0,
},
animations: [
{
animationStatic: {
xy: {
x: 0,
y: 0,
},
startTimeOffset: {
seconds: 0,
},
},
},
{
animationEnd: {
startTimeOffset: {
seconds: 10,
},
},
},
],
},
],
},
},
};
// Run request
const [response] = await transcoderServiceClient.createJob(request);
console.log(`Job: ${response.name}`);
}
createJobFromStaticOverlay();
Python
在尝试此示例之前,请按照使用客户端库的 Transcoder API 快速入门中的 Python 设置说明进行操作。
如需了解详情,请参阅 Transcoder API Python API 参考文档。
import argparse
from google.cloud.video import transcoder_v1
from google.cloud.video.transcoder_v1.services.transcoder_service import (
TranscoderServiceClient,
)
from google.protobuf import duration_pb2 as duration
def create_job_with_static_overlay(
project_id, location, input_uri, overlay_image_uri, output_uri
):
"""Creates a job based on an ad-hoc job configuration that includes a static image overlay.
Args:
project_id: The GCP project ID.
location: The location to start the job in.
input_uri: Uri of the video in the Cloud Storage bucket.
overlay_image_uri: Uri of the JPEG image for the overlay in the Cloud Storage bucket. Must be a JPEG.
output_uri: Uri of the video output folder in the Cloud Storage bucket."""
client = TranscoderServiceClient()
parent = f"projects/{project_id}/locations/{location}"
job = transcoder_v1.types.Job()
job.input_uri = input_uri
job.output_uri = output_uri
job.config = transcoder_v1.types.JobConfig(
elementary_streams=[
transcoder_v1.types.ElementaryStream(
key="video-stream0",
video_stream=transcoder_v1.types.VideoStream(
h264=transcoder_v1.types.VideoStream.H264CodecSettings(
height_pixels=360,
width_pixels=640,
bitrate_bps=550000,
frame_rate=60,
),
),
),
transcoder_v1.types.ElementaryStream(
key="audio-stream0",
audio_stream=transcoder_v1.types.AudioStream(
codec="aac", bitrate_bps=64000
),
),
],
mux_streams=[
transcoder_v1.types.MuxStream(
key="sd",
container="mp4",
elementary_streams=["video-stream0", "audio-stream0"],
),
],
overlays=[
transcoder_v1.types.Overlay(
image=transcoder_v1.types.Overlay.Image(
uri=overlay_image_uri,
resolution=transcoder_v1.types.Overlay.NormalizedCoordinate(
x=1,
y=0.5,
),
alpha=1,
),
animations=[
transcoder_v1.types.Overlay.Animation(
animation_static=transcoder_v1.types.Overlay.AnimationStatic(
xy=transcoder_v1.types.Overlay.NormalizedCoordinate(
x=0,
y=0,
),
start_time_offset=duration.Duration(
seconds=0,
),
),
),
transcoder_v1.types.Overlay.Animation(
animation_end=transcoder_v1.types.Overlay.AnimationEnd(
start_time_offset=duration.Duration(
seconds=10,
),
),
),
],
),
],
)
response = client.create_job(parent=parent, job=job)
print(f"Job: {response.name}")
return response
在输出视频中,静态重叠式广告具有以下特征:
- 它显示在时间轴的开头,并且持续显示 10 秒。
- 它会缩放到输出视频的完整宽度和一半高度。
- 它位于输出视频的左上角。
如需了解此配置,请参阅示例输出视频。此视频使用示例重叠式广告图片。
创建动画重叠式广告
在 image
对象中,使用 uri
字段指定 Cloud Storage 中的重叠式广告图片。在 resolution
对象中,设置 x 和 y 值(从 0 到 1.0)。如果值为 0,则系统会保持该尺寸的源图片分辨率;如果值为 1.0,则系统会缩放图片,使其与输出视频的尺寸相匹配。例如,使用值 x: 0
和 y: 0
来保持重叠式广告图片的原始分辨率。
在 animations
数组中,创建一个 fadeType
为 FADE_IN
的 animationFade
对象。设置 x 和 y 坐标(从 0 到 1.0)。这些坐标基于输出视频的分辨率。使用值 x: 0.5
和 y: 0.5
将重叠式广告的左上角放置在输出视频的中心。使用 startTimeOffset
字段指定重叠式广告应在输出视频时间轴的哪个时间点开始显示。在 endTimeOffset
字段设置的时间内,重叠式广告应完全可见。
如需让重叠式广告淡出,请创建另一个 animationFade
对象。这一次,请将 fadeType
设置为 FADE_OUT
。像之前一样,输入位置坐标以及开始时间和结束时间。
您可以将此配置添加到作业模板中,也可以将其添加到临时作业配置中。
REST
在使用任何请求数据之前,请先进行以下替换:
- PROJECT_ID:IAM 设置中列出的 Google Cloud 项目 ID。
- LOCATION:运行作业的位置。请使用其中一个受支持的区域:
us-central1
us-west1
us-west2
us-east1
us-east4
southamerica-east1
asia-east1
asia-south1
asia-southeast1
europe-west1
europe-west2
europe-west4
- STORAGE_BUCKET_NAME:您创建的 Cloud Storage 存储桶的名称。
- STORAGE_INPUT_VIDEO:您要对其进行转码的 Cloud Storage 存储桶中的视频的名称,例如
my-vid.mp4
。此字段应考虑您在存储桶中创建的任何文件夹(例如 input/my-vid.mp4
)。
- STORAGE_INPUT_OVERLAY:您的 Cloud Storage 存储桶中用于重叠式广告的 JPEG 图片的名称,例如
my-overlay.jpg
。此字段应考虑您在存储桶中创建的任何文件夹(例如 input/my-overlay.jpg
)。
- STORAGE_OUTPUT_FOLDER:您要在其中保存已编码视频输出的 Cloud Storage 文件夹名称。
请求 JSON 正文:
{
"config": {
"inputs": [
{
"key": "input0",
"uri": "gs://STORAGE_BUCKET_NAME/STORAGE_INPUT_VIDEO"
}
],
"elementaryStreams": [
{
"key": "video-stream0",
"videoStream": {
"h264": {
"heightPixels": 360,
"widthPixels": 640,
"bitrateBps": 550000,
"frameRate": 60
}
}
},
{
"key": "audio-stream0",
"audioStream": {
"codec": "aac",
"bitrateBps": 64000
}
}
],
"muxStreams": [
{
"key": "sd",
"container": "mp4",
"elementaryStreams": [
"video-stream0",
"audio-stream0"
]
}
],
"output": {
"uri": "gs://STORAGE_BUCKET_NAME/STORAGE_OUTPUT_FOLDER/"
},
"overlays": [
{
"image": {
"uri": "gs://STORAGE_BUCKET_NAME/STORAGE_INPUT_OVERLAY",
"resolution": {
"x": 0,
"y": 0
},
"alpha": 1
},
"animations": [
{
"animationFade": {
"fadeType": "FADE_IN",
"xy": {
"x": 0.5,
"y": 0.5
},
"startTimeOffset": "5s",
"endTimeOffset": "10s"
}
},
{
"animationFade": {
"fadeType": "FADE_OUT",
"xy": {
"x": 0.5,
"y": 0.5
},
"startTimeOffset": "12s",
"endTimeOffset": "15s"
}
}
]
}
]
}
}
如需发送您的请求,请展开以下选项之一:
curl(Linux、macOS 或 Cloud Shell)
将请求正文保存在名为 request.json
的文件中。
在终端中运行以下命令,在当前目录中创建或覆盖此文件:
cat > request.json << 'EOF'
{
"config": {
"inputs": [
{
"key": "input0",
"uri": "gs://STORAGE_BUCKET_NAME/STORAGE_INPUT_VIDEO"
}
],
"elementaryStreams": [
{
"key": "video-stream0",
"videoStream": {
"h264": {
"heightPixels": 360,
"widthPixels": 640,
"bitrateBps": 550000,
"frameRate": 60
}
}
},
{
"key": "audio-stream0",
"audioStream": {
"codec": "aac",
"bitrateBps": 64000
}
}
],
"muxStreams": [
{
"key": "sd",
"container": "mp4",
"elementaryStreams": [
"video-stream0",
"audio-stream0"
]
}
],
"output": {
"uri": "gs://STORAGE_BUCKET_NAME/STORAGE_OUTPUT_FOLDER/"
},
"overlays": [
{
"image": {
"uri": "gs://STORAGE_BUCKET_NAME/STORAGE_INPUT_OVERLAY",
"resolution": {
"x": 0,
"y": 0
},
"alpha": 1
},
"animations": [
{
"animationFade": {
"fadeType": "FADE_IN",
"xy": {
"x": 0.5,
"y": 0.5
},
"startTimeOffset": "5s",
"endTimeOffset": "10s"
}
},
{
"animationFade": {
"fadeType": "FADE_OUT",
"xy": {
"x": 0.5,
"y": 0.5
},
"startTimeOffset": "12s",
"endTimeOffset": "15s"
}
}
]
}
]
}
}
EOF
然后,执行以下命令以发送 REST 请求:
curl -X POST \
-H "Authorization: Bearer $(gcloud auth print-access-token)" \
-H "Content-Type: application/json; charset=utf-8" \
-d @request.json \
"https://transcoder.googleapis.com/v1/projects/PROJECT_ID/locations/LOCATION/jobs"
PowerShell (Windows)
将请求正文保存在名为 request.json
的文件中。
在终端中运行以下命令,在当前目录中创建或覆盖此文件:
@'
{
"config": {
"inputs": [
{
"key": "input0",
"uri": "gs://STORAGE_BUCKET_NAME/STORAGE_INPUT_VIDEO"
}
],
"elementaryStreams": [
{
"key": "video-stream0",
"videoStream": {
"h264": {
"heightPixels": 360,
"widthPixels": 640,
"bitrateBps": 550000,
"frameRate": 60
}
}
},
{
"key": "audio-stream0",
"audioStream": {
"codec": "aac",
"bitrateBps": 64000
}
}
],
"muxStreams": [
{
"key": "sd",
"container": "mp4",
"elementaryStreams": [
"video-stream0",
"audio-stream0"
]
}
],
"output": {
"uri": "gs://STORAGE_BUCKET_NAME/STORAGE_OUTPUT_FOLDER/"
},
"overlays": [
{
"image": {
"uri": "gs://STORAGE_BUCKET_NAME/STORAGE_INPUT_OVERLAY",
"resolution": {
"x": 0,
"y": 0
},
"alpha": 1
},
"animations": [
{
"animationFade": {
"fadeType": "FADE_IN",
"xy": {
"x": 0.5,
"y": 0.5
},
"startTimeOffset": "5s",
"endTimeOffset": "10s"
}
},
{
"animationFade": {
"fadeType": "FADE_OUT",
"xy": {
"x": 0.5,
"y": 0.5
},
"startTimeOffset": "12s",
"endTimeOffset": "15s"
}
}
]
}
]
}
}
'@ | Out-File -FilePath request.json -Encoding utf8
然后,执行以下命令以发送 REST 请求:
$cred = gcloud auth print-access-token
$headers = @{ "Authorization" = "Bearer $cred" }
Invoke-WebRequest `
-Method POST `
-Headers $headers `
-ContentType: "application/json; charset=utf-8" `
-InFile request.json `
-Uri "https://transcoder.googleapis.com/v1/projects/PROJECT_ID/locations/LOCATION/jobs" | Select-Object -Expand Content
您应该收到类似以下内容的 JSON 响应:
{
"name": "projects/PROJECT_NUMBER/locations/LOCATION/jobs/JOB_ID",
"config": {
...
},
"state": "PENDING",
"createTime": CREATE_TIME,
"ttlAfterCompletionDays": 30
}
gcloud CLI
- 创建一个定义作业字段的
request.json
文件。 对 gcloud
命令进行以下替换:- STORAGE_BUCKET_NAME:您创建的 Cloud Storage 存储桶的名称。
- STORAGE_INPUT_VIDEO:您要对其进行转码的 Cloud Storage 存储桶中的视频的名称,例如
my-vid.mp4
。此字段应考虑您在存储桶中创建的任何文件夹(例如 input/my-vid.mp4
)。
- STORAGE_INPUT_OVERLAY:您的 Cloud Storage 存储桶中用于重叠式广告的 JPEG 图片的名称,例如
my-overlay.jpg
。此字段应考虑您在存储桶中创建的任何文件夹(例如 input/my-overlay.jpg
)。
- LOCATION:运行作业的位置。请使用以下列表中的某个位置:
us-central1
us-west1
us-west2
us-east1
us-east4
southamerica-east1
asia-east1
asia-south1
asia-southeast1
europe-west1
europe-west2
europe-west4
- STORAGE_OUTPUT_FOLDER:您要在其中保存已编码视频输出的 Cloud Storage 文件夹名称。
{
"config": {
"inputs": [
{
"key": "input0",
"uri": "gs://STORAGE_BUCKET_NAME/STORAGE_INPUT_VIDEO"
}
],
"elementaryStreams": [
{
"key": "video-stream0",
"videoStream": {
"h264": {
"heightPixels": 360,
"widthPixels": 640,
"bitrateBps": 550000,
"frameRate": 60
}
}
},
{
"key": "audio-stream0",
"audioStream": {
"codec": "aac",
"bitrateBps": 64000
}
}
],
"muxStreams": [
{
"key": "sd",
"container": "mp4",
"elementaryStreams": [
"video-stream0",
"audio-stream0"
]
}
],
"output": {
"uri": "gs://STORAGE_BUCKET_NAME/STORAGE_OUTPUT_FOLDER/"
},
"overlays": [
{
"image": {
"uri": "gs://STORAGE_BUCKET_NAME/STORAGE_INPUT_OVERLAY",
"resolution": {
"x": 0,
"y": 0
},
"alpha": 1
},
"animations": [
{
"animationFade": {
"fadeType": "FADE_IN",
"xy": {
"x": 0.5,
"y": 0.5
},
"startTimeOffset": "5s",
"endTimeOffset": "10s"
}
},
{
"animationFade": {
"fadeType": "FADE_OUT",
"xy": {
"x": 0.5,
"y": 0.5
},
"startTimeOffset": "12s",
"endTimeOffset": "15s"
}
}
]
}
]
}
}
- 运行以下命令:
gcloud transcoder jobs create --location=LOCATION --file="request.json"
您应该会看到如下所示的响应:
{
"name": "projects/PROJECT_NUMBER/locations/LOCATION/jobs/JOB_ID",
"config": {
...
},
"state": "PENDING",
"createTime": CREATE_TIME,
"ttlAfterCompletionDays": 30
}
Go
在尝试此示例之前,请按照使用客户端库的 Transcoder API 快速入门中的 Go 设置说明进行操作。
如需了解详情,请参阅 Transcoder API Go API 参考文档。
import (
"context"
"fmt"
"io"
"github.com/golang/protobuf/ptypes/duration"
transcoder "cloud.google.com/go/video/transcoder/apiv1"
"cloud.google.com/go/video/transcoder/apiv1/transcoderpb"
)
// createJobWithAnimatedOverlay creates a job based on a given configuration that
// includes an animated overlay. See
// https://cloud.google.com/transcoder/docs/how-to/create-overlays#create-animated-overlay
// for more information.
func createJobWithAnimatedOverlay(w io.Writer, projectID string, location string, inputURI string, overlayImageURI string, outputURI string) error {
// projectID := "my-project-id"
// location := "us-central1"
// inputURI := "gs://my-bucket/my-video-file"
// overlayImageURI := "gs://my-bucket/my-overlay-image-file" - Must be a JPEG
// outputURI := "gs://my-bucket/my-output-folder/"
ctx := context.Background()
client, err := transcoder.NewClient(ctx)
if err != nil {
return fmt.Errorf("NewClient: %v", err)
}
defer client.Close()
req := &transcoderpb.CreateJobRequest{
Parent: fmt.Sprintf("projects/%s/locations/%s", projectID, location),
Job: &transcoderpb.Job{
InputUri: inputURI,
OutputUri: outputURI,
JobConfig: &transcoderpb.Job_Config{
Config: &transcoderpb.JobConfig{
ElementaryStreams: []*transcoderpb.ElementaryStream{
{
Key: "video_stream0",
ElementaryStream: &transcoderpb.ElementaryStream_VideoStream{
VideoStream: &transcoderpb.VideoStream{
CodecSettings: &transcoderpb.VideoStream_H264{
H264: &transcoderpb.VideoStream_H264CodecSettings{
BitrateBps: 550000,
FrameRate: 60,
HeightPixels: 360,
WidthPixels: 640,
},
},
},
},
},
{
Key: "audio_stream0",
ElementaryStream: &transcoderpb.ElementaryStream_AudioStream{
AudioStream: &transcoderpb.AudioStream{
Codec: "aac",
BitrateBps: 64000,
},
},
},
},
MuxStreams: []*transcoderpb.MuxStream{
{
Key: "sd",
Container: "mp4",
ElementaryStreams: []string{"video_stream0", "audio_stream0"},
},
},
Overlays: []*transcoderpb.Overlay{
{
Image: &transcoderpb.Overlay_Image{
Uri: overlayImageURI,
Resolution: &transcoderpb.Overlay_NormalizedCoordinate{
X: 0,
Y: 0,
},
Alpha: 1,
},
Animations: []*transcoderpb.Overlay_Animation{
{
AnimationType: &transcoderpb.Overlay_Animation_AnimationFade{
AnimationFade: &transcoderpb.Overlay_AnimationFade{
FadeType: transcoderpb.Overlay_FADE_IN,
Xy: &transcoderpb.Overlay_NormalizedCoordinate{
X: 0.5,
Y: 0.5,
},
StartTimeOffset: &duration.Duration{
Seconds: 5,
},
EndTimeOffset: &duration.Duration{
Seconds: 10,
},
},
},
},
{
AnimationType: &transcoderpb.Overlay_Animation_AnimationFade{
AnimationFade: &transcoderpb.Overlay_AnimationFade{
FadeType: transcoderpb.Overlay_FADE_OUT,
Xy: &transcoderpb.Overlay_NormalizedCoordinate{
X: 0.5,
Y: 0.5,
},
StartTimeOffset: &duration.Duration{
Seconds: 12,
},
EndTimeOffset: &duration.Duration{
Seconds: 15,
},
},
},
},
},
},
},
},
},
},
}
// Creates the job. Jobs take a variable amount of time to run.
// You can query for the job state; see getJob() in get_job.go.
response, err := client.CreateJob(ctx, req)
if err != nil {
return fmt.Errorf("createJobWithAnimatedOverlay: %v", err)
}
fmt.Fprintf(w, "Job: %v", response.GetName())
return nil
}
Node.js
在尝试此示例之前,请按照使用客户端库的 Transcoder API 快速入门中的 Node.js 设置说明进行操作。
如需了解详情,请参阅 Transcoder API Node.js API 参考文档。
/**
* TODO(developer): Uncomment these variables before running the sample.
*/
// projectId = 'my-project-id';
// location = 'us-central1';
// inputUri = 'gs://my-bucket/my-video-file';
// overlayImageUri = 'gs://my-bucket/my-overlay-image-file'; // Must be a JPEG
// outputUri = 'gs://my-bucket/my-output-folder/';
// Imports the Transcoder library
const {TranscoderServiceClient} =
require('@google-cloud/video-transcoder').v1;
// Instantiates a client
const transcoderServiceClient = new TranscoderServiceClient();
async function createJobFromAnimatedOverlay() {
// Construct request
const request = {
parent: transcoderServiceClient.locationPath(projectId, location),
job: {
inputUri: inputUri,
outputUri: outputUri,
config: {
elementaryStreams: [
{
key: 'video-stream0',
videoStream: {
h264: {
heightPixels: 360,
widthPixels: 640,
bitrateBps: 550000,
frameRate: 60,
},
},
},
{
key: 'audio-stream0',
audioStream: {
codec: 'aac',
bitrateBps: 64000,
},
},
],
muxStreams: [
{
key: 'sd',
container: 'mp4',
elementaryStreams: ['video-stream0', 'audio-stream0'],
},
],
overlays: [
{
image: {
uri: overlayImageUri,
resolution: {
x: 0,
y: 0,
},
alpha: 1.0,
},
animations: [
{
animationFade: {
fadeType: 'FADE_IN',
xy: {
x: 0.5,
y: 0.5,
},
startTimeOffset: {
seconds: 5,
},
endTimeOffset: {
seconds: 10,
},
},
},
{
animationFade: {
fadeType: 'FADE_OUT',
xy: {
x: 0.5,
y: 0.5,
},
startTimeOffset: {
seconds: 12,
},
endTimeOffset: {
seconds: 15,
},
},
},
],
},
],
},
},
};
// Run request
const [response] = await transcoderServiceClient.createJob(request);
console.log(`Job: ${response.name}`);
}
createJobFromAnimatedOverlay();
Python
在尝试此示例之前,请按照使用客户端库的 Transcoder API 快速入门中的 Python 设置说明进行操作。
如需了解详情,请参阅 Transcoder API Python API 参考文档。
import argparse
from google.cloud.video import transcoder_v1
from google.cloud.video.transcoder_v1.services.transcoder_service import (
TranscoderServiceClient,
)
from google.protobuf import duration_pb2 as duration
def create_job_with_animated_overlay(
project_id, location, input_uri, overlay_image_uri, output_uri
):
"""Creates a job based on an ad-hoc job configuration that includes an animated image overlay.
Args:
project_id: The GCP project ID.
location: The location to start the job in.
input_uri: Uri of the video in the Cloud Storage bucket.
overlay_image_uri: Uri of the JPEG image for the overlay in the Cloud Storage bucket. Must be a JPEG.
output_uri: Uri of the video output folder in the Cloud Storage bucket."""
client = TranscoderServiceClient()
parent = f"projects/{project_id}/locations/{location}"
job = transcoder_v1.types.Job()
job.input_uri = input_uri
job.output_uri = output_uri
job.config = transcoder_v1.types.JobConfig(
elementary_streams=[
transcoder_v1.types.ElementaryStream(
key="video-stream0",
video_stream=transcoder_v1.types.VideoStream(
h264=transcoder_v1.types.VideoStream.H264CodecSettings(
height_pixels=360,
width_pixels=640,
bitrate_bps=550000,
frame_rate=60,
),
),
),
transcoder_v1.types.ElementaryStream(
key="audio-stream0",
audio_stream=transcoder_v1.types.AudioStream(
codec="aac", bitrate_bps=64000
),
),
],
mux_streams=[
transcoder_v1.types.MuxStream(
key="sd",
container="mp4",
elementary_streams=["video-stream0", "audio-stream0"],
),
],
overlays=[
transcoder_v1.types.Overlay(
image=transcoder_v1.types.Overlay.Image(
uri=overlay_image_uri,
resolution=transcoder_v1.types.Overlay.NormalizedCoordinate(
x=0,
y=0,
),
alpha=1,
),
animations=[
transcoder_v1.types.Overlay.Animation(
animation_fade=transcoder_v1.types.Overlay.AnimationFade(
fade_type=transcoder_v1.types.Overlay.FadeType.FADE_IN,
xy=transcoder_v1.types.Overlay.NormalizedCoordinate(
x=0.5,
y=0.5,
),
start_time_offset=duration.Duration(
seconds=5,
),
end_time_offset=duration.Duration(
seconds=10,
),
),
),
transcoder_v1.types.Overlay.Animation(
animation_fade=transcoder_v1.types.Overlay.AnimationFade(
fade_type=transcoder_v1.types.Overlay.FadeType.FADE_OUT,
xy=transcoder_v1.types.Overlay.NormalizedCoordinate(
x=0.5,
y=0.5,
),
start_time_offset=duration.Duration(
seconds=12,
),
end_time_offset=duration.Duration(
seconds=15,
),
),
),
],
),
],
)
response = client.create_job(parent=parent, job=job)
print(f"Job: {response.name}")
return response
在生成的视频中,动画重叠式广告具有以下特征:
- 它会在输出视频的 5 秒标记处开始淡入。重叠式广告的 alpha 值从 0 开始,到 1.0 结束。重叠式广告的左上角会显示在输出视频的中心。重叠式广告会以重叠式广告图片的原始分辨率显示。
- 淡入后,重叠式广告会显示 2 秒钟。
- 它会在输出视频的 12 秒标记处开始淡出。重叠式广告的 alpha 值从 1.0 开始,到 0 结束。
- 动画会在显示 15 秒标记后消失。
如需了解此配置,请参阅示例输出视频。此视频使用示例重叠式广告图片。