0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

IBM Video Streaming(旧Ustream)配信時の視聴者人数取得方法

Last updated at Posted at 2023-01-08

概要

配信時に視聴者が何人だったかを自動取得できるようにしてほしいというリクエストがあったので
それを作ったときのメモ

使うもの

IBM Video StreamingがAPIを公開しているのでそれを使います。
https://developers.video.ibm.com/channel-api-basic-channel-management

チャンネルの基本情報の中に視聴者数があります。
stats.viewer_total

PHPでのサンプルプログラム

sample.php
$data = file_get_contents("https://api.video.ibm.com/channels/【配信チャンネルのID】.json");
if ($data == null) {
    return;
}
$json = json_decode($data);
$stats = $json->channel->stats;
if ($stats == null) {
    return;
}

$viewer = @$stats->viewer;
if ($viewer == null) {
    return;
}

echo $viewer;

これで配信時は視聴者数を取ることができます。
実際には配信時間中にcronで1分間に1回実行して、そのデータをDBに突っ込んで
配信期間の最大視聴者数を取得してメールなどで通知しています。

注意点

データが少し遅れてやってくる

視聴者数はビデオ画面に表示されているものとAPIで取得したものとで差があります。
APIで取得すると2~3分前の情報が戻ってきている雰囲気(体感

APIのlimit rate

多分APIごとに違いますかね。
レスポンスのhttpヘッダで確認することができます。
https://developers.video.ibm.com/api-basics-rate-limits

実際にたたいてみたところこんな感じ

X-RateLimit-Limit	120
X-RateLimit-Remaining	119
X-RateLimit-Reset	1673176740

私の環境だと1分間に120回叩いても大丈夫そうです。

おまけ

APIのエンドポイントはapi.ustream.tvもまだ使うことができました。

nslookupで確認してみると

> api.ustream.tv
サーバー:  dns.google
Address:  8.8.8.8

権限のない回答:
名前:    api.ustream.tv
Addresses:  169.44.146.91
          169.44.146.92
          169.45.159.77
          169.45.159.76

> api.video.ibm.com
サーバー:  dns.google
Address:  8.8.8.8

権限のない回答:
名前:    api.video.ibm.com
Addresses:  169.44.146.91
          169.45.159.77
          169.45.159.76
          169.44.146.92

となっていたので、同じサーバを見に行ってますね。
まあドメイン的にはapi.video.ibm.comにしておいた方がいいんでしょうけど
過去の資産がそのまま使えるのが好印象でした。

参考

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?