本頁說明如何為直播設定 AI 生成字幕和翻譯字幕 (即翻譯)。
HLS 和 DASH 即時串流支援 AI 生成的字幕和翻譯。
事前準備
本頁面假設您已完成「開始前」一節中的步驟,也就是「HTTP 即時串流快速入門」或「MPEG-DASH 即時串流快速入門」中的步驟。
支援的地區
AI 生成字幕的語言代碼支援依據地點設定。
| 位置 | 支援的語言代碼 |
|---|---|
asia-northeast1 |
en-US |
asia-south1 |
en-INen-GBen-US |
asia-southeast1 |
en-US |
australia-southeast1 |
en-AU |
europe-west1 |
da-DKnl-NLen-GBen-USfr-FRde-DEit-ITes-ES |
europe-west2 |
en-GB |
europe-west3 |
da-DKnl-NLen-GBen-US fr-FRde-DEit-ITes-ES |
northamerica-northeast1 |
en-CAfr-CA |
us-central1 |
en-USpt-BRes-COes-MXes-US |
us-east1 |
en-USpt-BRes-COes-MXes-US |
us-west1 |
en-USpt-BRes-COes-MXes-US |
AI 生成翻譯功能僅支援 us-west1 和 europe-west1。所有支援 AI 生成字幕的語言代碼,都支援 AI 生成翻譯的來源和目標語言代碼。
請對這個頁面上的所有 API 要求使用 us-west1 或 europe-west1。
建立輸入端點
如要建立輸入端點,請使用 projects.locations.inputs.create 方法。
使用任何要求資料之前,請先替換以下項目:
PROJECT_NUMBER:您的 Google Cloud 專案編號;這位於「專案編號」欄位,在「IAM 設定」頁面上LOCATION:建立輸入端點的位置;請使用支援的地區之一。顯示地區us-central1us-east1us-east4us-west1us-west2northamerica-northeast1southamerica-east1asia-east1asia-east2asia-south1asia-northeast1asia-southeast1australia-southeast1europe-north1europe-west1europe-west2europe-west3europe-west4
INPUT_ID:為要建立的新輸入端點定義的 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-central1us-east1us-east4us-west1us-west2northamerica-northeast1southamerica-east1asia-east1asia-east2asia-south1asia-northeast1asia-southeast1australia-southeast1europe-north1europe-west1europe-west2europe-west3europe-west4
OPERATION_ID:作業的 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_ai", "textStream": { "codec": "webvtt", "displayName": "English (AI captioned)", "languageCode": "en-US", "mapping": [ { "inputTrack": 1 // audio track number } ] } }TextStreamcodec欄位必須設為webvtt。TextStreammapping欄位會使用inputTrack指定要從哪個輸入音軌生成字幕。對應檔不得包含inputCeaChannel欄位,這個欄位只用於使用者提供的隱藏式輔助字幕。將
languageCode設為音軌中使用的語言。基本串流用於建立具有金鑰
vtt_english_ai的MuxStream。{ "key": "vtt_english_ai", "container": "vtt", "elementaryStreams": [ "webvtt_english_ai" ], "segmentSettings": { "segmentDuration": "2s" } }然後,HLS 和 DASH 資訊清單都會參照這個多工串流。
{ "fileName": "main.m3u8", "type": "HLS", "muxStreams": [ "mux_video_ts", "vtt_english_ai" ], "maxSegmentCount": 5 }
使用任何要求資料之前,請先替換以下項目:
PROJECT_NUMBER:您的 Google Cloud 專案編號;這位於「專案編號」欄位,在「IAM 設定」頁面上LOCATION:建立頻道的所在位置;請使用支援的區域之一。顯示地區us-central1us-east1us-east4us-west1us-west2northamerica-northeast1southamerica-east1asia-east1asia-east2asia-south1asia-northeast1asia-southeast1australia-southeast1europe-north1europe-west1europe-west2europe-west3europe-west4
CHANNEL_ID:要建立的管道使用者定義 ID;這個值必須介於 1 至 63 個字元之間,開頭和結尾須為[a-z0-9],且字元之間可包含破折號 (-)INPUT_ID:輸入端點的使用者定義 IDBUCKET_NAME:您建立的 Cloud Storage bucket 名稱,用於保存即時串流資訊清單和區隔檔案
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_ai",
"textStream": {
"codec": "webvtt",
"displayName": "English (AI captioned)",
"languageCode": "en-US",
"mapping": [
{
"inputTrack": 1
}
]
}
}
],
"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_ai",
"container": "vtt",
"elementaryStreams": [
"webvtt_english_ai"
],
"segmentSettings": {
"segmentDuration": "2s"
}
}
],
"manifests": [
{
"key": "manifest_dash",
"fileName": "main.mpd",
"type": "DASH",
"muxStreams": [
"mux_video_fmp4",
"mux_audio_fmp4",
"vtt_english_ai"
],
"maxSegmentCount": 5
},
{
"key": "manifest_hls",
"fileName": "main.m3u8",
"type": "HLS",
"muxStreams": [
"mux_video_ts",
"vtt_english_ai"
],
"maxSegmentCount": 5
}
]
}
如要傳送要求,請展開以下其中一個選項:
您應該會收到如下的 JSON 回應:
取得頻道
您可以使用新的作業 ID 檢查作業結果。
建立管道後,請使用 projects.locations.channels.get 方法查詢管道狀態。
使用任何要求資料之前,請先替換以下項目:
PROJECT_NUMBER:您的 Google Cloud 專案編號;這位於「專案編號」欄位,在「IAM 設定」頁面上LOCATION:頻道所在位置;請使用支援的區域之一顯示地區us-central1us-east1us-east4us-west1us-west2northamerica-northeast1southamerica-east1asia-east1asia-east2asia-south1asia-northeast1asia-southeast1australia-southeast1europe-north1europe-west1europe-west2europe-west3europe-west4
CHANNEL_ID:使用者定義的管道 ID
如要傳送要求,請展開以下其中一個選項:
您應該會收到如下的 JSON 回應:
完整回應包含下列欄位:
{
...
"streamingState": "STOPPED"
...
}
這項回應表示你現在可以啟動頻道。
啟動管道
使用 projects.locations.channels.start 方法啟動管道。頻道必須先啟動,才能接受輸入串流或產生輸出串流。
在區域中啟動第一個頻道約需 10 分鐘。
使用任何要求資料之前,請先替換以下項目:
PROJECT_NUMBER:您的 Google Cloud 專案編號;這位於「專案編號」欄位,在「IAM 設定」頁面上LOCATION:頻道所在位置;請使用支援的區域之一顯示地區us-central1us-east1us-east4us-west1us-west2northamerica-northeast1southamerica-east1asia-east1asia-east2asia-south1asia-northeast1asia-southeast1australia-southeast1europe-north1europe-west1europe-west2europe-west3europe-west4
CHANNEL_ID:使用者定義的管道 ID
如要傳送要求,請展開以下其中一個選項:
您應該會收到如下的 JSON 回應:
如要判斷頻道是否已啟動,請取得頻道資訊,做法與先前相同。回應應包含下列內容:
{
...
"streamingState": "AWAITING_INPUT"
...
}
傳送輸入串流
頻道準備就緒後,請將輸入串流傳送至輸入端點,產生直播串流。你可以下載附有字幕的 MP4 檔案 (或其他格式),然後使用 ffmpeg 將檔案傳送至輸入端點。TEST_VOD_FILE
開啟新的終端機視窗。使用「檢查結果」一節中的 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"
驗證輸出資訊清單中的說明文字
執行下列指令,查看產生的 HLS 資訊清單內容:
gcloud storage cat gs://BUCKET_NAME/main.m3u8
AI 生成的英文 WebVTT 字幕會顯示在輸出資訊清單中,如下所示:
#EXT-X-MEDIA:TYPE=SUBTITLES,GROUP-ID="sub",LANGUAGE="en-US",NAME="English (AI captioned)",AUTOSELECT=YES,DEFAULT=YES,FORCED=NO,URI="vtt_english_ai/index-1.m3u8"
停止管道
更新頻道設定前,請務必先停止頻道。
使用 projects.locations.channels.stop 方法停止頻道。
使用任何要求資料之前,請先替換以下項目:
PROJECT_NUMBER:您的 Google Cloud 專案編號;這位於「專案編號」欄位,在「IAM 設定」頁面上LOCATION:頻道所在位置;請使用支援的區域之一顯示地區us-central1us-east1us-east4us-west1us-west2northamerica-northeast1southamerica-east1asia-east1asia-east2asia-south1asia-northeast1asia-southeast1australia-southeast1europe-north1europe-west1europe-west2europe-west3europe-west4
CHANNEL_ID:使用者定義的管道 ID
如要傳送要求,請展開以下其中一個選項:
您應該會收到如下的 JSON 回應:
更新翻譯的頻道設定
更新頻道設定,加入翻譯字幕 (即翻譯)。
請注意頻道設定的下列新增項目:
一個
ElementaryStream,一個TextStream,用於單一翻譯語言的字幕。{ "key": "webvtt_spanish_ai", "textStream": { "codec": "webvtt", "displayName": "Spanish (AI captioned)", "languageCode": "es-MX", "mapping": [ { "inputTrack": 1, // audio track number "fromLanguageCode": "en-US" // original audio in English } ] } }將
languageCode設為所選翻譯語言。將
fromLanguageCode設為音軌的原始語言。基本串流用於建立具有金鑰
vtt_spanish_ai的MuxStream。{ "key": "vtt_spanish_ai", "container": "vtt", "elementaryStreams": [ "webvtt_spanish_ai" ], "segmentSettings": { "segmentDuration": "2s" } }然後,HLS 和 DASH 資訊清單都會參照這個多工串流。
{ "fileName": "main.m3u8", "type": "HLS", "muxStreams": [ "mux_video_ts", "vtt_english_ai", "vtt_spanish_ai" ], "maxSegmentCount": 5 }
如要更新頻道,請使用 projects.locations.channels.patch 方法。
使用任何要求資料之前,請先替換以下項目:
PROJECT_NUMBER:您的 Google Cloud 專案編號;這位於「專案編號」欄位,在「IAM 設定」頁面上LOCATION:建立頻道的所在位置;請使用支援的區域之一。顯示地區us-central1us-east1us-east4us-west1us-west2northamerica-northeast1southamerica-east1asia-east1asia-east2asia-south1asia-northeast1asia-southeast1australia-southeast1europe-north1europe-west1europe-west2europe-west3europe-west4
CHANNEL_ID:要建立的管道使用者定義 ID;這個值必須介於 1 至 63 個字元之間,開頭和結尾須為[a-z0-9],且字元之間可包含破折號 (-)
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_ai",
"textStream": {
"codec": "webvtt",
"displayName": "English (AI captioned)",
"languageCode": "en-US",
"mapping": [
{
"inputTrack": 1
}
]
}
},
{
"key": "webvtt_spanish_ai",
"textStream": {
"codec": "webvtt",
"displayName": "Spanish (AI captioned)",
"languageCode": "es-MX",
"mapping": [
{
"inputTrack": 1,
"fromLanguageCode": "en-US"
}
]
}
}
],
"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_ai",
"container": "vtt",
"elementaryStreams": [
"webvtt_english_ai"
],
"segmentSettings": {
"segmentDuration": "2s"
}
},
{
"key": "vtt_spanish_ai",
"container": "vtt",
"elementaryStreams": [
"webvtt_spanish_ai"
],
"segmentSettings": {
"segmentDuration": "2s"
}
}
],
"manifests": [
{
"key": "manifest_dash",
"fileName": "main.mpd",
"type": "DASH",
"muxStreams": [
"mux_video_fmp4",
"mux_audio_fmp4",
"vtt_english_ai",
"vtt_spanish_ai"
],
"maxSegmentCount": 5
},
{
"key": "manifest_hls",
"fileName": "main.m3u8",
"type": "HLS",
"muxStreams": [
"mux_video_ts",
"vtt_english_ai",
"vtt_spanish_ai"
],
"maxSegmentCount": 5
}
]
}
如要傳送要求,請展開以下其中一個選項:
您應該會收到如下的 JSON 回應:
您可以使用新的作業 ID 檢查作業結果。
重新啟動頻道並重新傳送輸入串流
在輸出資訊清單中驗證翻譯
執行下列指令,查看產生的 HLS 資訊清單內容:
gcloud storage cat gs://BUCKET_NAME/main.m3u8
AI 生成的英文和西班牙文 WebVTT 字幕會顯示在輸出資訊清單中,類似以下內容:
#EXT-X-MEDIA:TYPE=SUBTITLES,GROUP-ID="sub",LANGUAGE="en-US",NAME="English (AI captioned)",AUTOSELECT=YES,DEFAULT=YES,FORCED=NO,URI="vtt_english_ai/index-1.m3u8"
#EXT-X-MEDIA:TYPE=SUBTITLES,GROUP-ID="sub",LANGUAGE="es-MX",NAME="Spanish (AI captioned)",AUTOSELECT=NO,DEFAULT=NO,FORCED=NO,URI="vtt_spanish_ai/index-1.m3u8"
設定要發布的 AI 生成轉錄稿
設定 AI 生成的字幕和翻譯以供發布前,請先熟悉如何將直播發布至遠端端點。
如要將 AI 生成的字幕和翻譯內容發布至遠端端點,請勿參照先前建立含 AI 生成字幕的頻道和更新頻道翻譯時,以 webvtt 格式設定的文字串流。用於發布的文字串流必須是內嵌字幕,且 codec 設為 cea608 或 cea708。
在管道設定中,請注意下列事項:
一個
ElementaryStream(TextStream) 用於字幕,另一個ElementaryStream(TextStream) 則用於翻譯。{ "key": "embedded_english_ai", "textStream": { "codec": "cea608", "displayName": "English (AI captioned)", "languageCode": "en-US", "outputCeaChannel": "CC1", // closed caption channel that will contain AI-generated English captions "mapping": [ { "inputTrack": 1 // audio track number } ] } } { "key": "embedded_spanish_ai", "textStream": { "codec": "cea608", "displayName": "Spanish (AI captioned)", "languageCode": "es-MX", "outputCeaChannel": "CC2", // closed caption channel that will contain AI-generated Spanish captions "mapping": [ { "inputTrack": 1, // audio track number "fromLanguageCode": "en-US" // original audio in English } ] } }TextStreamcodec欄位必須設為cea608或cea708。TextStreammapping欄位會使用inputTrack指定要從哪個輸入音軌生成字幕。對應檔不得包含inputCeaChannel欄位,這個欄位只用於使用者提供的隱藏式輔助字幕。將
outputCeaChannel設為字幕頻道,在發布串流中加入 AI 生成的轉錄稿。將
languageCode設為音軌中使用的語言。基本串流會包含在
DistributionStream物件的elementaryStreams欄位中。{ "distributionStreams": [ { "key": "ds1", "container": "ts", // to be used with an SRT distribution "elementaryStreams": [ "es_video", "es_audio", "embedded_english_ai", // distribute both English "embedded_spanish_ai" // and Spanish captions ] }, { "key": "ds2", "container": "flv", // to be used with an RTMP distribution "elementaryStreams": [ "es_video", "es_audio", "embedded_spanish_ai" // only distribute Spanish captions ] } ] }
按照將直播內容發布至遠端端點指南完成頻道建立程序。
進階設定
在頻道層級設定 AutoTranscriptionConfig,進一步根據需求調整 AI 生成的文字串流。
字幕顯示時間碼
根據預設,AI 生成的字幕會與音訊和影片非同步顯示。將 AutoTranscriptionConfig 中的 DisplayTiming 欄位設為 SYNC,即可同步顯示這些欄位。
{
"autoTranscriptionConfig": {
"displayTiming": "SYNC"
}
}
同步顯示字幕會縮短音訊和文字之間的觀看延遲時間,但會增加整體端對端媒體延遲時間。
畫質預設
使用 AutoTranscriptionConfig 中的 QualityPreset 欄位,設定 AI 生成文字串流的品質偏好設定。
舉例來說,您可以設定以下項目,進一步縮短文字串流生成作業的延遲時間:
{
"autoTranscriptionConfig" : {
"qualityPreset": "LOW_LATENCY"
}
}
清除所用資源
停止管道
使用 projects.locations.channels.stop 方法停止管道。你必須先停止頻道,才能刪除。
使用任何要求資料之前,請先替換以下項目:
PROJECT_NUMBER:您的 Google Cloud 專案編號;這位於「專案編號」欄位,在「IAM 設定」頁面上LOCATION:頻道所在位置;請使用支援的區域之一顯示地區us-central1us-east1us-east4us-west1us-west2northamerica-northeast1southamerica-east1asia-east1asia-east2asia-south1asia-northeast1asia-southeast1australia-southeast1europe-north1europe-west1europe-west2europe-west3europe-west4
CHANNEL_ID:使用者定義的管道 ID
如要傳送要求,請展開以下其中一個選項:
您應該會收到如下的 JSON 回應:
停止輸入串流
如果使用 ffmpeg 傳送輸入串流,停止頻道後,連線會自動中斷。
刪除頻道
使用 projects.locations.channels.delete 方法刪除頻道。您必須先刪除頻道,才能刪除頻道使用的輸入端點。
使用任何要求資料之前,請先替換以下項目:
PROJECT_NUMBER:您的 Google Cloud 專案編號;這位於「專案編號」欄位,在「IAM 設定」頁面上LOCATION:頻道所在位置;請使用支援的區域之一顯示地區us-central1us-east1us-east4us-west1us-west2northamerica-northeast1southamerica-east1asia-east1asia-east2asia-south1asia-northeast1asia-southeast1australia-southeast1europe-north1europe-west1europe-west2europe-west3europe-west4
CHANNEL_ID:使用者定義的管道 ID
如要傳送要求,請展開以下其中一個選項:
您應該會收到如下的 JSON 回應:
刪除輸入端點
使用 projects.locations.inputs.delete 方法刪除輸入端點。
使用任何要求資料之前,請先替換以下項目:
PROJECT_NUMBER:您的 Google Cloud 專案編號;這位於「專案編號」欄位,在「IAM 設定」頁面上LOCATION:輸入端點所在位置;請使用其中一個支援的區域顯示地區us-central1us-east1us-east4us-west1us-west2northamerica-northeast1southamerica-east1asia-east1asia-east2asia-south1asia-northeast1asia-southeast1australia-southeast1europe-north1europe-west1europe-west2europe-west3europe-west4
INPUT_ID:輸入端點的使用者定義 ID
如要傳送要求,請展開以下其中一個選項:
您應該會收到如下的 JSON 回應:
刪除 Cloud Storage 值區
前往 Google Cloud 控制台的 Cloud Storage 瀏覽器頁面。
找出您建立的值區,並選取旁邊的核取方塊。
點選「刪除」。
在隨即顯示的對話方塊中,按一下「刪除」,即可刪除值區及其內容。