Get the dimensions of video programmatically in objective c

An1Ba7 picture An1Ba7 · Feb 14, 2012 · Viewed 9.1k times · Source

I am a beginner in iOS development. I am developing an application where I require the size of a video which is getting played in the MPMoviePlayerController.

I am using the property naturalSize of MPMoviePlayerController to get the dimensions of the video that is played.

@property(nonatomic, readonly) CGSize naturalSize;

But the problem is that I can get the natural size only when the Video gets played in the MPMoviePlayerController.

Is there a way in which I can get the dimensions of the video before it gets played in the MPMoviePlayerController.

-- EDIT --

Is there any other workaround I can approach to solve this problem? Please help.

Answer

mjwunderlich picture mjwunderlich · Jan 23, 2013

You can read a video's dimensions using AVURLAsset and AVAssetTrack, like this:

NSURL *mediaURL; // Your video's URL
AVURLAsset *asset = [AVURLAsset URLAssetWithURL:mediaURL options:nil];
NSArray *tracks = [asset tracksWithMediaType:AVMediaTypeVideo];
AVAssetTrack *track = [tracks objectAtIndex:0];

Then using the AVAssetTrack's naturalSize property:

CGSize mediaSize = track.naturalSize;

The benefit of getting the video's metadata this way is that you can access it immediately, without having to play the video or waiting for it to load.