Created
March 22, 2016 10:02
-
-
Save wuyongrui/bcdad2ed92797964953f to your computer and use it in GitHub Desktop.
renderSepiaEffectIntoAsset
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
// https://osxentwicklerforum.de/index.php/Thread/24626-AVFoundation-CALayer/ | |
-(void)renderSepiaEffectIntoAsset:(AVAsset*)asset withOnCompletion:(void(^)(NSURL* fileURL))aBlock{ | |
CALayer* sepiaLayer = [CALayer layer]; | |
sepiaLayer.frame = CGRectMake(20, 20, 320,480); | |
CIFilter *myFilter = [CIFilter filterWithName:@"CISepiaTone"]; | |
[myFilter setDefaults]; | |
[myFilter setValue:[NSNumber numberWithFloat:0.8f] forKey:@"inputIntensity"]; | |
[sepiaLayer setFilters:@[myFilter]]; | |
[sepiaLayer setBackgroundColor:[[UIColor blackColor]CGColor]]; | |
//[self.view.layer setFilters:@[myFilter]]; | |
AVMutableComposition *videoComposition = [AVMutableComposition composition]; | |
NSError *error; | |
AVMutableCompositionTrack *compositionVideoTrack = [videoComposition addMutableTrackWithMediaType:AVMediaTypeVideo preferredTrackID:kCMPersistentTrackID_Invalid]; | |
AVMutableCompositionTrack *compositionAudioTrack = [videoComposition addMutableTrackWithMediaType:AVMediaTypeAudio preferredTrackID:kCMPersistentTrackID_Invalid]; | |
AVAssetTrack *clipVideoTrack = [[asset tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0]; | |
[compositionVideoTrack insertTimeRange:CMTimeRangeMake(kCMTimeZero, [asset duration]) ofTrack:clipVideoTrack atTime:kCMTimeZero error:&error]; | |
AVAssetTrack *clipAudioTrack = [[asset tracksWithMediaType:AVMediaTypeAudio] objectAtIndex:0]; | |
[compositionAudioTrack insertTimeRange:CMTimeRangeMake(kCMTimeZero, [asset duration]) ofTrack:clipAudioTrack atTime:kCMTimeZero error:&error]; | |
AVMutableVideoComposition* videoComp = [AVMutableVideoComposition videoComposition] ; | |
videoComp.renderSize = CGSizeMake(480, 320); | |
videoComp.frameDuration = CMTimeMake(1, 30); | |
//videoComp.animationTool = [AVVideoCompositionCoreAnimationTool videoCompositionCoreAnimationToolWithPostProcessingAsVideoLayer:aLayer inLayer:parentLayer]; | |
videoComp.animationTool = [AVVideoCompositionCoreAnimationTool videoCompositionCoreAnimationToolWithAdditionalLayer:sepiaLayer asTrackID:2]; | |
/// instruction | |
AVMutableVideoCompositionInstruction *instruction = [AVMutableVideoCompositionInstruction videoCompositionInstruction]; | |
instruction.timeRange = CMTimeRangeMake(kCMTimeZero, CMTimeMakeWithSeconds(60, 30) ); | |
AVMutableVideoCompositionLayerInstruction* layerInstruction = [AVMutableVideoCompositionLayerInstruction videoCompositionLayerInstructionWithAssetTrack:clipVideoTrack]; | |
[layerInstruction setTrackID:2]; | |
[layerInstruction setOpacity:1.0 atTime:kCMTimeZero ]; | |
instruction.layerInstructions = [NSArray arrayWithObject:layerInstruction]; | |
videoComp.instructions = [NSArray arrayWithObject: instruction]; | |
NSString *exportPath = [NSString stringWithFormat:@"%@%@", NSTemporaryDirectory(), [NSString stringWithFormat:@"%f_tmp.mp4",[NSDate timeIntervalSinceReferenceDate]]]; | |
NSURL *exportUrl = [NSURL fileURLWithPath:exportPath]; | |
if([[NSFileManager defaultManager] fileExistsAtPath:exportPath]) | |
{ | |
[[NSFileManager defaultManager] removeItemAtPath:exportPath error:nil]; | |
} | |
AVAssetExportSession* _assetExport = [[AVAssetExportSession alloc] initWithAsset:cmp | |
presetName:AVAssetExportPresetPassthrough]; | |
_assetExport.outputFileType = AVFileTypeQuickTimeMovie; | |
_assetExport.outputURL = exportUrl; | |
NSLog(@"file type %@",_assetExport.outputURL); | |
_assetExport.videoComposition=animComp; | |
[_assetExport exportAsynchronouslyWithCompletionHandler:^{ | |
switch (_assetExport.status) | |
{ | |
case AVAssetExportSessionStatusFailed: | |
{ | |
NSLog (@"FAIL: %@",_assetExport.error); | |
break; | |
} | |
case AVAssetExportSessionStatusCompleted: | |
{ | |
NSLog (@"SUCCESS"); | |
UISaveVideoAtPathToSavedPhotosAlbum(exportPath, nil, nil, nil); | |
aBlock(exportUrl); | |
break; | |
} | |
}; | |
}]; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
cmp = videoComposition
animComp = videoComp
right ?