Possible Duplicate:
How do I use AVFoundation to crop a video
The end goal is to crop my 480x640 .mov video to a 4:3 ratioed landscape video of exact resolution 480x360.
I'm aware of the near exact question posed at this link: How do I use AVFoundation to crop a video
I believe it's easier to show a preview layer that has already been cropped, and then crop the final (uncropped portrait 480x640) video at the very end. I've been playing around with the below code. At the moment, I'm just trying to get it to transform anything - there is extraneous code in there that I'll prune later on. The below code tries to scale the video to 70%:
-(void) convertVideoToSize{
AVURLAsset *videoToConvert = [AVURLAsset URLAssetWithURL:[NSURL fileURLWithPath:tempURL] options:nil];
AVMutableComposition *mixComposition = [[AVMutableComposition alloc] init];
AVMutableCompositionTrack *firstTrack = [mixComposition addMutableTrackWithMediaType:AVMediaTypeVideo preferredTrackID:kCMPersistentTrackID_Invalid];
[firstTrack insertTimeRange:CMTimeRangeMake(kCMTimeZero, videoToConvert.duration) ofTrack:[[videoToConvert tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0] atTime:kCMTimeZero error:nil];
AVMutableVideoCompositionInstruction *mainInstruction = [AVMutableVideoCompositionInstruction videoCompositionInstruction];
mainInstruction.timeRange = CMTimeRangeMake(kCMTimeZero,videoToConvert.duration);
AVMutableVideoCompositionLayerInstruction *firstLayerInstruction = [AVMutableVideoCompositionLayerInstruction videoCompositionLayerInstructionWithAssetTrack:firstTrack];
//WE PERFORM VIDEO EDITING OPERATIONS HERE
CGAffineTransform translateToCenter = CGAffineTransformMakeTranslation( 0,-420);
CGAffineTransform Rotate = CGAffineTransformMakeRotation(M_PI_2);
CGAffineTransform Scale = CGAffineTransformMakeScale(1.0f,1.0f);
CGAffineTransform shrinkWidth = CGAffineTransformMakeScale(1, 1);
CGAffineTransform finalTransform = CGAffineTransformConcat(shrinkWidth, CGAffineTransformConcat(translateToCenter, Rotate) );
[firstLayerInstruction setTransform:finalTransform atTime:kCMTimeZero];
//[firstLayerInstruction setTransform:CGAffineTransformConcat(Rotate,Scale) atTime:kCMTimeZero];
//[firstLayerInstruction setTransform:Scale atTime:kCMTimeZero];
mainInstruction.layerInstructions = [NSArray arrayWithObject:firstLayerInstruction];
AVMutableVideoComposition *mainCompositionInst = [AVMutableVideoComposition videoComposition];
mainCompositionInst.instructions = [NSArray arrayWithObject:mainInstruction];
mainCompositionInst.frameDuration = CMTimeMake(1,30);
mainCompositionInst.renderSize = CGSizeMake(480, 360);
//AVPlayerItem *newPlayerItem = [AVPlayerItem playerItemWithAsset:mixComposition];
//newPlayerItem.videoComposition = mainCompositionInst;
AVAssetExportSession *exportSession = [[AVAssetExportSession alloc] initWithAsset:mixComposition presetName:AVAssetExportPresetMediumQuality];
exportSession.outputURL = [NSURL fileURLWithPath:tempURL3];
exportSession.videoComposition = mainCompositionInst;
exportSession.outputFileType = AVFileTypeQuickTimeMovie;
[exportSession exportAsynchronouslyWithCompletionHandler:^
{
switch (exportSession.status)
{
case AVAssetExportSessionStatusFailed:
{
NSLog (@"FAIL");
break;
}
case AVAssetExportSessionStatusCompleted:
{
NSLog (@"SUCCESS");
break;
}
case AVAssetExportSessionStatusCancelled:
{
NSLog (@"CANCELED");
break;
}
};
}];
}
It doesn't work, and outputs a .mov file that is unreadable. I was wondering if anyone could point me in an easier direction, or run me through the best way to go about changing/scaling a video.
UPDATE: Upon saving the video to a file transformed.mov, I try to play it and I receive a 'movie's file format is not recognized' error in QuickTime. Possible source of corruption anyone?
UPDATE 2: I've successfully set up the export session and compositions to write transformations. Now I just need the specific transformation that will allow me to convert my 480x640 into a 480x360. Any help there?