本日はHoloLensの調査枠です。
現在キャプチャに関して調査を行っています。
今回はビデオのキャプチャに関して学びます。
〇ビデオキャプチャ
ビデオキャプチャとはフォトキャプチ ャ同様の流れで撮影しますが、FPSを記録することと.mp4として設定する必要があることです。
以下のような処理の流れになります。
①ビデオキャプチャオブジェクトを作成する
②設定用のカメラパラメータオブジェクトを作成する
③ビデオモードを起動する
④録画の開始
⑤録画の終了
⑥ビデオモードを停止しリソースをクリーンアップ
〇ビデオキャプチャオブジェクトの作成
void Start () { VideoCapture.CreateAsync(false, OnVideoCaptureCreated); }
UnityEngine.Windows.WebCam.VideoCapture.CreateAsync(showHolograms,onCreatedCallback)はデバイスのWebCamからディスクにビデオを録画するために使用されるオブジェクトを非同期に作成します。
引数の[showHolograms]はホログラムをビデオにキャプチャするかを指定します。
[onCreatedCallback]はビデオキャプチャが作成された際に呼び出されます。
〇録音に必要なパラメータを設定します。
void OnVideoCaptureCreated (VideoCapture videoCapture) { if (videoCapture != null) { m_VideoCapture = videoCapture; Resolution cameraResolution = VideoCapture.SupportedResolutions.OrderByDescending((res) => res.width * res.height).First(); float cameraFramerate = VideoCapture.GetSupportedFrameRatesForResolution(cameraResolution).OrderByDescending((fps) => fps).First(); CameraParameters cameraParameters = new CameraParameters(); cameraParameters.hologramOpacity = 0.0f; cameraParameters.frameRate = cameraFramerate; cameraParameters.cameraResolutionWidth = cameraResolution.width; cameraParameters.cameraResolutionHeight = cameraResolution.height; cameraParameters.pixelFormat = CapturePixelFormat.BGRA32; m_VideoCapture.StartVideoModeAsync(cameraParameters, VideoCapture.AudioState.None, OnStartedVideoCaptureMode); } else { Debug.LogError("Failed to create VideoCapture Instance!"); } }
UnityEngine.Windows.WebCam.CameraParametersはカメラを使用するためのパラメーターを含んだオブジェクトです。
CameraParametersを新規で作成し、パラメータを代入します。
CameraParametersが持つパラメーターは次のようになります。
・hologramOpacity
ホログラムの不透明度です。
・frameRate
キャプチャする際のフレームレートを指定します。
・cameraResolutionWidth
キャプチャする際の解像度の横幅です。
・cameraResolutionHeight
キャプチャする際の解像度の縦幅です。
・pixelFormat
録画時の画像データの記録形式です。
〇キャプチャモードの起動
void OnStartedVideoCaptureMode(VideoCapture.VideoCaptureResult result) { if (result.success) { string filename = string.Format("MyVideo_{0}.mp4", Time.time); string filepath = System.IO.Path.Combine(Application.persistentDataPath, filename); m_VideoCapture.StartRecordingAsync(filepath, OnStartedRecordingVideo); } }
ビデオキャプチャが作成されている場合ファイルネームと保存場所のパスを指定して記録を開始します。
StartRecordingAsyncは非同期にビデオモードを開始します。
〇録画をストップした際にビデオモードを停止し、リソースをクリーンアップ
void OnStoppedRecordingVideo(VideoCapture.VideoCaptureResult result) { Debug.Log("Stopped Recording Video!"); m_VideoCapture.StopVideoModeAsync(OnStoppedVideoCaptureMode); } void OnStoppedVideoCaptureMode(VideoCapture.VideoCaptureResult result) { m_VideoCapture.Dispose(); m_VideoCapture = null; }
以上の流れでビデオキャプチャを行っています。
〇コード全体
using System.Collections; using System.Collections.Generic; using UnityEngine; using System.Linq; using UnityEngine.Windows.WebCam; public class VideoCaptureTest : MonoBehaviour { VideoCapture m_VideoCapture; // Start is called before the first frame update void Start() { VideoCapture.CreateAsync(false, OnVideoCaptureCreated); } void OnVideoCaptureCreated(VideoCapture videoCapture) { if (videoCapture != null) { m_VideoCapture = videoCapture; Resolution cameraResolution = VideoCapture.SupportedResolutions.OrderByDescending((res) => res.width * res.height).First(); float cameraFramerate = VideoCapture.GetSupportedFrameRatesForResolution(cameraResolution).OrderByDescending((fps) => fps).First(); CameraParameters cameraParameters = new CameraParameters(); cameraParameters.hologramOpacity = 0.0f; cameraParameters.cameraResolutionWidth = cameraResolution.width; cameraParameters.cameraResolutionHeight = cameraResolution.height; cameraParameters.pixelFormat = CapturePixelFormat.BGRA32; videoCapture.StartVideoModeAsync(cameraParameters, VideoCapture.AudioState.None, OnStartedVideoCaptureMode); } else { } } void OnStartedVideoCaptureMode(VideoCapture.VideoCaptureResult result) { if (result.success) { string filename = string.Format("MyVideo_{0}.mp4", Time.time); string filepath = System.IO.Path.Combine(Application.persistentDataPath, filename); m_VideoCapture.StartRecordingAsync(filepath, OnStartedRecordingVideo); } } void OnStartedRecordingVideo(VideoCapture.VideoCaptureResult result) { Debug.Log("Started Recording Video!"); // We will stop the video from recording via other input such as a timer or a tap, etc. } void OnStoppedRecordingVideo(VideoCapture.VideoCaptureResult result) { Debug.Log("Stopped Recording Video!"); m_VideoCapture.StopVideoModeAsync(OnStoppedVideoCaptureMode); } void OnStoppedVideoCaptureMode(VideoCapture.VideoCaptureResult result) { m_VideoCapture.Dispose(); m_VideoCapture = null; } }