Skip to content

Instantly share code, notes, and snippets.

@w-i-n-s
Created April 17, 2017 15:45
Show Gist options
  • Save w-i-n-s/f7fff7b830a99e2d706601e63ff529ce to your computer and use it in GitHub Desktop.
Save w-i-n-s/f7fff7b830a99e2d706601e63ff529ce to your computer and use it in GitHub Desktop.
- (void)fixVideoUrl:(NSURL *)url completion:(void (^)(NSURL *outputUrl))completion {
// output file
NSString* docFolder = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
NSString* outputPath = [docFolder stringByAppendingPathComponent:@"output2.mov"];
if ([[NSFileManager defaultManager] fileExistsAtPath:outputPath]) {
[[NSFileManager defaultManager] removeItemAtPath:outputPath error:nil];
}
// input file
AVAsset* asset = [AVAsset assetWithURL:url];
AVMutableComposition *composition = [AVMutableComposition composition];
[composition addMutableTrackWithMediaType:AVMediaTypeVideo preferredTrackID:kCMPersistentTrackID_Invalid];
// input clip
AVAssetTrack *clipVideoTrack = [[asset tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0];
// make it square
AVMutableVideoComposition* videoComposition = [AVMutableVideoComposition videoComposition];
//675x379
//(clipVideoTrack.naturalSize.height, clipVideoTrack.naturalSize.height);
CGSize size = CGSizeMake(clipVideoTrack.naturalSize.height, clipVideoTrack.naturalSize.height);
videoComposition.renderSize = size;
videoComposition.frameDuration = CMTimeMake(1, 30);
AVMutableVideoCompositionInstruction *instruction = [AVMutableVideoCompositionInstruction videoCompositionInstruction];
instruction.timeRange = CMTimeRangeMake(kCMTimeZero, CMTimeMakeWithSeconds(60, 30) );
// rotate to portrait
AVMutableVideoCompositionLayerInstruction* transformer = [AVMutableVideoCompositionLayerInstruction videoCompositionLayerInstructionWithAssetTrack:clipVideoTrack];
CGAffineTransform t1 = CGAffineTransformMakeTranslation(clipVideoTrack.naturalSize.height, - (clipVideoTrack.naturalSize.width - clipVideoTrack.naturalSize.height)/2 );
//CGAffineTransform t1 = CGAffineTransformMakeTranslation((clipVideoTrack.naturalSize.width - clipVideoTrack.naturalSize.height) /2,-clipVideoTrack.naturalSize.height );
CGAffineTransform t2 = CGAffineTransformRotate(t1, M_PI_2);
CGAffineTransform finalTransform = t2;
[transformer setTransform:finalTransform atTime:kCMTimeZero];
instruction.layerInstructions = [NSArray arrayWithObject:transformer];
videoComposition.instructions = [NSArray arrayWithObject: instruction];
// export
__block NSURL *exportUrl = [NSURL fileURLWithPath:outputPath];
AVAssetExportSession *exporter = [[AVAssetExportSession alloc] initWithAsset:asset presetName:AVAssetExportPresetHighestQuality] ;
exporter.videoComposition = videoComposition;
exporter.outputURL = exportUrl;
exporter.outputFileType = AVFileTypeQuickTimeMovie;
[exporter exportAsynchronouslyWithCompletionHandler:^(void) {
if (exporter.status == AVAssetExportSessionStatusCompleted && completion) {
completion(exportUrl);
}
}];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment