本页介绍了如何为直播配置 Web 视频文本轨道 (WebVTT) 格式的字幕。字幕是从输入流中的 CEA608 或 CEA708 字幕生成的。
字幕功能适用于 HLS 和 DASH 直播。
准备工作
本页假设您已完成 HLS 直播快速入门或 MPEG-DASH 直播快速入门的准备工作部分中的步骤。
创建输入端点
如需创建输入端点,请使用 projects.locations.inputs.create
方法。
在使用任何请求数据之前,请先进行以下替换:
PROJECT_NUMBER
:您的 Google Cloud 项目编号;该编号位于 IAM 设置页面上的项目编号字段中LOCATION
:创建输入端点的位置;请使用某个受支持的地区显示位置us-central1
us-east1
us-east4
us-west1
us-west2
northamerica-northeast1
southamerica-east1
asia-east1
asia-east2
asia-south1
asia-northeast1
asia-southeast1
australia-southeast1
europe-north1
europe-west1
europe-west2
europe-west3
europe-west4
INPUT_ID
:要创建的新输入端点(您将输入流发送到该端点)的用户定义标识符。此值必须为 1-63 个字符,以[a-z0-9]
开头和结尾,并且可以在字符之间包含短划线 (-)。例如my-input
。
请求 JSON 正文:
{ "type": "RTMP_PUSH" }
如需发送您的请求,请展开以下选项之一:
您应该收到类似以下内容的 JSON 响应:
{ "name": "projects/PROJECT_NUMBER/locations/LOCATION/operations/OPERATION_ID", "metadata": { "@type": "type.googleapis.com/google.cloud.video.livestream.v1.OperationMetadata", "createTime": CREATE_TIME, "target": "projects/PROJECT_NUMBER/locations/LOCATION/inputs/INPUT_ID", "verb": "create", "requestedCancellation": false, "apiVersion": "v1" }, "done": false }
复制返回的 OPERATION_ID 以便在下一部分中使用。
检查结果
使用 projects.locations.operations.get
方法检查是否已创建输入端点。如果响应包含 "done: false"
,请重复执行该命令,直到响应包含 "done: true"
。在某个区域中创建第一个输入端点最多可能需要 10 分钟。
在使用任何请求数据之前,请先进行以下替换:
PROJECT_NUMBER
:您的 Google Cloud 项目编号;该编号位于 IAM 设置页面上的项目编号字段中LOCATION
:输入端点所在的位置;请使用某个受支持的地区显示位置us-central1
us-east1
us-east4
us-west1
us-west2
northamerica-northeast1
southamerica-east1
asia-east1
asia-east2
asia-south1
asia-northeast1
asia-southeast1
australia-southeast1
europe-north1
europe-west1
europe-west2
europe-west3
europe-west4
OPERATION_ID
:操作的标识符
如需发送您的请求,请展开以下选项之一:
您应该收到类似以下内容的 JSON 响应:
{ "name": "projects/PROJECT_NUMBER/locations/LOCATION/operations/OPERATION_ID", "metadata": { "@type": "type.googleapis.com/google.cloud.video.livestream.v1.OperationMetadata", "createTime": CREATE_TIME, "endTime": END_TIME, "target": "projects/PROJECT_NUMBER/locations/LOCATION/inputs/INPUT_ID", "verb": "create", "requestedCancellation": false, "apiVersion": "v1" }, "done": true, "response": { "@type": "type.googleapis.com/google.cloud.video.livestream.v1.Input", "name": "projects/PROJECT_NUMBER/locations/LOCATION/inputs/INPUT_ID", "createTime": CREATE_TIME, "updateTime": UPDATE_TIME, "type": "RTMP_PUSH", "uri": INPUT_STREAM_URI, # For example, "rtmp://1.2.3.4/live/b8ebdd94-c8d9-4d88-a16e-b963c43a953b", "tier": "HD" } }
找到 uri
字段,然后复制返回的 INPUT_STREAM_URI 以供稍后在发送输入流部分中使用。
创建频道
如需创建渠道,请使用 projects.locations.channels.create
方法。
请注意频道配置中的以下事项:
一个
ElementaryStream
(即TextStream
)用于字幕。{ "key": "webvtt-english", "textStream": { "codec": "webvtt", "display_name": "English", "language_code": "en-US", "mapping": [ { "input_cea_channel": "CC1" } ] } }
TextStream
codec
字段设置为webvtt
。TextStream
mapping
字段将输入流映射到文本轨道。在此示例中,input_cea_channel
映射到输入渠道CC1
,用于英文字幕。基本数据流用于创建键为
vtt-english
的MuxStream
。{ "key": "vtt_english", "container": "vtt", "elementaryStreams": [ "webvtt-english" ], "segmentSettings": { "segmentDuration": "2s" } }
然后,在 HLS 和 DASH 清单中引用此多路复用流。
{ "fileName": "main.m3u8", "type": "HLS", "muxStreams": [ "mux_video_ts", "vtt_english" ], "maxSegmentCount": 5 }
在使用任何请求数据之前,请先进行以下替换:
PROJECT_NUMBER
:您的 Google Cloud 项目编号;该编号位于 IAM 设置页面上的项目编号字段中LOCATION
:创建渠道的位置;请使用某个受支持的地区显示位置us-central1
us-east1
us-east4
us-west1
us-west2
northamerica-northeast1
southamerica-east1
asia-east1
asia-east2
asia-south1
asia-northeast1
asia-southeast1
australia-southeast1
europe-north1
europe-west1
europe-west2
europe-west3
europe-west4
CHANNEL_ID
:要创建的渠道的用户定义标识符;此值必须为 1-63 个字符,以[a-z0-9]
开头和结尾,并且可以在字符之间包含短划线 (-)。INPUT_ID
:输入端点的用户定义标识符BUCKET_NAME
:您创建的用于存储直播清单和分段文件的 Cloud Storage 存储桶的名称
请求 JSON 正文:
{ "inputAttachments": [ { "key": "my-input", "input": "projects/PROJECT_NUMBER/locations/LOCATION/inputs/INPUT_ID" } ], "output": { "uri": "gs://BUCKET_NAME" }, "elementaryStreams": [ { "key": "es_video", "videoStream": { "h264": { "profile": "high", "widthPixels": 1280, "heightPixels": 720, "bitrateBps": 3000000, "frameRate": 30 } } }, { "key": "es_audio", "audioStream": { "codec": "aac", "channelCount": 2, "bitrateBps": 160000 } }, { "key": "webvtt-english", "textStream": { "codec": "webvtt", "display_name": "English", "language_code": "en-US", "mapping": [ { "input_cea_channel": "CC1" } ] } } ], "muxStreams": [ { "key": "mux_video_fmp4", "container": "fmp4", "elementaryStreams": [ "es_video" ], "segmentSettings": { "segmentDuration": "2s" } }, { "key": "mux_audio_fmp4", "container": "fmp4", "elementaryStreams": [ "es_audio" ], "segmentSettings": { "segmentDuration": "2s" } }, { "key": "mux_video_ts", "container": "ts", "elementaryStreams": [ "es_video", "es_audio" ], "segmentSettings": { "segmentDuration": "2s" } }, { "key": "vtt_english", "container": "vtt", "elementaryStreams": [ "webvtt-english" ], "segmentSettings": { "segmentDuration": "2s" } } ], "manifests": [ { "key": "manifest_dash", "fileName": "main.mpd", "type": "DASH", "muxStreams": [ "mux_video_fmp4", "mux_audio_fmp4", "vtt_english" ], "maxSegmentCount": 5 }, { "key": "manifest_hls", "fileName": "main.m3u8", "type": "HLS", "muxStreams": [ "mux_video_ts", "vtt_english" ], "maxSegmentCount": 5 } ] }
如需发送您的请求,请展开以下选项之一:
您应该收到类似以下内容的 JSON 响应:
{ "name": "projects/PROJECT_NUMBER/locations/LOCATION/operations/OPERATION_ID", "metadata": { "@type": "type.googleapis.com/google.cloud.video.livestream.v1.OperationMetadata", "createTime": CREATE_TIME, "target": "projects/PROJECT_NUMBER/locations/LOCATION/channels/CHANNEL_ID", "verb": "create", "requestedCancellation": false, "apiVersion": "v1" }, "done": false }
获取频道
您可以使用新的操作 ID 检查频道创建操作的结果。
创建渠道后,使用 projects.locations.channels.get
方法查询渠道状态。
在使用任何请求数据之前,请先进行以下替换:
PROJECT_NUMBER
:您的 Google Cloud 项目编号;该编号位于 IAM 设置页面上的项目编号字段中LOCATION
:频道所在的地理位置;请使用支持的地区之一显示位置us-central1
us-east1
us-east4
us-west1
us-west2
northamerica-northeast1
southamerica-east1
asia-east1
asia-east2
asia-south1
asia-northeast1
asia-southeast1
australia-southeast1
europe-north1
europe-west1
europe-west2
europe-west3
europe-west4
CHANNEL_ID
:用户定义的渠道标识符
如需发送您的请求,请展开以下选项之一:
您应该收到类似以下内容的 JSON 响应:
{ "name": "projects/PROJECT_NUMBER/locations/LOCATION/channels/CHANNEL_ID", "createTime": CREATE_TIME, "updateTime": UPDATE_TIME, "inputAttachments": [ { "key": "INPUT_ID", "input": "projects/PROJECT_NUMBER/locations/LOCATION/inputs/INPUT_ID" } ], "activeInput": "INPUT_ID", "output": { "uri": "gs://BUCKET_NAME" }, "elementaryStreams": [ { "key": "es_video", "videoStream": { "h264": { "profile": "high", "widthPixels": 1280, "heightPixels": 720, "bitrateBps": 3000000, "frameRate": 30 } } }, { "key": "es_audio", "audioStream": { "codec": "aac", "channelCount": 2, "bitrateBps": 160000 } }, { "key": "webvtt-english", "textStream": { "codec": "webvtt", "display_name": "English", "language_code": "en-US", "mapping": [ { "input_cea_channel": "CC1" } ] } } ], "muxStreams": [ { "key": "mux_video_fmp4", "container": "fmp4", "elementaryStreams": [ "es_video" ], "segmentSettings": { "segmentDuration": "2s" } }, { "key": "mux_audio_fmp4", "container": "fmp4", "elementaryStreams": [ "es_audio" ], "segmentSettings": { "segmentDuration": "2s" } }, { "key": "mux_video_ts", "container": "ts", "elementaryStreams": [ "es_video", "es_audio" ], "segmentSettings": { "segmentDuration": "2s" } }, { "key": "vtt_english", "container": "vtt", "elementaryStreams": [ "webvtt-english" ], "segmentSettings": { "segmentDuration": "2s" } } ], "manifests": [ { "key": "manifest_dash", "fileName": "main.mpd", "type": "DASH", "muxStreams": [ "mux_video_fmp4", "mux_audio_fmp4", "vtt_english" ], "maxSegmentCount": 5 }, { "key": "manifest_hls", "fileName": "main.m3u8", "type": "HLS", "muxStreams": [ "mux_video_ts", "vtt_english" ], "maxSegmentCount": 5 } ], "streamingState": "STOPPED" }
完整响应包含以下字段:
{
...
"streamingState": "STOPPED"
...
}
此响应表示您现在可以开始播放频道了。
创建频道
使用 projects.locations.channels.start
方法启动渠道。必须先启动频道,然后才能接受输入流或生成输出流。
在某个区域启动第一个频道大约需要 10 分钟。
在使用任何请求数据之前,请先进行以下替换:
PROJECT_NUMBER
:您的 Google Cloud 项目编号;该编号位于 IAM 设置页面上的项目编号字段中LOCATION
:频道所在的地理位置;请使用支持的地区之一显示位置us-central1
us-east1
us-east4
us-west1
us-west2
northamerica-northeast1
southamerica-east1
asia-east1
asia-east2
asia-south1
asia-northeast1
asia-southeast1
australia-southeast1
europe-north1
europe-west1
europe-west2
europe-west3
europe-west4
CHANNEL_ID
:用户定义的渠道标识符
如需发送您的请求,请展开以下选项之一:
您应该收到类似以下内容的 JSON 响应:
{ "name": "projects/PROJECT_NUMBER/locations/LOCATION/operations/OPERATION_ID", "metadata": { "@type": "type.googleapis.com/google.cloud.video.livestream.v1.OperationMetadata", "createTime": CREATE_TIME, "target": "projects/PROJECT_NUMBER/locations/LOCATION/channels/CHANNEL_ID", "verb": "start", "requestedCancellation": false, "apiVersion": "v1" }, "done": false }
如需确定频道是否已开始,请像之前一样获取频道信息。响应应包含以下内容:
{
...
"streamingState": "AWAITING_INPUT"
...
}
发送输入流
频道现已准备就绪,接下来向输入端点发送输入流以生成直播。您可以下载带有字幕的 MP4(或其他 TEST_VOD_FILE),然后使用 ffmpeg
将其发送到输入端点。
打开一个新终端窗口。运行以下命令,使用检查结果部分中的 INPUT_STREAM_URI:
ffmpeg -re -stream_loop -1 -i "TEST_VOD_FILE" \
-c:v copy -c:a aac -strict 2 -f "flv" "INPUT_STREAM_URI"
播放生成的直播
如需在 Shaka Player 中播放生成的媒体文件,请完成以下步骤:
- 请将 Cloud Storage 存储分区设为可公开读取。
- 如需在 Cloud Storage 存储分区上启用跨域资源共享 (CORS),请执行以下操作:
- 创建一个包含以下代码的 JSON 文件:
[ { "origin": ["https://shaka-player-demo.appspot.com/"], "responseHeader": ["Content-Type", "Range"], "method": ["GET", "HEAD"], "maxAgeSeconds": 3600 } ]
-
将
JSON_FILE_NAME
替换为您在上一步中创建的 JSON 文件的名称,然后运行以下命令:gcloud storage buckets update gs://BUCKET_NAME --cors-file=JSON_FILE_NAME.json
- 创建一个包含以下代码的 JSON 文件:
- 在 Cloud Storage 存储桶中,找到生成的
main.m3u8
文件。在该文件的公开访问权限列中,点击复制网址。 - 导航到在线直播播放器 Shaka Player。
- 点击顶部导航栏中的自定义内容。
- 点击 + 按钮。
将文件的公开网址粘贴到清单网址框中。
在名称框中输入一个名称。
点击保存。
点击播放。
您应该会看到直播开始播放。您可能需要在 Shaka Player 界面中启用字幕。
清理
停止频道
使用 projects.locations.channels.stop
方法停止渠道。
您必须先停止频道,然后才能将其删除。
在使用任何请求数据之前,请先进行以下替换:
PROJECT_NUMBER
:您的 Google Cloud 项目编号;该编号位于 IAM 设置页面上的项目编号字段中LOCATION
:频道所在的地理位置;请使用支持的地区之一显示位置us-central1
us-east1
us-east4
us-west1
us-west2
northamerica-northeast1
southamerica-east1
asia-east1
asia-east2
asia-south1
asia-northeast1
asia-southeast1
australia-southeast1
europe-north1
europe-west1
europe-west2
europe-west3
europe-west4
CHANNEL_ID
:用户定义的渠道标识符
如需发送您的请求,请展开以下选项之一:
您应该收到类似以下内容的 JSON 响应:
{ "name": "projects/PROJECT_NUMBER/locations/LOCATION/operations/OPERATION_ID", "metadata": { "@type": "type.googleapis.com/google.cloud.video.livestream.v1.OperationMetadata", "createTime": CREATE_TIME, "target": "projects/PROJECT_NUMBER/locations/LOCATION/channels/CHANNEL_ID", "verb": "stop", "requestedCancellation": false, "apiVersion": "v1" }, "done": false }
停止输入流
如果您使用 ffmpeg
发送输入源,则在停止频道后,连接会自动断开。
删除频道
使用 projects.locations.channels.delete
方法删除频道。您必须先删除频道,然后才能删除该频道使用的输入端点。
在使用任何请求数据之前,请先进行以下替换:
PROJECT_NUMBER
:您的 Google Cloud 项目编号;该编号位于 IAM 设置页面上的项目编号字段中LOCATION
:频道所在的地理位置;请使用支持的地区之一显示位置us-central1
us-east1
us-east4
us-west1
us-west2
northamerica-northeast1
southamerica-east1
asia-east1
asia-east2
asia-south1
asia-northeast1
asia-southeast1
australia-southeast1
europe-north1
europe-west1
europe-west2
europe-west3
europe-west4
CHANNEL_ID
:用户定义的渠道标识符
如需发送您的请求,请展开以下选项之一:
您应该收到类似以下内容的 JSON 响应:
{ "name": "projects/PROJECT_NUMBER/locations/LOCATION/operations/OPERATION_ID", "metadata": { "@type": "type.googleapis.com/google.cloud.video.livestream.v1.OperationMetadata", "createTime": CREATE_TIME, "target": "projects/PROJECT_NUMBER/locations/LOCATION/channels/CHANNEL_ID", "verb": "delete", "requestedCancellation": false, "apiVersion": "v1" }, "done": false }
删除输入端点
使用 projects.locations.inputs.delete
方法删除输入端点。
在使用任何请求数据之前,请先进行以下替换:
PROJECT_NUMBER
:您的 Google Cloud 项目编号;该编号位于 IAM 设置页面上的项目编号字段中LOCATION
:输入端点所在的位置;请使用某个受支持的地区显示位置us-central1
us-east1
us-east4
us-west1
us-west2
northamerica-northeast1
southamerica-east1
asia-east1
asia-east2
asia-south1
asia-northeast1
asia-southeast1
australia-southeast1
europe-north1
europe-west1
europe-west2
europe-west3
europe-west4
INPUT_ID
:输入端点的用户定义标识符
如需发送您的请求,请展开以下选项之一:
您应该收到类似以下内容的 JSON 响应:
{ "name": "projects/PROJECT_NUMBER/locations/LOCATION/operations/OPERATION_ID", "metadata": { "@type": "type.googleapis.com/google.cloud.video.livestream.v1.OperationMetadata", "createTime": CREATE_TIME, "target": "projects/PROJECT_NUMBER/locations/LOCATION/inputs/INPUT_ID", "verb": "delete", "requestedCancellation": false, "apiVersion": "v1" }, "done": false }
删除 Cloud Storage 存储桶
在 Google Cloud 控制台中,前往 Cloud Storage 浏览器页面。
选中所创建存储分区旁边的复选框。
点击删除。
在随即显示的对话框窗口中,点击删除以永久删除存储桶及其内容。