視訊完成時收到通知

本主題向您展示如何在 Zencoder 作業完成時獲得通知。

概覽

作業或輸出文件完成後,您可以通過多種方式了解它。

  • 我們可以向您的應用程序發送包含詳細信息的 HTTP 請求。
  • 您可以使用第三方工具(例如恩格羅克。當您在本地進行開發並且Zencoder無法訪問開發服務器時,這很有用。
  • 我們可以發送電子郵件。
  • 您可以檢查Zencoder儀表板的作業狀態。

獲取 HTTP/電子郵件通知

要接收 HTTP 或電子郵件通知,請將 通知選項 添加到您的 API 請求,如下所示:

{
  "api_key": "93h630j1dsyshjef620qlkavnmzui3",
  "input": "s3://bucket-name/file-name.avi",
  "notifications": [
  "http://user:password@example.com/zencoder",
  "admin@example.com"
  ],
  "outputs": [
  {
      "label": "web",
      "url": "s3://output-bucket/web.mp4",
      "size": "512x384",
      "notifications": [
      "http://user:password@example.com/zencoder",
      "admin@example.com"
      ]
  },
  {
      "label": "iphone",
      "url": "s3://output-bucket/iphone.m4v",
      "size": "480x320",
      "notifications": [
      {"format": "xml", "url": "http://user:password@example.com/zencoder1"},
      {"format": "json", "url": "http://user:password@example.com/zencoder2"},
      "http://example.com/zencoder3",
      "admin@example.com"
      ]
  }
  ]
}

在此示例中,當第一個輸出文件完成時,將發送兩個通知:

  • 通過電子郵件發送給admin@example.com有關輸出和輸入/作業的信息。
  • HTTP POST http://user:password@example.com/zencoder與內容類型頭設置為「應用程序/JSON」與有關輸出和輸入/作業的信息。

當第二個輸出文件完成時,將發送另外四個通知:

  • HTTP POST http://example.com/zencoder1與內容類型頭設置為「應用程序/XML」與有關輸出和輸入信息。
  • HTTP POST http://example.com/zencoder2與內容類型頭設置為「應用程序/JSON」與有關輸出和輸入/作業的信息。
  • 通過電子郵件發送給admin@example.com有關輸出和輸入/作業的信息。
  • HTTP POST http://example.com/zencoder3與內容類型頭設置為「應用程序/JSON」與有關輸出和輸入/作業的信息。

作業完成後,將發送另外兩個通知:

  • admin@example.com有關輸出和輸入的信息以電子郵件發送給。
  • HTTP POST http://user:password@example.com/zencoder與內容類型頭設置為「應用程序/JSON」與有關輸出和輸入的信息。

注意:如果在發送通知時Zencoder無法成功連接到目標服務器,則該通知將排隊等待重試。嘗試通知最多20次,每次嘗試之間的延遲加倍。

HTTP 通知

通知包含豐富的內容,其中包含有關作業以及輸入和輸出文件的更多信息。通知正文可以包含 JSON 或 XML。如果上傳失敗,通知還將包含有關 錯誤和使用的備份服務器的信息。

工作通知示例

{
  "outputs":[
  {
      "height":120,
      "audio_sample_rate":8000,
      "frame_rate":8.0,
      "channels":"1",
      "duration_in_ms":1920,
      "video_bitrate_in_kbps":70,
      "video_codec":"h264",
      "format":"mpeg4",
      "audio_codec":"aac",
      "label":null,
      "file_size_in_bytes":17938,
      "width":160,
      "audio_bitrate_in_kbps":9,
      "id":235314,
      "total_bitrate_in_kbps":79,
      "state":"finished",
      "url":"ftp://example.com/file.mp4",
      "md5_checksum":"7f106918e02a69466afa0ee014172496",
      "thumbnails": [
      {
          "label":"poster",
          "images":
          [
          {
              "url": "ftp://example.com/images/123.png",
              "format": "PNG",
              "file_size_bytes": 1273573,
              "dimensions": "1280x720"
          }
          ]
      }
      ]
  },
  {
      "height":120,
      "audio_sample_rate":8000,
      "frame_rate":8.0,
      "channels":"1",
      "duration_in_ms":1920,
      "video_bitrate_in_kbps":70,
      "video_codec":"h264",
      "format":"mpeg4",
      "audio_codec":"aac",
      "label":null,
      "file_size_in_bytes":17938,
      "width":160,
      "audio_bitrate_in_kbps":9,
      "id":235314,
      "total_bitrate_in_kbps":79,
      "state":"finished",
      "url":"ftp://example.com/file.mp4",
      "md5_checksum":"7f106918e02a69466afa0ee014172496",
      "thumbnails": [
      {
          "label":"poster",
          "images":
          [
          {
              "url": "ftp://example.com/images/123.png",
              "format": "PNG",
              "file_size_bytes": 1273573,
              "dimensions": "1280x720"
          }
          ]
      }
      ]
  }
  ],
  "job":{
      "created_at":"2011-09-27T04:20:10Z",
      "pass_through":null,
      "updated_at":"2011-09-27T04:21:18Z",
      "submitted_at":"2011-09-27T04:20:10Z",
      "id":172151,
      "state":"finished"
  },
  "input":{
      "height":120,
      "audio_sample_rate":8000,
      "frame_rate":8.0,
      "channels":"1",
      "duration_in_ms":1552,
      "video_bitrate_in_kbps":32,
      "video_codec":"mpeg4",
      "format":"mpeg4",
      "audio_codec":"aac",
      "file_size_in_bytes":13960,
      "width":160,
      "audio_bitrate_in_kbps":9,
      "id":172149,
      "state":"finished",
      "total_bitrate_in_kbps":41,
      "md5_checksum":"7f106918e02a69466afa0ee014174143"
  }
}

輸出通知示例

{
  "output":{
      "height":120,
      "audio_sample_rate":8000,
      "frame_rate":8.0,
      "channels":"1",
      "duration_in_ms":1920,
      "video_bitrate_in_kbps":70,
      "video_codec":"h264",
      "format":"mpeg4",
      "audio_codec":"aac",
      "label":null,
      "file_size_in_bytes":17938,
      "width":160,
      "audio_bitrate_in_kbps":9,
      "id":235314,
      "total_bitrate_in_kbps":79,
      "state":"finished",
      "url":"ftp://example.com/file.mp4",
      "md5_checksum":"7f106918e02a69466afa0ee014172496",
      "thumbnails":
      [
      {
          "label":"poster",
          "images":
          [
          {
              "url": "ftp://example.com/images/123.png",
              "format": "PNG",
              "file_size_bytes": 1273573,
              "dimensions": "1280x720"
          }
          ]
      }
      ]
  },
  "job":{
      "created_at":"2011-09-27T04:20:10Z",
      "pass_through":null,
      "updated_at":"2011-09-27T04:21:18Z",
      "submitted_at":"2011-09-27T04:20:10Z",
      "id":172151,
      "state":"finished"
  },
  "input":{
      "height":120,
      "audio_sample_rate":8000,
      "frame_rate":8.0,
      "channels":"1",
      "duration_in_ms":1552,
      "video_bitrate_in_kbps":32,
      "video_codec":"mpeg4",
      "format":"mpeg4",
      "audio_codec":"aac",
      "file_size_in_bytes":13960,
      "width":160,
      "audio_bitrate_in_kbps":9,
      "id":172149,
      "state":"finished",
      "total_bitrate_in_kbps":41,
      "md5_checksum":"7f106918e02a69466afa0ee014174143"
  }
}

示例通知處理程序

下面是一個用於處理通知的簡單 PHP 應用程序。

getMessage();
      $notification = $json;
  }

  $logEntry = $notification."\n \n";

  // Tell PHP where it can find the log file and tell PHP to open it
  // and add the string we created earlier to it.
      $logFileLocation = "live-log.txt";
      $fileHandle      = fopen($logFileLocation, 'a') or die("-1");
      fwrite($fileHandle, $logEntry);
      fclose($fileHandle);

  // line below is displayed when you browse the app directly
  echo "Zencoder callback app is running";
  ?>