How to choose the right orientation for video with PhotoLibrary iOS

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.

+7
ios objective-c video mpmovieplayercontroller uiinterfaceorientation
source share
2 answers

The only way to solve this problem for me is to capture a thumbnail of the image from the video using the method:

 - (void)requestThumbnailImagesAtTimes:(NSArray *)playbackTimes timeOption:(MPMovieTimeOption)option 

and then adding a thumbnail check, for example:

 if (singleFrameImage.size.height > singleFrameImage.size.width) { // potrait video } else { // landscape video } 

This solution is tested on video taken from the iPhone camera, as well as on a set (8-10) of videos that were accidentally downloaded from the Internet.

+5
source share

AVAssetTrack has the preferredTransform property. You can use this to find the actual orientation of the video.

  AVAssetTrack *videoAssetTrack = [[videoAsset tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0]; UIImageOrientation videoAssetOrientation_ = UIImageOrientationUp; CGFloat videoAngle = RadiansToDegrees(atan2(videoAssetOrientation_.b, videoAssetOrientation_.a)); int orientation = 0; switch ((int)videoAngle) { case 0: orientation = UIImageOrientationRight; break; case 90: orientation = UIImageOrientationUp; break; case 180: orientation = UIImageOrientationLeft; break; case -90: orientation = UIImageOrientationDown; break; default: //Not found break; } 
+4
source share

All Articles