LoginSignup
11
13

More than 5 years have passed since last update.

RICOH THETA S用のnode.jsモジュールを公開しました

Last updated at Posted at 2016-01-10

はじめに

表題の通りです。npmに公開しました。
https://www.npmjs.com/package/osc-client-theta_s

と言っても、ゼロから作ったわけではありません。
RICOH THETA S用のAPI(RICOH THETA API v2)は、Google社のOpen Spherical Camera API Version 1.0(OSC)に準拠していますが、このOSC用のクライアントモジュールはosc-clientとして既に公開されているので、大部分はそれを使っています。

npm install osc-client-theta_s

でインストールできます。

使い方

上記の通り、大部分はosc-clientをそのまま使っているので、静止画撮影やオプションの取得/設定はosc-clientと全く同じです。

一方、動画撮影などはosc-clientにはないTHETA API v2の独自拡張なので、それに対応する形でosc-clientを拡張して作っています。
使い方は、下記のような手順です。

THETA API v2の独自拡張コマンドは、コマンド名の先頭に"_"(アンダースコア)が付いていますが、このモジュールのメソッド名には必要ありません。
一方、オプション名前/値は、"_"をつける必要があります。
ご注意ください。

var ThetaSOscClient = require('osc-client-theta_s').ThetaSOscClient;

var _thetaClient = new ThetaSOscClient();
var sessionId;

// 動画撮影開始
// 1. セッションIDの取得
// 2. キャプチャモードを「動画」に変更
// 3. 撮影開始
_thetaClient.startSession().then(function(res){
  sessionId = res.body.results.sessionId;
  return _thetaClient.setOptions(_sessionId, {captureMode:"_video"})
}).then(function(res){
  return _thetaClient.startCapture(_sessionId);
}).catch(function (error) {
  console.log(error);
});



// 動画撮影終了
// 1. 撮影終了
// 2. 最新撮影動画の情報を取得
// 3. 動画データの取得
// 4. 動画データの保存
// 5. セッションの終了
_thetaClient.stopCapture(_sessionId)
.then(function(res){
  return _thetaClient.listAll({entryCount:1, sort:"newest"});
}).then(function(res){
  return _thetaClient.getVideo(res.body.results.entries[0], "full");
}).then(function(res){
  fs.writeFile(filename, res.body);
}).then(function(err){
  return client.closeSession(sessionId);
}).catch(function (error) {
  console.log(error);
});

開発秘話

osc-client(というか、その下位モジュールのsuperagent)では、バイナリーデータのダウンロードが画像(ContentTypeがimage/***のもの)だけしか対応しておらず、そのままではgetVideoメソッドには対応できませんでした。
なので、このTHETA S用モジュールは、少しトリッキーな方法で動画ダウンロード対応しています。
具体的には、getVideoメソッド開始時にsuperagentのprototypeから該当処理を一時的に差し替えています。getVideo終了時に元に戻していますが、もしかしたら、他でもsuperagentを使用していた場合、悪影響があるかもしれません。ご注意ください。

11
13
1

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
11
13