設定字幕

本頁說明如何為直播設定 Web Video Text Tracks (WebVTT) 格式的字幕。字幕是從輸入串流中的 cea608 或 cea708 字幕產生。

字幕支援 HTTP 即時串流和 DASH 直播。

事前準備

本頁面假設您已完成「開始前」一節中的步驟,也就是「HTTP 即時串流快速入門」或「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:為要建立的新輸入端點定義的 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:作業的 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-englishMuxStream

    {
      "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:要建立的管道使用者定義 ID;這個值必須介於 1 至 63 個字元之間,開頭和結尾須為 [a-z0-9],且字元之間可包含破折號 (-)
  • INPUT_ID:輸入端點的使用者定義 ID
  • BUCKET_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",
      "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:使用者定義的管道 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:使用者定義的管道 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 (或其他格式) 影片,然後使用 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"

播放系統產生的即時串流內容

如要在 Shaka Player 中播放產生的媒體檔案,請完成下列步驟:

  1. 將您建立的 Cloud Storage bucket 設為可公開讀取
  2. 如要在 Cloud Storage bucket 上啟用跨源資源共享 (CORS),請按照下列步驟操作:
    1. 建立包含下列內容的 JSON 檔案:
      [
        {
          "origin": ["https://shaka-player-demo.appspot.com/"],
          "responseHeader": ["Content-Type", "Range"],
          "method": ["GET", "HEAD"],
          "maxAgeSeconds": 3600
        }
      ]
    2. 執行下列指令,並將 JSON_FILE_NAME 替換為您在上一個步驟中建立的 JSON 檔案名稱:
      gcloud storage buckets update gs://BUCKET_NAME --cors-file=JSON_FILE_NAME.json
  3. 在 Cloud Storage bucket 中,找出系統產生的 main.m3u8 檔案。在該檔案的「Public access」(公開存取權) 資料欄中,按一下「Copy URL」(複製網址)
  4. 前往 Shaka Player (這是一個線上即時串流播放器)。
  5. 按一下頂端導覽列中的「Custom Content」(自訂內容)
  6. 按一下「+」按鈕。
  7. 將檔案的公開網址貼到「Manifest URL」(資訊清單網址) 方塊中。

  8. 在「Name」(名稱) 方塊中輸入名稱。

  9. 按一下「Save」(儲存)

  10. 按一下「Play」(播放)

這時應該就會看到直播內容。你可能需要在 Shaka Player UI 中啟用字幕。

清除所用資源

停止管道

使用 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:使用者定義的管道 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:使用者定義的管道 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:輸入端點的使用者定義 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 值區

  1. 前往 Google Cloud 控制台的 Cloud Storage 瀏覽器頁面。

    前往 Cloud Storage 瀏覽器頁面

  2. 找出您建立的值區,並選取旁邊的核取方塊。

  3. 點選「刪除」。

  4. 在出現的對話方塊中,按一下「刪除」,即可永久刪除值區及其內容。