以近乎即時的方式預覽輸入串流

本頁說明如何以極低的延遲時間預覽輸入串流,以便近乎即時地偵測潛在問題或事件。您可以採取修正措施,例如暫停串流、插入廣告或顯示板岩,確保觀眾享有優質的觀看體驗。

預覽內容的格式如下:

  • 格式:MPEG-TS
  • 影片:
    • 解析度:640x480
    • 轉碼器:H.264
    • 畫面更新率:每秒 25 個影格 (FPS)
  • 音訊
    • 轉碼器:AAC
    • 取樣率:48 kHz
    • 通訊協定:HTTPS

事前準備

本頁面假設您已完成 HLS 直播快速入門導覽課程開始前」一節中的步驟。

建立輸入端點

如要建立輸入端點,請使用 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
}

這項指令會建立長期執行作業 (LRO),您可以使用這項作業追蹤要求進度。詳情請參閱管理長時間執行的作業

取得輸入端點詳細資料

如要取得輸入端點的詳細資料,請使用 projects.locations.inputs.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
  • INPUT_ID:輸入端點的使用者定義 ID

如要傳送要求,請展開以下其中一個選項:

您應該會收到如下的 JSON 回應:

{
  "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 方法。下列範例會建立管道,產生 HLS 直播串流。直播內容為單一高畫質 (1280x720) 轉譯版本。

使用任何要求資料之前,請先替換以下項目:

  • 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
      }
    }
  ],
  "muxStreams": [
    {
      "key": "mux_video_ts",
      "container": "ts",
      "elementaryStreams": ["es_video", "es_audio"],
      "segmentSettings": { "segmentDuration": "2s" }
    }
  ],
  "manifests": [
    {
      "key": "manifest_hls",
      "fileName": "main.m3u8",
      "type": "HLS",
      "muxStreams": [
        "mux_video_ts"
      ],
      "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
}

這項指令會建立長期執行作業 (LRO),您可以使用這項作業追蹤要求進度。詳情請參閱管理長時間執行的作業

啟動管道

如要啟動管道,請使用 projects.locations.channels.start 方法。

使用任何要求資料之前,請先替換以下項目:

  • 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
}

這項指令會建立長期執行作業 (LRO),您可以使用這項作業追蹤要求進度。詳情請參閱管理長時間執行的作業

安裝編碼器

如要使用 API,必須要有編碼器產生 API 處理的輸入串流。

安裝 ffmpeg,因為本頁面說明如何使用 ffmpeg 產生輸入串流。您可以在 Cloud Shell 中使用下列指令安裝這項工具。

sudo apt install ffmpeg

傳送輸入串流

開啟新的終端機視窗。使用「取得輸入端點詳細資料」一節中的 INPUT_STREAM_URI,執行下列指令。這個指令會產生測試串流。

ffmpeg -re -f lavfi -i "testsrc=size=1280x720 [out0]; sine=frequency=500 [out1]" \
  -acodec aac -vcodec h264 -f flv INPUT_STREAM_URI

取得端點網址和不記名權杖

如要取得預覽內容的端點網址和驗證所需的承載權杖,請使用 projects.locations.inputs.preview 方法。

使用任何要求資料之前,請先替換以下項目:

  • 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 回應:

{
  "uri":  ENDPOINT_URL, # For example, "preview.k3fhpvei3vvf.us-central1.livestream.goog/inputs/my-preview-input",
  "bearerToken": "BEARER_TOKEN"
}

您會在下一節中使用 uribearerToken 欄位的值。

顯示預覽內容

顯示預覽內容的主要選項如下:

mpegts.js

mpegts.js 是 GitHub 上的程式庫,可在網頁瀏覽器中播放 MPEG-TS 串流。

上一個步驟中收到的 bearerTokenuri 欄位值複製到下列 HTML 中。從下列 HTML 建立 HTML 檔案,並在瀏覽器中開啟。

<html>
  <head>
  <script src="https://xqq.im/mpegts.js/dist/mpegts.js"></script>
  <video id="video" autoplay controls muted></video>
  <script>
    function main() {
      if (!mpegts.getFeatureList().mseLivePlayback) {
        console.log("MSE live playback is not supported by the browswer!")
        return;
      }
      var videoElement = document.getElementById("video");
      player = mpegts.createPlayer(
        {
          type: "mse",
          isLive: true,
          <!-- Placeholder: Replace ENDPOINT_URL with the actual endpoint URL -->
          url: "ENDPOINT_URL",
        },
        {
          headers: {
            <!-- Placeholder: Replace BEARER_TOKEN with the actual bearer token -->
            Authorization: "BEARER_TOKEN",
          },
        }
      );
      player.attachMediaElement(videoElement);
      player.load();
      player.play();
    }
    main();
  </script>
  </body>
</html>

這時候應該就會看到測試模式播放,延遲時間比相關聯的即時串流短。這個串流播放器只能播放串流中的第一個音軌。

測試模式影片

FFplay

FFplay 是多功能的指令列媒體播放器,使用 FFmpeg 程式庫。如要下載 FFplay,請前往「下載 FFmpeg」頁面。

開啟新的終端機視窗。將上一個步驟中收到的 bearerTokenuri 欄位值複製到下列指令,然後執行。

ffplay -headers "Authorization: Bearer BEARER_TOKEN" \
  -fflags nobuffer -flags low_delay -probesize 32 -analyzeduration 0 \
  ENDPOINT_URL

這時候應該就會看到測試模式播放,延遲時間比相關聯的即時串流短。ffplay 支援按下「A」鍵切換音軌。

測試模式影片

清除所用資源

如要避免系統向您的 Google Cloud 帳戶收取本頁所用資源的費用,請按照下列步驟操作。

停止管道

使用 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
}

這項指令會建立長期執行作業 (LRO),您可以使用這項作業追蹤要求進度。詳情請參閱管理長時間執行的作業

停止輸入串流

如果使用 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
}

這項指令會建立長期執行作業 (LRO),您可以使用這項作業追蹤要求進度。詳情請參閱管理長時間執行的作業

刪除輸入端點

使用 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
}

這項指令會建立長期執行作業 (LRO),您可以使用這項作業追蹤要求進度。詳情請參閱管理長時間執行的作業

刪除 Cloud Storage 值區

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

    前往 Cloud Storage 瀏覽器頁面

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

  3. 點選「刪除」。

  4. 在隨即顯示的對話方塊中,按一下「刪除」,即可刪除值區及其內容。