private void StartRecording(string savePath)
        {
            if (isRecording || isPlaying)
            {
                return;
            }

            this.savePath = savePath;

            writer = new VideoWriter();
            writer.open(savePath, VideoWriter.fourcc('M', 'J', 'P', 'G'), 30, new Size(Screen.width, Screen.height));

            if (!writer.isOpened())
            {
                Debug.LogError("writer.isOpened() false");
                writer.release();
                return;
            }

            screenCapture        = new Texture2D(Screen.width, Screen.height, TextureFormat.RGB24, false);
            recordingFrameRgbMat = new Mat(Screen.height, Screen.width, CvType.CV_8UC3);
            frameCount           = 0;

            isRecording = true;
        }
Ejemplo n.º 2
0
 public void Close()
 {
     _writer.release();
     #if !UNITY_EDITOR && UNITY_ANDROID
     if (!File.Exists(_filePath))
     {
         return;
     }
     try {
         NativeGallery.SaveVideoToGallery(_filePath, "InvisibleCamera", _fileName);
         File.Delete(_filePath);
     }
     catch (Exception e) {
         a = e.Message;
     }
     #endif
 }
Ejemplo n.º 3
0
        private void StopRecording()
        {
            if (!isRecording || isPlaying)
            {
                return;
            }

            if (writer != null && !writer.IsDisposed)
            {
                writer.release();
            }

            if (recordingFrameRgbMat != null && !recordingFrameRgbMat.IsDisposed)
            {
                recordingFrameRgbMat.Dispose();
            }

            isRecording = false;
        }
Ejemplo n.º 4
0
        void OnApplicationQuit()
        {
            if (!(m_vidCapturer is null))
            {
                m_vidCapturer.release();
                m_vidCapturer = null;
            }

            if (!(m_vidWriter is null))
            {
                m_vidWriter.release();
                m_vidWriter = null;
            }

            if (!(m_distortedRT is null))
            {
                m_distortedRT.Release();
                m_distortedRT = null;
            }
        }
Ejemplo n.º 5
0
        private void StopRecording()
        {
            if (!isRecording || isPlaying)
            {
                return;
            }

            if (writer != null && !writer.IsDisposed)
            {
                writer.release();
            }

            if (recordingFrameRgbMat != null && !recordingFrameRgbMat.IsDisposed)
            {
                recordingFrameRgbMat.Dispose();
            }

            savePathInputField.text = savePath;
            Debug.Log("savePath " + savePath);

            isRecording = false;
        }
Ejemplo n.º 6
0
        public IEnumerator MakeVideo(TMPro.TMP_Text progressDisplay, TMPro.TMP_Text statusDisplay)
        {
            // 1. Create video capture
            m_vidCapturer = new VideoCapture(m_vidName);
            // capture validity check
            if (!m_vidCapturer.isOpened())
            {
                m_vidCapturer.release();
                m_vidCapturer = null;
                DanbiUtils.LogErr($"Failed to open the selected video at {m_vidName}");
                yield break;
            }

            // 2. init persistant resources
            receivedFrameMat  = new Mat((int)m_vidCapturer.get(4), (int)m_vidCapturer.get(3), CvType.CV_8UC4); // CV_8UC4 (RGBA).
            distortedFrameMat = new Mat((int)m_vidCapturer.get(4), (int)m_vidCapturer.get(3), CvType.CV_8UC4); // CV_8UC4 (RGBA).
            texForVideoFrame  = new Texture2D((int)m_vidCapturer.get(3), (int)m_vidCapturer.get(4), TextureFormat.RGBA32, false);

            // 4. calc video frame counts.
            m_currentFrameCount = 0;
            m_maxFrameCount     = (int)m_vidCapturer?.get(DanbiOpencvVideoCapturePropID.frame_count);

            // 5. get a codec for a video writer.
            // MJPG -> error!
            int codec_fourcc = DanbiOpencvVideoCodec_fourcc.get_fourcc_videoCodec(m_videoCodec);

            if (codec_fourcc == -999)
            {
                DanbiUtils.LogErr($"codec is invalid! codec propID -> {codec_fourcc}");
                yield break;
            }

            // 6. create a video writer
            var frameSize = new Size(m_vidCapturer.get(3), m_vidCapturer.get(4)); // width , height

            m_vidWriter = new VideoWriter(m_savedVideoPathAndName, codec_fourcc, m_targetFrameRate, frameSize, true);

            // while (m_currentFrameCount < m_dbgMaxFrameCount)
            while (m_currentFrameCount < m_maxFrameCount - 1)
            {
                if (m_isSaving)
                {
                    break;
                }

                // read the new Frame into 'newFrameMat'.
                if (!m_vidCapturer.read(receivedFrameMat))
                {
                    DanbiUtils.LogErr($"Failed to read the current video frame! <No next frame>");
                    break;
                }

                // testRT = new Mat((int)receivedFrameMat.get(4), (int)receivedFrameMat.get(3), CvType.CV_8UC4);
                // OpenCVForUnity.ImgprocModule.Imgproc.cvtColor(receivedFrameMat, testRT, OpenCVForUnity.ImgprocModule.Imgproc.COLOR_RGBA)

                if (receivedFrameMat.empty())
                {
                    DanbiUtils.LogErr("Frame failed to receive the captured frame from the video!");
                    break;
                }

                Utils.matToTexture2D(receivedFrameMat, texForVideoFrame);

                yield return(StartCoroutine(DistortCurrentFrame(texForVideoFrame)));

                Utils.texture2DToMat(texForVideoFrame, distortedFrameMat);

                if (distortedFrameMat.empty())
                {
                    DanbiUtils.LogErr("Frame failed to receive the distorted result!");
                    break;
                }

                // write the newFrameMat into the video writer
                m_vidWriter.write(distortedFrameMat);

                // TODO: update the text with DanbiStatusDisplayHelper
                // progressDisplayText.text = $"Start to warp" +
                //   "(500 / 25510) " +
                //   "(1.96001%)";
                // TODO: update the text with DanbiStatusDisplayHelper
                // statusDisplayText.text = "Image generating succeed!";

                ++m_currentFrameCount;
            }

            // dispose resources.
            m_vidCapturer.release();
            m_vidWriter.release();
            receivedFrameMat.release();
            distortedFrameMat.release();
            texForVideoFrame = null;

            // reset flags
            DanbiManager.instance.renderFinished = false;
            m_isSaving = false;

            Application.runInBackground = false;

            // wait the saved file
            yield return(new WaitUntil(() => new System.IO.FileInfo(m_savedVideoPathAndName).Exists));

            System.Diagnostics.Process.Start(@"" + m_savedVideoPath);
        }