1

I have this little code snippet

void Update() {
        if (Input.GetMouseButtonDown(0)) {
            if (videoPlayer.isPlaying) {
                Texture2D currentFrame = videoPlayer.texture as Texture2D;
                videoPlayer.Stop();
                string destPath = Path.Combine (Application.streamingAssetsPath, "dnn/ClickedFrame.png");
                Debug.Log("destPath :"+ destPath);
                if (currentFrame != null)
                {
                    File.WriteAllBytes(destPath, currentFrame.EncodeToPNG());
                }
                else {
                    Debug.Log("Current Frame is null");
                }
            }  
        }       
    }

I want to get current video frame texture and want to save on the disk. But each time its saying current frame is null. I guess casting has a problem. How can i convert texture to texture2d and save on the disk?

Update

I found a way to do this job

convertedTex2D = new Texture2D(videoPlayer.texture.width, videoPlayer.texture.height, TextureFormat.ARGB32, false);
            convertedTex2D.ReadPixels(new Rect(0, 0, videoPlayer.texture.width, videoPlayer.texture.height), 0, 0);
            convertedTex2D.Apply();

Its working fine and convertig texture into texture2d but sometime i am getting this error. Trying to resolve it.

ReadPixels was called to read pixels from system frame buffer, while not inside drawing frame. UnityEngine.Texture2D:ReadPixels(Rect, Int32, Int32) VideoPlayerManager:Update() (at Assets/VideoPlayerManager.cs:40)

[d3d11] attempting to ReadPixels outside of RenderTexture bounds! Reading (0, 0, 1920, 1088) from (656, 595)

Some Information about the error I checked that :

  1. texture width = 1920 and texture height 1088
  2. screen width = 803 and screen height 557

when i used scree width and height then this is working correctly but i don't want to use it.

Muhammad Faizan Khan
  • 10,013
  • 18
  • 97
  • 186
  • The issue has slove my using resize function as given by this link https://stackoverflow.com/questions/42747285/get-current-frame-texture-from-videoplayer – Muhammad Faizan Khan Dec 26 '17 at 09:35

0 Answers0