I am trying to implement the cropping function of an Instagram video. To do this, I need to get the correct orientation of the video orientation , with which I can display the MPMoviePlayerController in the correct aspect ratio, and then the user will be able to pan the video, and the square video shown will be cropped to the action.
I have a problem with the correct orientation of the video. I am using the following code
AVURLAsset *asset = [AVURLAsset URLAssetWithURL:fileUrl options:nil]; NSArray *tracks = [asset tracksWithMediaType:AVMediaTypeVideo]; AVAssetTrack *track = [tracks objectAtIndex:0]; UIInterfaceOrientation orientation =[self orientationForTrack:track]; // method to get orientation - (UIInterfaceOrientation)orientationForTrack:(AVAssetTrack *)videoTrack { CGSize size = [videoTrack naturalSize]; CGAffineTransform txf = [videoTrack preferredTransform]; if (size.width == txf.tx && size.height == txf.ty) return UIInterfaceOrientationLandscapeRight; else if (txf.tx == 0 && txf.ty == 0) return UIInterfaceOrientationLandscapeLeft; else if (txf.tx == 0 && txf.ty == size.width) return UIInterfaceOrientationPortraitUpsideDown; else return UIInterfaceOrientationUnknown; }
The problem I am facing is that on several occasions the videos that are actually in the portrait above the piece of code classify it as landscape and vice versa. I need to know only two states of the video orientation (Landscape or Portrait) . I want the exact same orientation value that MPMoviePlayerController interprets. My application only supports Portrait . Any help would be greatly appreciated.
PS: The code works fine for the video that is taken from the iphone camera, the videos that are downloaded or transmitted via whatsapp cause a problem.
ios objective-c video mpmovieplayercontroller uiinterfaceorientation
Usama
source share