Search Unity

  1. We would like to hear your feedback about Unity and our products. Click here for more information.
    Dismiss Notice

Video Video Player preview

Discussion in 'Audio & Video' started by SrTartarus, Apr 16, 2018.

  1. SrTartarus

    SrTartarus

    Joined:
    Feb 27, 2017
    Posts:
    9
    Hello there, i´m working in a project gallery with a images previews, the last step is make a video previews, i haven´t idea how take the first frame in the video player to get the video preview, i need a help to do that, so, anyones know?, how you can preview the video in the video player component?. Thanks.
     
  2. Bscsproperties

    Bscsproperties

    Joined:
    Nov 15, 2017
    Posts:
    28
    My method is this:

    Code (CSharp):
    1. //Set time of the VideoPlayer to 0
    2.             videoPlayer.time = 0;
    3.             //Plays the video for one frame
    4.             videoPlayer.Play();
    5.             //Sets the frame to display on the RawImage
    6.             image.texture = videoPlayer.texture;
    7.             //Pauses the video after one frame so that the first frame
    8.             //of the video is displayed during idle
    9.             videoPlayer.Pause();
    It basically plays the video for a frame, grabs that frame and sets the rawImage texture to that frame.
     
    efge and whoa_apps like this.
  3. SrTartarus

    SrTartarus

    Joined:
    Feb 27, 2017
    Posts:
    9
    Thanks you Bscsproperties, this helped me so much, i´m working now to create my own gallery and you can take images and videos as well from any devices explorer, so, thanks again.
     
    Bscsproperties likes this.
  4. Bscsproperties

    Bscsproperties

    Joined:
    Nov 15, 2017
    Posts:
    28
    Not a problem, the only issue with it is, that method requires you to have the video loaded into memory, Freeing the video from memory will make the "thumbnail" go away. So if your gallery is going to have a lot of videos then it might cause some lag.
     
  5. SrTartarus

    SrTartarus

    Joined:
    Feb 27, 2017
    Posts:
    9
    Yep, i know,i´m considering this point so, my solution is this:

    Code (CSharp):
    1. videoPlayer.time = 0;
    2. videoPlayer.Play();
    3. int width = videoPlayer.texture.width;
    4. int height = videoPlayer.texture.height;
    5. Texture2D preview = new Texture2D(width, height, TextureFormat.RGB24, false);
    6. RenderTexture.active = videoPlayer.targetTexture;
    7. preview.ReadPixels(new Rect(0, 0, width, height), 0, 0);
    8. preview.Apply();
    9. tex.sprite = Sprite.Create(preview, new Rect(0, 0, width, height), new Vector2(0.5f, 0.5f));
    10. videoPlayer.Pause();
    11. RenderTexture.active = null;
    I´m working only with one Video Player in RenderMode with RenderTexture, i create Texture2D to ReadPixels from the VideoPlayer, with Texture2D i can handle to maybe resample pixels or something and also i can delete the VideoPlayer because i have the pixels in my Texture2D. Thanks for you warning.
     
  6. Bscsproperties

    Bscsproperties

    Joined:
    Nov 15, 2017
    Posts:
    28
    Hmm that is very interesting. I hadn't thought of doing that.

    It seems that even if you assign the VideoPlayer Texture to a seperate texture, then clear the VideoPlayer, the seperate texture gets cleared also.

    Does your method fix that problem?
     
  7. SrTartarus

    SrTartarus

    Joined:
    Feb 27, 2017
    Posts:
    9
    This is my full code:


    Code (CSharp):
    1. private IEnumerator GetFiles(string path)
    2.     {
    3.         DirectoryInfo dir = new DirectoryInfo(path);
    4.         string[] extensions = new string[] { ".jpeg", ".jpg", ".png", ".avi", ".mp4", ".mov" };
    5.         FileInfo[] files = dir.GetFiles("*.*", SearchOption.AllDirectories).Where(f => extensions.Contains(f.Extension.ToLower())).ToArray();
    6.  
    7.         int filesLenght = files.Length;
    8.         for (int i = 0; i < filesLenght; i++)
    9.         {
    10.             if(File.Exists(files[i].FullName))
    11.             {
    12.                 if(files[i].Extension == ".jpeg" || files[i].Extension == ".jpg" || files[i].Extension == ".png")
    13.                 {
    14.                     GameObject go = Instantiate(Resources.Load("preview") as GameObject, content);
    15.                     go.name = files[i].Name;
    16.                     Texture2D tex = new Texture2D(1, 1);
    17.                     byte[] bytes = File.ReadAllBytes(files[i].FullName);
    18.                     tex.LoadImage(bytes);
    19.                     tex = ResampleAndCrop(tex, 256, 256);
    20.                     go.transform.GetChild(1).GetComponent<Image>().sprite = Sprite.Create(tex, new Rect(0, 0, tex.width, tex.height), new Vector2(0.5f, 0.5f));
    21.                 }
    22.                 else if(files[i].Extension == ".avi" || files[i].Extension == ".mp4" || files[i].Extension == ".mov")
    23.                 {
    24.                     VideoController controller = GetComponent<VideoController>();
    25.                     controller.LoadVideo(files[i].FullName);
    26.  
    27.                     while(!controller.IsPrepared())
    28.                     {
    29.                         yield return null;
    30.                     }
    31.  
    32.                     int width = controller.video.texture.width;
    33.                     int height = controller.video.texture.height;
    34.                     controller.video.time = 0;
    35.                     RenderTexture renderTexture = new RenderTexture(width, height, 24, RenderTextureFormat.ARGB32);
    36.                     renderTexture.name = files[i].Name + "Render";
    37.                     controller.video.targetTexture = renderTexture;
    38.                     controller.PlayVideo();
    39.                     yield return new WaitForSeconds(1.5f);
    40.                     Texture2D texture = new Texture2D(width, height, TextureFormat.RGBA32, false);
    41.                     RenderTexture.active = controller.video.targetTexture;
    42.                     texture.ReadPixels(new Rect(0, 0, width, height), 0, 0);
    43.                     texture.Apply();
    44.                     controller.PauseVideo();
    45.                     RenderTexture.active = null;
    46.                     texture = ResampleAndCrop(texture, 256, 256);
    47.                     GameObject go = Instantiate(Resources.Load("preview") as GameObject, content);
    48.                     go.name = files[i].Name;
    49.                     go.transform.GetChild(1).GetComponent<Image>().sprite = Sprite.Create(texture, new Rect(0, 0, texture.width, texture.height), new Vector2(0.5f, 0.5f));
    50.                     controller.video.targetTexture = null;
    51.                     Destroy(renderTexture);
    52.                 }
    53.             }
    54.             yield return null;
    55.         }
    56.     }
    This works fine, i used Render Texture only to get first frames in a range of 1.5 seconds from my videos, after i created a Texture2D to gets all colors from my RenderTexture with Texture2D.ReadPixels before that play video, set Render Texture and pause the video, after you do that, you´ll need to destroy Render Texture to clean memory also you´ll need to clean you targetTexture from you Video Player.
     
    gunboldb likes this.
  8. frangagn

    frangagn

    Joined:
    Sep 20, 2018
    Posts:
    11
    I'm trying to use your method above to read a directory of files and display thumbnails. It works for some of the files, but not for others where I get a grey uniform texture. Any idea why?
     
  9. Bscsproperties

    Bscsproperties

    Joined:
    Nov 15, 2017
    Posts:
    28
    Could you make sure that all of the thumbnails' TextureTypes are Sprites?

    Then also make sure that they are of a supported file type (.jpg, .png, etc)

    The only other things I could think of are that your code is screwing up on certain cases. So make sure to debug your code to ensure that is not the issue.

    I was actually able to implement what you are trying to do so if you have any more questions don't hesitate to ask!