Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Video Create Thumbnail From Video

Discussion in 'Audio & Video' started by dcamod, Oct 31, 2019.

  1. dcamod

    dcamod

    Joined:
    Oct 29, 2019
    Posts:
    19
    Okay, I found this code written on another thread here but the thread is very old and I did not want to res. it for my questions.
    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.         }
    57.  


    I have found that this is using the Video Player Helper from this link: https://github.com/JannikLassahn/unity-videoplayer-helper.

    The problem is that the code does not work because it is missing a few things now. The method LoadVideo is not in there at all and neither is the variable video. The ResampleandCrop function is a separate function that is not there also. Not sure if some of this is due to an update in the Video Player Helper VideoController.cs Code or not but I would really like help understanding how to get this working. I am new to coding with C# and have only limited programming experience.
     
  2. dcamod

    dcamod

    Joined:
    Oct 29, 2019
    Posts:
    19
    Okay, it has been a while and no insight on this yet. Anyone have anything to offer? It would be greatly appreciated.