Exemplo n.º 1
0
    public void CreateVideoFromByteArray(string filePath, int width, int height, int frameRate)
    {
        byte[]   frameData   = new byte[width * height * 4];
        GCHandle frameHandle = GCHandle.Alloc(frameData, GCHandleType.Pinned);

        // Start the recording session
        int encoderHandle = AVProMovieCapturePlugin.CreateRecorderAVI(filePath, (uint)width, (uint)height, frameRate, (int)AVProMovieCapturePlugin.PixelFormat.RGBA32, false, _videoCodecIndex, false, 0, 0, -1, -1, false, false, false);

        if (encoderHandle >= 0)
        {
            AVProMovieCapturePlugin.Start(encoderHandle);

            // Write out 100 frames
            int numFrames = 100;
            for (int i = 0; i < numFrames; i++)
            {
                // TODO: update the byte array with your data :)


                // Wait for the encoder to be ready for the next frame
                int numAttempts = 32;
                while (numAttempts > 0)
                {
                    if (AVProMovieCapturePlugin.IsNewFrameDue(encoderHandle))
                    {
                        // Encode the new frame
                        AVProMovieCapturePlugin.EncodeFrame(encoderHandle, frameHandle.AddrOfPinnedObject());
                        break;
                    }
                    System.Threading.Thread.Sleep(1);
                    numAttempts--;
                }
            }

            // End the session
            AVProMovieCapturePlugin.Stop(encoderHandle, false);
            AVProMovieCapturePlugin.FreeRecorder(encoderHandle);
        }

        if (frameHandle.IsAllocated)
        {
            frameHandle.Free();
        }
    }
    public virtual void PrepareCapture()
    {
        // Disable vsync
        if (QualitySettings.vSyncCount > 0)
        {
            _oldVSyncCount = QualitySettings.vSyncCount;
            //Debug.LogWarning("For best results vsync should be disabled during video capture.  Disabling vsync.");
            QualitySettings.vSyncCount = 0;
        }

        if (_isRealTime)
        {
            Application.targetFrameRate = (int)_frameRate;
        }
        else
        {
            Time.captureFramerate = (int)_frameRate;
        }

        Debug.Log("[AVProMovieCapture] New movie capture: (" + _targetWidth + "x" + _targetHeight + " @ " + ((int)_frameRate).ToString() + "fps) to file '" + _filePath + "'");
        Debug.Log("[AVProMovieCapture] Using video codec: '" + _codecName + "' audio device: '" + _audioDeviceName + "'");

        int  audioDeviceIndex = _audioDeviceIndex;
        int  audioCodecIndex  = _audioCodecIndex;
        bool noAudio          = _noAudio;

        if (_noAudio || (_audioCapture == null && _audioCodecIndex < 0))
        {
            audioCodecIndex = audioDeviceIndex = -1;
            noAudio         = true;
        }

        _handle = AVProMovieCapturePlugin.CreateRecorderAVI(_filePath, (uint)_targetWidth, (uint)_targetHeight, (int)_frameRate,
                                                            (int)(_pixelFormat), _isTopDown, _codecIndex, !noAudio, audioDeviceIndex, audioCodecIndex, _isRealTime);

        if (_handle < 0)
        {
            Debug.LogWarning("[AVProMovieCapture] Failed to create recorder");
            StopCapture();
        }
    }
