4

I am working on a Unity project that requires me to download a video file (mp4 format) and play it using the VideoPlayer component. Because the file is downloaded at runtime, I effectively have to "stream" it via a url to its downloaded location, as opposed to loading it as a VideoClip.

To accurately size the target RenderTexture and GameObject the video will be playing to, I need the dimensions of the video file itself. Because it is not a VideoClip I cannot use:

VideoClip clip = videoPlayer.clip;
float videoWidth = clip.width;
float videoHeight = clip.height;

Because I am using a source URL as opposed to a VideoClip, this will return null.

Can I get the video dimensions directly from the file somehow?

Vanguard3000
  • 223
  • 1
  • 6
  • 15

1 Answers1

4

You can retrieve these information from the Texture that VideoPlayer constructs.

Get VideoPlayer

VideoPlayer videoPlayer = GetComponent<VideoPlayer>();

Get the Texture VideoPlayer

Texture vidTex = videoPlayer.texture;

Get VideoPlayer dimension width/height

float videoWidth = vidTex.width;
float videoHeight = vidTex.height;

Make sure to only get the texture after videoPlayer.isPrepared is true. See my other answer for full code on how to play video make it display on RawImage component.

Programmer
  • 121,791
  • 22
  • 236
  • 328
  • I could have sworn I tried this method already. I think I just didn't get the results I wanted (dimensions were listed as 1280 x 1280, but the video was letterboxed so I wasn't expecting a square result). Thank you for your help (twice)! – Vanguard3000 Jul 25 '17 at 17:37
  • I suspect you were accessing something else and exactly the properties from the texture in the VideoPlayer. You are welcome! – Programmer Jul 25 '17 at 21:27