Vanguard3000
Vanguard3000

Reputation: 223

Accessing dimensions (width/height) from a VideoPlayer source URL

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?

Upvotes: 4

Views: 7332

Answers (1)

Programmer
Programmer

Reputation: 125275

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.

Upvotes: 5

Related Questions