Q:

Strange cropping behaviour

When trying to re-encode a MOV to MP4 using SDAVAssetExportSession with example from readme, I get borders on left and right with rear camera device, and a square crop with front camera.
Without any re-encoding, my player (PBJVideoPlayerController, using AVLayerVideoGravityResizeAspectFill) displays correctly the fullscreen video.
I guess there is something wrong with renderSize of SDAVAssetExportSession, but I don't really understand why nobody else seems to have the problem.
Here are screenshots with front and rear camera after re-encoding :
Screenshot of video without re-encoding, rear camera (perfectly fullscreen):

Screenshot of video with re-encoding, rear camera:

Screenshot of video with re-encoding, front camera

As you can see, the player shouldn't be the problem as everything is ok without re encoding. But after exporting, borders appears with rear, and I-don't-know-really-know-what-happens with front camera...
Any help ?
Thanks !
PS : Like my coffee bowl ?

A:

I am using SDAVAssetExportSession as well and I've realized that bad video cropping will occur if you give a portrait video more width than height and deviate from the aspect ratio that it needs. The same will occur for a landscape video if you give it more height than width.
My solution is to get the videos aspect ratio and current dimensions first before transcoding and then calculate a smaller width and height based off its current dimensions in order to maintain the aspect ratio. This will keep the resulting video from having black borders.
Update: Here is the relevant code - 
This shows how to resize and keep the aspect ratio and avoid cropping issues.  Note that the width and height are integer variables controlled by the user.  If they aren't provided I transcode with the original video width and height.
AVURLAsset *avAsset = [AVURLAsset URLAssetWithURL:[NSURL fileURLWithPath:assetPath] options:nil];

NSString *cacheDir = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *outputPath = [NSString stringWithFormat:@"%@/%@%@", cacheDir, videoFileName, outputExtension];
NSURL *outputURL = [NSURL fileURLWithPath:outputPath];

NSArray *tracks = [avAsset tracksWithMediaType:AVMediaTypeVideo];
AVAssetTrack *track = [tracks objectAtIndex:0];
CGSize mediaSize = track.naturalSize;

float videoWidth = mediaSize.width;
float videoHeight = mediaSize.height;
float aspectRatio = videoWidth / videoHeight;
int newWidth = (width && height) ? height * aspectRatio : videoWidth;
int newHeight = (width && height) ? newWidth / aspectRatio : videoHeight;

NSLog(@"input videoWidth: %f", videoWidth);
NSLog(@"input videoHeight: %f", videoHeight);
NSLog(@"output newWidth: %d", newWidth);
NSLog(@"output newHeight: %d", newHeight);

SDAVAssetExportSession *encoder = [SDAVAssetExportSession.alloc initWithAsset:avAsset];
encoder.outputFileType = stringOutputFileType;
encoder.outputURL = outputURL;
encoder.videoSettings = @
{
    AVVideoCodecKey: AVVideoCodecH264,
    AVVideoWidthKey: [NSNumber numberWithInt: newWidth],
    AVVideoHeightKey: [NSNumber numberWithInt: newHeight],
    AVVideoCompressionPropertiesKey: @
    {
        AVVideoAverageBitRateKey: [NSNumber numberWithInt: videoBitRate],
        AVVideoProfileLevelKey: AVVideoProfileLevelH264High40
    },
};

