本页面介绍了如何从您转码的视频生成包含帧的精灵表。这些缩小的帧(也称为缩略图)有助于整理和预览内容。要生成精灵表,请在 JobConfig
模板中使用 spriteSheets
数组。
您可以通过以下两种方式生成精灵表:
生成一定数量的缩略图
以下配置会生成一个包含大图片的 10x10 精灵表和一个由小图片组成的 10x10 精灵表,每张小图片都有 100 个缩略图。请注意,Sprite 工作表要求作业配置中至少包含一个 VideoStream
。
您可以将此配置添加到作业模板中,也可以将其添加到临时作业配置中。
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_OUTPUT_FOLDER
:您要在其中保存已编码视频输出的 Cloud Storage 文件夹名称。
如需发送您的请求,请展开以下选项之一:
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/"
},
"spriteSheets": [
{
"filePrefix": "small-sprite-sheet",
"spriteHeightPixels": 32,
"spriteWidthPixels": 64,
"columnCount": 10,
"rowCount": 10,
"totalCount": 100
},
{
"filePrefix": "large-sprite-sheet",
"spriteHeightPixels": 72,
"spriteWidthPixels": 128,
"columnCount": 10,
"rowCount": 10,
"totalCount": 100
}
]
}
}
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/"
},
"spriteSheets": [
{
"filePrefix": "small-sprite-sheet",
"spriteHeightPixels": 32,
"spriteWidthPixels": 64,
"columnCount": 10,
"rowCount": 10,
"totalCount": 100
},
{
"filePrefix": "large-sprite-sheet",
"spriteHeightPixels": 72,
"spriteWidthPixels": 128,
"columnCount": 10,
"rowCount": 10,
"totalCount": 100
}
]
}
}
'@ | 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
- 创建一个定义作业字段的
request.json
文件。 对 gcloud
命令进行以下替换:- STORAGE_BUCKET_NAME:您创建的 Cloud Storage 存储桶的名称。
- STORAGE_INPUT_VIDEO:您要对其进行转码的 Cloud Storage 存储桶中的视频的名称,例如
my-vid.mp4
。此字段应考虑您在存储桶中创建的任何文件夹(例如 input/my-vid.mp4
)。
- 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/"
},
"spriteSheets": [
{
"filePrefix": "small-sprite-sheet",
"spriteHeightPixels": 32,
"spriteWidthPixels": 64,
"columnCount": 10,
"rowCount": 10,
"totalCount": 100
},
{
"filePrefix": "large-sprite-sheet",
"spriteHeightPixels": 72,
"spriteWidthPixels": 128,
"columnCount": 10,
"rowCount": 10,
"totalCount": 100
}
]
}
}
- 运行以下命令:
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 参考文档。
如需向 Transcoder API 进行身份验证,请设置应用默认凭据。如需了解详情,请参阅为本地开发环境设置身份验证。
import (
"context"
"fmt"
"io"
transcoder "cloud.google.com/go/video/transcoder/apiv1"
"cloud.google.com/go/video/transcoder/apiv1/transcoderpb"
)
// createJobWithSetNumberImagesSpritesheet creates a job from an ad-hoc configuration and generates
// two spritesheets from the input video. Each spritesheet contains a set number of images.
func createJobWithSetNumberImagesSpritesheet(w io.Writer, projectID string, location string, inputURI string, outputURI string) error {
// projectID := "my-project-id"
// location := "us-central1"
// inputURI := "gs://my-bucket/my-video-file"
// outputURI := "gs://my-bucket/my-output-folder/"
ctx := context.Background()
client, err := transcoder.NewClient(ctx)
if err != nil {
return fmt.Errorf("NewClient: %w", 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"},
},
},
SpriteSheets: []*transcoderpb.SpriteSheet{
{
FilePrefix: "small-sprite-sheet",
SpriteWidthPixels: 64,
SpriteHeightPixels: 32,
ColumnCount: 10,
RowCount: 10,
ExtractionStrategy: &transcoderpb.SpriteSheet_TotalCount{
TotalCount: 100,
},
},
{
FilePrefix: "large-sprite-sheet",
SpriteWidthPixels: 128,
SpriteHeightPixels: 72,
ColumnCount: 10,
RowCount: 10,
ExtractionStrategy: &transcoderpb.SpriteSheet_TotalCount{
TotalCount: 100,
},
},
},
},
},
},
}
// Creates the job. Jobs take a variable amount of time to run. You can query for the job state.
// See https://cloud.google.com/transcoder/docs/how-to/jobs#check_job_status for more info.
response, err := client.CreateJob(ctx, req)
if err != nil {
return fmt.Errorf("createJobWithSetNumberImagesSpritesheet: %w", err)
}
fmt.Fprintf(w, "Job: %v", response.GetName())
return nil
}
Node.js
在试用此示例之前,请按照 Transcoder API 快速入门:使用客户端库中的 Node.js 设置说明进行操作。
如需了解详情,请参阅 Transcoder API Node.js API 参考文档。
如需向 Transcoder API 进行身份验证,请设置应用默认凭据。如需了解详情,请参阅为本地开发环境设置身份验证。
/**
* TODO(developer): Uncomment these variables before running the sample.
*/
// projectId = 'my-project-id';
// location = 'us-central1';
// inputUri = 'gs://my-bucket/my-video-file';
// 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 createJobWithSetNumberImagesSpritesheet() {
// 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'],
},
],
spriteSheets: [
{
filePrefix: 'small-sprite-sheet',
spriteHeightPixels: 32,
spriteWidthPixels: 64,
columnCount: 10,
rowCount: 10,
totalCount: 100,
},
{
filePrefix: 'large-sprite-sheet',
spriteHeightPixels: 72,
spriteWidthPixels: 128,
columnCount: 10,
rowCount: 10,
totalCount: 100,
},
],
},
},
};
// Run request
const [response] = await transcoderServiceClient.createJob(request);
console.log(`Job: ${response.name}`);
}
createJobWithSetNumberImagesSpritesheet();
Python
在试用此示例之前,请按照 Transcoder API 快速入门:使用客户端库中的 Python 设置说明进行操作。
如需了解详情,请参阅 Transcoder API Python API 参考文档。
如需向 Transcoder API 进行身份验证,请设置应用默认凭据。如需了解详情,请参阅为本地开发环境设置身份验证。
import argparse
from google.cloud.video import transcoder_v1
from google.cloud.video.transcoder_v1.services.transcoder_service import (
TranscoderServiceClient,
)
def create_job_with_set_number_images_spritesheet(
project_id: str,
location: str,
input_uri: str,
output_uri: str,
) -> transcoder_v1.types.resources.Job:
"""Creates a job based on an ad-hoc job configuration that generates two spritesheets.
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.
output_uri: Uri of the video output folder in the Cloud Storage bucket.
Returns:
The job resource.
"""
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(
# Create an ad-hoc job. For more information, see https://cloud.google.com/transcoder/docs/how-to/jobs#create_jobs_ad_hoc.
# See all options for the job config at https://cloud.google.com/transcoder/docs/reference/rest/v1/JobConfig.
elementary_streams=[
# This section defines the output video stream.
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,
),
),
),
# This section defines the output audio stream.
transcoder_v1.types.ElementaryStream(
key="audio-stream0",
audio_stream=transcoder_v1.types.AudioStream(
codec="aac", bitrate_bps=64000
),
),
],
# This section multiplexes the output audio and video together into a container.
mux_streams=[
transcoder_v1.types.MuxStream(
key="sd",
container="mp4",
elementary_streams=["video-stream0", "audio-stream0"],
),
],
# Generate two sprite sheets from the input video into the GCS bucket. For more information, see
# https://cloud.google.com/transcoder/docs/how-to/generate-spritesheet#generate_set_number_of_images.
sprite_sheets=[
# Generate a 10x10 sprite sheet with 64x32px images.
transcoder_v1.types.SpriteSheet(
file_prefix="small-sprite-sheet",
sprite_width_pixels=64,
sprite_height_pixels=32,
column_count=10,
row_count=10,
total_count=100,
),
# Generate a 10x10 sprite sheet with 128x72px images.
transcoder_v1.types.SpriteSheet(
file_prefix="large-sprite-sheet",
sprite_width_pixels=128,
sprite_height_pixels=72,
column_count=10,
row_count=10,
total_count=100,
),
],
)
response = client.create_job(parent=parent, job=job)
print(f"Job: {response.name}")
return response
对示例视频使用此配置,以便生成以下精灵表:
图 1.小图片精灵表(100 张缩略图)
图 2.大图片精灵表(100 张缩略图)
定期生成缩略图
以下配置生成一个小图片精灵表和一个大图片精灵表。每个精灵表都包含从输入视频每 7 秒生成的缩略图。请注意,Sprite 工作表要求作业配置中至少包含一个 VideoStream
。
您可以将此配置添加到作业模板中,也可以将其添加到临时作业配置中。
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_OUTPUT_FOLDER
:您要在其中保存已编码视频输出的 Cloud Storage 文件夹名称。
如需发送您的请求,请展开以下选项之一:
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/"
},
"spriteSheets": [
{
"filePrefix": "small-sprite-sheet",
"spriteHeightPixels": 32,
"spriteWidthPixels": 64,
"interval": "7s"
},
{
"filePrefix": "large-sprite-sheet",
"spriteHeightPixels": 72,
"spriteWidthPixels": 128,
"interval": "7s"
}
]
}
}
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/"
},
"spriteSheets": [
{
"filePrefix": "small-sprite-sheet",
"spriteHeightPixels": 32,
"spriteWidthPixels": 64,
"interval": "7s"
},
{
"filePrefix": "large-sprite-sheet",
"spriteHeightPixels": 72,
"spriteWidthPixels": 128,
"interval": "7s"
}
]
}
}
'@ | 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
- 创建一个定义作业字段的
request.json
文件。 对 gcloud
命令进行以下替换:- STORAGE_BUCKET_NAME:您创建的 Cloud Storage 存储桶的名称。
- STORAGE_INPUT_VIDEO:您要对其进行转码的 Cloud Storage 存储桶中的视频的名称,例如
my-vid.mp4
。此字段应考虑您在存储桶中创建的任何文件夹(例如 input/my-vid.mp4
)。
- 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/"
},
"spriteSheets": [
{
"filePrefix": "small-sprite-sheet",
"spriteHeightPixels": 32,
"spriteWidthPixels": 64,
"interval": "7s"
},
{
"filePrefix": "large-sprite-sheet",
"spriteHeightPixels": 72,
"spriteWidthPixels": 128,
"interval": "7s"
}
]
}
}
- 运行以下命令:
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 参考文档。
如需向 Transcoder 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"
)
// createJobWithPeriodicImagesSpritesheet creates a job from an ad-hoc configuration and generates
// two spritesheets from the input video. Each spritesheet contains images that are captured
// periodically based on a user-defined time interval.
func createJobWithPeriodicImagesSpritesheet(w io.Writer, projectID string, location string, inputURI string, outputURI string) error {
// projectID := "my-project-id"
// location := "us-central1"
// inputURI := "gs://my-bucket/my-video-file"
// outputURI := "gs://my-bucket/my-output-folder/"
ctx := context.Background()
client, err := transcoder.NewClient(ctx)
if err != nil {
return fmt.Errorf("NewClient: %w", 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"},
},
},
SpriteSheets: []*transcoderpb.SpriteSheet{
{
FilePrefix: "small-sprite-sheet",
SpriteWidthPixels: 64,
SpriteHeightPixels: 32,
ExtractionStrategy: &transcoderpb.SpriteSheet_Interval{
Interval: &duration.Duration{
Seconds: 7,
},
},
},
{
FilePrefix: "large-sprite-sheet",
SpriteWidthPixels: 128,
SpriteHeightPixels: 72,
ExtractionStrategy: &transcoderpb.SpriteSheet_Interval{
Interval: &duration.Duration{
Seconds: 7,
},
},
},
},
},
},
},
}
// Creates the job. Jobs take a variable amount of time to run. You can query for the job state.
// See https://cloud.google.com/transcoder/docs/how-to/jobs#check_job_status for more info.
response, err := client.CreateJob(ctx, req)
if err != nil {
return fmt.Errorf("createJobWithPeriodicImagesSpritesheet: %w", err)
}
fmt.Fprintf(w, "Job: %v", response.GetName())
return nil
}
Python
在试用此示例之前,请按照 Transcoder API 快速入门:使用客户端库中的 Python 设置说明进行操作。
如需了解详情,请参阅 Transcoder API Python API 参考文档。
如需向 Transcoder 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_periodic_images_spritesheet(
project_id: str,
location: str,
input_uri: str,
output_uri: str,
) -> transcoder_v1.types.resources.Job:
"""Creates a job based on an ad-hoc job configuration that generates two spritesheets.
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.
output_uri: Uri of the video output folder in the Cloud Storage bucket.
Returns:
The job resource.
"""
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(
# Create an ad-hoc job. For more information, see https://cloud.google.com/transcoder/docs/how-to/jobs#create_jobs_ad_hoc.
# See all options for the job config at https://cloud.google.com/transcoder/docs/reference/rest/v1/JobConfig.
elementary_streams=[
# This section defines the output video stream.
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,
),
),
),
# This section defines the output audio stream.
transcoder_v1.types.ElementaryStream(
key="audio-stream0",
audio_stream=transcoder_v1.types.AudioStream(
codec="aac", bitrate_bps=64000
),
),
],
# This section multiplexes the output audio and video together into a container.
mux_streams=[
transcoder_v1.types.MuxStream(
key="sd",
container="mp4",
elementary_streams=["video-stream0", "audio-stream0"],
),
],
# Generate two sprite sheets from the input video into the GCS bucket. For more information, see
# https://cloud.google.com/transcoder/docs/how-to/generate-spritesheet#generate_image_periodically.
sprite_sheets=[
# Generate a sprite sheet with 64x32px images. An image is taken every 7 seconds from the video.
transcoder_v1.types.SpriteSheet(
file_prefix="small-sprite-sheet",
sprite_width_pixels=64,
sprite_height_pixels=32,
interval=duration.Duration(
seconds=7,
),
),
# Generate a sprite sheet with 128x72px images. An image is taken every 7 seconds from the video.
transcoder_v1.types.SpriteSheet(
file_prefix="large-sprite-sheet",
sprite_width_pixels=128,
sprite_height_pixels=72,
interval=duration.Duration(
seconds=7,
),
),
],
)
response = client.create_job(parent=parent, job=job)
print(f"Job: {response.name}")
return response
对示例视频使用此配置,以便生成以下精灵表:
图 3.小图片精灵表(每 7 秒钟生成 1 张缩略图)
图 4.大图片精灵表(每 7 秒钟生成 1 张缩略图)
示例视频中生成 8 张缩略图,时长为 60 秒。