Exemplo n.º 3
0
    public virtual bool PrepareCapture()
    {
        // Delete file if it already exists
        if (File.Exists(_filePath))
        {
            File.Delete(_filePath);
        }

#if UNITY_EDITOR_WIN || UNITY_STANDALONE_WIN
        if (_minimumDiskSpaceMB > 0)
        {
            ulong freespace = 0;
            if (DriveFreeBytes(System.IO.Path.GetPathRoot(_filePath), out freespace))
            {
                _freeDiskSpaceMB = (long)(freespace / (1024 * 1024));
            }

            if (!IsEnoughDiskSpace())
            {
                Debug.LogError("[AVProMovieCapture] Not enough free space to start capture.  Stopping capture.");
                return(false);
            }
        }
#endif

        // Disable vsync
        if (_allowVSyncDisable && !Screen.fullScreen && QualitySettings.vSyncCount > 0)
        {
            _oldVSyncCount             = QualitySettings.vSyncCount;
            QualitySettings.vSyncCount = 0;
        }

        if (_isRealTime)
        {
            if (_allowFrameRateChange)
            {
                _oldTargetFrameRate         = Application.targetFrameRate;
                Application.targetFrameRate = (int)_frameRate;
            }
        }
        else
        {
            if (_useMotionBlur && _motionBlurSamples > 1 && _motionBlurCameras.Length > 0)
            {
                Time.captureFramerate = _motionBlurSamples * (int)_frameRate;

                // Setup the motion blur filters
                foreach (Camera camera in _motionBlurCameras)
                {
                    AVProMovieCaptureMotionBlur mb = camera.GetComponent <AVProMovieCaptureMotionBlur>();
                    if (mb == null)
                    {
                        mb = camera.gameObject.AddComponent <AVProMovieCaptureMotionBlur>();
                    }
                    if (mb != null)
                    {
                        mb.NumSamples = _motionBlurSamples;
                        _motionBlur   = mb;
                    }

                    mb.enabled = true;
                }
            }
            else
            {
                Time.captureFramerate = (int)_frameRate;
            }

            // Change physics update speed
            _oldFixedDeltaTime  = Time.fixedDeltaTime;
            Time.fixedDeltaTime = 1.0f / Time.captureFramerate;
        }

        int  audioDeviceIndex = _audioDeviceIndex;
        int  audioCodecIndex  = _audioCodecIndex;
        bool noAudio          = _noAudio;
        if (!_isRealTime)
        {
            noAudio = true;
        }

        // We if try to capture audio from Unity but there isn't an AVProUnityAudioCapture component set
        if (!noAudio && _audioCapture == null && _audioDeviceIndex < 0)
        {
            // Try to find it locally
            _audioCapture = this.GetComponent <AVProUnityAudioCapture>();
            if (_audioCapture == null)
            {
                // Try to find it globally
                _audioCapture = GameObject.FindObjectOfType <AVProUnityAudioCapture>();
            }

            if (_audioCapture == null)
            {
                AudioListener audioListener = this.GetComponent <AudioListener>();
                if (audioListener == null)
                {
                    audioListener = GameObject.FindObjectOfType <AudioListener>();
                }
                if (audioListener != null)
                {
                    _audioCapture = audioListener.gameObject.AddComponent <AVProUnityAudioCapture>();
                    Debug.LogWarning("[AVProMovieCapture] Capturing audio from Unity without an AVProUnityAudioCapture assigned so we had to create one manually (very slow).  Consider adding a AVProUnityAudioCapture component to your scene and assigned it to this MovieCapture component.");
                }
                else
                {
                    noAudio = true;
                    Debug.LogWarning("[AVProMovieCapture] No audio listener found in scene.  Unable to capture audio from Untiy.");
                }
            }
            else
            {
                Debug.LogWarning("[AVProMovieCapture] Capturing audio from Unity without an AVProUnityAudioCapture assigned so we had to search for one manually (very slow)");
            }
        }

        if (noAudio || (_audioCapture == null && _audioDeviceIndex < 0))
        {
            audioCodecIndex  = audioDeviceIndex = -1;
            _audioDeviceName = "none";
            noAudio          = true;
        }

        _unityAudioSampleRate   = -1;
        _unityAudioChannelCount = -1;
        if (!noAudio && _audioDeviceIndex < 0 && _audioCapture != null)
        {
            if (!_audioCapture.enabled)
            {
                _audioCapture.enabled = true;
            }
            _unityAudioSampleRate   = AudioSettings.outputSampleRate;
            _unityAudioChannelCount = _audioCapture.NumChannels;
        }

        string info = string.Format("{0}x{1} @ {2}fps [{3}]", _targetWidth, _targetHeight, ((int)_frameRate).ToString(), _pixelFormat.ToString());
        info += string.Format(" vcodec:'{0}'", _codecName);
        if (!noAudio)
        {
            if (_audioDeviceIndex >= 0)
            {
                info += string.Format(" audio device:'{0}'", _audioDeviceName);
            }
            else
            {
                info += string.Format(" audio device:'Unity' {0}hz {1} channels", _unityAudioSampleRate, _unityAudioChannelCount);
            }
            info += string.Format(" acodec:'{0}'", _audioCodecName);
        }
        info += string.Format(" to file: '{0}'", _filePath);

        // If the user has overriden the vertical flip
        if (_flipVertically)
        {
            _isTopDown = !_isTopDown;
        }

        if (_outputType == OutputType.VideoFile)
        {
            // TOOD: make _frameRate floating point, or add timeLapse time system
            Debug.Log("[AVProMovieCapture] Start File Capture: " + info);
            _handle = AVProMovieCapturePlugin.CreateRecorderAVI(_filePath, (uint)_targetWidth, (uint)_targetHeight, (int)_frameRate,
                                                                (int)(_pixelFormat), _isTopDown, _codecIndex, !noAudio, _unityAudioSampleRate, _unityAudioChannelCount, audioDeviceIndex, audioCodecIndex, _isRealTime, _useMediaFoundationH264, _supportAlpha);
        }
        else if (_outputType == OutputType.NamedPipe)
        {
            Debug.Log("[AVProMovieCapture] Start Pipe Capture: " + info);
            _handle = AVProMovieCapturePlugin.CreateRecorderPipe(_filePath, (uint)_targetWidth, (uint)_targetHeight, (int)_frameRate,
                                                                 (int)(_pixelFormat), _isTopDown, _supportAlpha);
        }

        if (_handle < 0)
        {
            Debug.LogError("[AVProMovieCapture] Failed to create recorder");
            StopCapture();
        }

        return(_handle >= 0);
    }
    public virtual bool PrepareCapture()
    {
        // Delete file if it already exists
        if (File.Exists(_filePath))
        {
            File.Delete(_filePath);
        }

        // Disable vsync
        if (!Screen.fullScreen && QualitySettings.vSyncCount > 0)
        {
            _oldVSyncCount = QualitySettings.vSyncCount;
            //Debug.LogWarning("For best results vsync should be disabled during video capture.  Disabling vsync.");
            QualitySettings.vSyncCount = 0;
        }

        if (_isRealTime)
        {
            Application.targetFrameRate = (int)_frameRate;
        }
        else
        {
            Time.captureFramerate = (int)_frameRate;
        }

        int  audioDeviceIndex = _audioDeviceIndex;
        int  audioCodecIndex  = _audioCodecIndex;
        bool noAudio          = _noAudio;

        if (_noAudio || (_audioCapture == null && _audioDeviceIndex < 0))
        {
            audioCodecIndex  = audioDeviceIndex = -1;
            _audioDeviceName = "none";
            noAudio          = true;
        }

        _unityAudioSampleRate   = -1;
        _unityAudioChannelCount = -1;
        if (!noAudio && _audioDeviceIndex < 0 && _audioCapture != null)
        {
            if (!_audioCapture.enabled)
            {
                _audioCapture.enabled = true;
            }
            _unityAudioSampleRate   = AudioSettings.outputSampleRate;
            _unityAudioChannelCount = _audioCapture.NumChannels;
        }

        string info = string.Format("{0}x{1} @ {2}fps [{3}]", _targetWidth, _targetHeight, ((int)_frameRate).ToString(), _pixelFormat.ToString());

        info += string.Format(" vcodec:'{0}'", _codecName);
        if (!noAudio)
        {
            if (_audioDeviceIndex >= 0)
            {
                info += string.Format(" audio device:'{0}'", _audioDeviceName);
            }
            else
            {
                info += string.Format(" audio device:'Unity' {0}hz {1} channels", _unityAudioSampleRate, _unityAudioChannelCount);
            }
            info += string.Format(" acodec:'{0}'", _audioCodecName);
        }
        info += string.Format(" to file: '{0}'", _filePath);
        Debug.Log("[AVProMovieCapture] Start Capture: " + info);
        _handle = AVProMovieCapturePlugin.CreateRecorderAVI(_filePath, (uint)_targetWidth, (uint)_targetHeight, (int)_frameRate,
                                                            (int)(_pixelFormat), _isTopDown, _codecIndex, !noAudio, _unityAudioSampleRate, _unityAudioChannelCount, audioDeviceIndex, audioCodecIndex, _isRealTime);

        if (_handle < 0)
        {
            Debug.LogWarning("[AVProMovieCapture] Failed to create recorder");
            StopCapture();
        }

        return(_handle >= 0);
    }