見出し画像

Nreal / ビデオキャプチャ

1. ビデオキャプチャ

「RGBCamera-Record」は、ビデオキャプチャ(MR映像の録画)を行うサンプルです。

画像1


操作方法は次のとおりです。

・Startボタンのクリック: ビデオキャプチャの開始。
・Stopボタンのクリック: ビデオキャプチャの停止。
・Home Buttonのクリック: プレビューの表示・非表示の切り替え

動画ファイルは、「/sdcard/RecordVideos/」に保存されます。

2. ビデオキャプチャのサンプルの利用

ビデオキャプチャのサンプルの使い方は、次のとおりです。

(1) シーン「Assets/NRSDK/Demos/RGBCamera-Record」を開く。
(2) サンプルをビルドして実行。

3. 動画ファイルの確認

動画ファイルの確認は、「adb shell」で行います。

$ adb shell
$ cd /sdcard/RecordVideos/
$ ls
TestVideo_1137604.mp4
$ exit

4. 動画ファイルの取得

動画ファイルの取得は、「adb pull」で行います。

$ adb pull /sdcard/RecordVideos/TestVideo_1137604.mp4

5. ビデオキャプチャの使用方法

ビデオキャプチャの使用方法の例については、「Assets/NRSDK/Demos/Record/Scripts/VideoCaptureLocalExample/CameraCaptureController.cs」を参照してください。

public class VideoCaptureLocalExample : MonoBehaviour
{
    public NRPreviewer Previewer; // プレビューワ

    // 保存ファイルのパスの取得
    public string SdcardSavePath
    {
        get
        {
            string timeStamp = Time.time.ToString().Replace(".", "").Replace(":", "");
            string filename = string.Format("TestVideo_{0}.mp4", timeStamp);
            string basepath = "/sdcard/RecordVideos/";
            if (!Directory.Exists(basepath))
            {
                Directory.CreateDirectory(basepath);
            }
            string filepath = Path.Combine(basepath, filename);
            return filepath;
        }
    }

    NRVideoCapture m_VideoCapture = null; // ビデオキャプチャ

    // スタート時に呼ばれる
    void Start()
    {
        // ビデオキャプチャーの生成
        CreateVideoCaptureTest();
    }

    // フレーム毎に呼ばれる
    void Update()
    {
        if (m_VideoCapture == null)
        {
            return;
        }

        // R or Touchpadのクリック
        if (Input.GetKeyDown(KeyCode.R) || NRInput.GetButtonDown(ControllerButton.TRIGGER))
        {
            // ビデオキャプチャーの開始
            StartVideoCapture();

            Previewer.SetData(m_VideoCapture.RecordBehaviour.PreviewTexture, true);
        }

        // T or Homeボタン
        if (Input.GetKeyDown(KeyCode.T) || NRInput.GetButtonDown(ControllerButton.HOME))
        {
            // ビデオキャプチャーの停止
            StopVideoCapture();

            Previewer.SetData(m_VideoCapture.RecordBehaviour.PreviewTexture, false);
        }
    }

    // ビデオキャプチャーの生成
    void CreateVideoCaptureTest()
    {
        NRVideoCapture.CreateAsync(false, delegate (NRVideoCapture videoCapture)
        {
            if (videoCapture != null)
            {
                m_VideoCapture = videoCapture;
            }
            else
            {
                Debug.LogError("Failed to create VideoCapture Instance!");
            }
        });
    }

    // ビデオキャプチャの開始
    private void StartVideoCapture()
    {
        Resolution cameraResolution = NRVideoCapture.SupportedResolutions.OrderByDescending((res) => res.width * res.height).First();
        Debug.Log(cameraResolution);

        int cameraFramerate = NRVideoCapture.GetSupportedFrameRatesForResolution(cameraResolution).OrderByDescending((fps) => fps).First();
        Debug.Log(cameraFramerate);

        if (m_VideoCapture != null)
        {
            Debug.Log("Created VideoCapture Instance!");
            CameraParameters cameraParameters = new CameraParameters();
            cameraParameters.hologramOpacity = 0.0f;
            cameraParameters.frameRate = cameraFramerate;
            cameraParameters.cameraResolutionWidth = cameraResolution.width;
            cameraParameters.cameraResolutionHeight = cameraResolution.height;
            cameraParameters.pixelFormat = CapturePixelFormat.BGRA32;
            cameraParameters.blendMode = BlendMode.Blend;

            m_VideoCapture.StartVideoModeAsync(cameraParameters,
                NRVideoCapture.AudioState.ApplicationAndMicAudio,
                OnStartedVideoCaptureMode);
        }
    }

    // ビデオキャプチャの停止
    private void StopVideoCapture()
    {
        Debug.Log("Stop Video Capture!");
        m_VideoCapture.StopRecordingAsync(OnStoppedRecordingVideo);
    }

    // ビデオキャプチャ開始時に呼ばれる
    void OnStartedVideoCaptureMode(NRVideoCapture.VideoCaptureResult result)
    {
        Debug.Log("Started Video Capture Mode!");
 
        // 録画の開始
        m_VideoCapture.StartRecordingAsync(SdcardSavePath, OnStartedRecordingVideo);
    }

    // ビデオキャプチャ停止時に呼ばれる
    void OnStoppedVideoCaptureMode(NRVideoCapture.VideoCaptureResult result)
    {
        Debug.Log("Stopped Video Capture Mode!");
    }

    // ビデオキャプチャ開始後に呼ばれる
    void OnStartedRecordingVideo(NRVideoCapture.VideoCaptureResult result)
    {
        Debug.Log("Started Recording Video!");
    }

    // ビデオキャプチャ停止後に呼ばれる
    void OnStoppedRecordingVideo(NRVideoCapture.VideoCaptureResult result)
    {
        Debug.Log("Stopped Recording Video!");
 
        // 録画の停止
        m_VideoCapture.StopVideoModeAsync(OnStoppedVideoCaptureMode);
    }
}


この記事が気に入ったらサポートをしてみませんか?