Last active
August 6, 2024 09:29
-
-
Save nguyen-tam/1eafe29f6b67c0e1b6fb23deda65f932 to your computer and use it in GitHub Desktop.
Objective C - Rotate a video file with AVMutableVideoCompositionLayerInstruction
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-(void)changeOrientation:(NSURL *) videoURL isClockWise:(BOOL)isClockWise fileName:(NSString *)fileName{ | |
AVURLAsset* videoAsset = [[AVURLAsset alloc]initWithURL:videoURL options:nil]; | |
AVMutableComposition* mixComposition = [[AVMutableComposition alloc] init]; | |
AVMutableCompositionTrack *videoCompositionTrack = [mixComposition addMutableTrackWithMediaType:AVMediaTypeVideo preferredTrackID:kCMPersistentTrackID_Invalid]; | |
[videoCompositionTrack insertTimeRange:CMTimeRangeMake(kCMTimeZero, videoAsset.duration) ofTrack:[[videoAsset tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0] atTime:kCMTimeZero error:nil]; | |
AVMutableVideoCompositionInstruction *videoCompositionInstruction = [AVMutableVideoCompositionInstruction videoCompositionInstruction]; | |
videoCompositionInstruction.timeRange = CMTimeRangeMake(kCMTimeZero, videoAsset.duration); | |
AVMutableVideoCompositionLayerInstruction *videoCompositionLayerInstruction = [AVMutableVideoCompositionLayerInstruction videoCompositionLayerInstructionWithAssetTrack:videoCompositionTrack]; | |
AVAssetTrack *firstVideoTrack = [[videoAsset tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0]; | |
[videoCompositionLayerInstruction setOpacity:0.0 atTime:videoAsset.duration]; | |
videoCompositionInstruction.layerInstructions = [NSArray arrayWithObjects:videoCompositionLayerInstruction,nil]; | |
AVMutableVideoComposition *exportVideoComposition = [AVMutableVideoComposition videoComposition]; | |
exportVideoComposition.instructions = [NSArray arrayWithObject:videoCompositionInstruction]; | |
exportVideoComposition.frameDuration = CMTimeMake(1, 30); | |
CGSize naturalSizeFirst; | |
CGAffineTransform mixedTransform; | |
if (isClockWise) { | |
CGAffineTransform rotation = CGAffineTransformMakeRotation(degreeToRadian(90.0)); | |
CGAffineTransform translateToCenter = CGAffineTransformMakeTranslation(firstVideoTrack.naturalSize.height, 0); | |
mixedTransform = CGAffineTransformConcat(rotation, translateToCenter); | |
} else { | |
CGAffineTransform rotation = CGAffineTransformMakeRotation(degreeToRadian(-90.0)); | |
CGAffineTransform translateToCenter = CGAffineTransformMakeTranslation(0,firstVideoTrack.naturalSize.width); | |
mixedTransform = CGAffineTransformConcat(rotation, translateToCenter); | |
} | |
[videoCompositionLayerInstruction setTransform:mixedTransform atTime:kCMTimeZero]; | |
naturalSizeFirst = CGSizeMake(firstVideoTrack.naturalSize.height, firstVideoTrack.naturalSize.width); | |
exportVideoComposition.renderSize = naturalSizeFirst; | |
NSString *tmpDirectory = NSTemporaryDirectory(); | |
NSString *tmpFile = [tmpDirectory stringByAppendingPathComponent:fileName]; | |
NSURL *url = [NSURL fileURLWithPath:tmpFile]; | |
AVAssetExportSession *exporter = [[AVAssetExportSession alloc] initWithAsset:mixComposition presetName:AVAssetExportPresetMediumQuality]; | |
if ([[NSFileManager defaultManager] fileExistsAtPath:tmpFile]) | |
{ | |
[[NSFileManager defaultManager] removeItemAtPath:tmpFile error:nil]; | |
} | |
exporter.outputURL=url; | |
exporter.outputFileType = AVFileTypeQuickTimeMovie; | |
exporter.videoComposition = exportVideoComposition; | |
exporter.shouldOptimizeForNetworkUse = YES; | |
// Save your video in Temporary directory and save path for uploading to server. | |
[exporter exportAsynchronouslyWithCompletionHandler:^{ | |
if (AVAssetExportSessionStatusCompleted == exporter.status) { | |
NSLog(@"AVAssetExportSessionStatusCompleted"); | |
NSLog(@"new video saved"); | |
} else if (AVAssetExportSessionStatusFailed == exporter.status) { | |
NSLog(@"AVAssetExportSessionStatusFailed %@",exporter.error); | |
} else { | |
NSLog(@"Export Session Status: %d", exporter.status); | |
} | |
}]; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
code hay lam, toi da su dung thanh cong code cua ban