-
-
Save kakhaberikiknadze/458d2f26f58c5dd58651ae08cf1f9ca7 to your computer and use it in GitHub Desktop.
AVComposition & AVVideoComposition
This file contains 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
// For whatever reason (I guess because my code is wrong) the "composition" returns twice the same asset (one transformed, one not), | |
// although it should display 2 different videos... If anybody has a clue :( | |
NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithBool:YES], AVURLAssetPreferPreciseDurationAndTimingKey, nil]; | |
AVMutableComposition *composition = [AVMutableComposition composition]; | |
CMTimeRange timeRange = CMTimeRangeMake(kCMTimeZero, CMTimeMakeWithSeconds(4, 1)); | |
AVMutableVideoComposition *videoComposition = [AVMutableVideoComposition videoComposition]; | |
// Track B | |
NSURL *urlVideo2 = [NSURL URLWithString:@"file://localhost/Users/me/Movies/Temp/IMG_1388.m4v"]; | |
AVAsset *video2 = [AVURLAsset URLAssetWithURL:urlVideo2 options:options]; | |
AVMutableCompositionTrack *videoTrack2 = [composition addMutableTrackWithMediaType:AVMediaTypeVideo preferredTrackID:0]; | |
NSArray *videoAssetTracks2 = [video2 tracksWithMediaType:AVMediaTypeVideo]; | |
AVAssetTrack *videoAssetTrack2 = ([videoAssetTracks2 count] > 0 ? [videoAssetTracks2 objectAtIndex:0] : nil); | |
[videoTrack2 insertTimeRange:timeRange ofTrack:videoAssetTrack2 atTime:kCMTimeZero error:nil]; | |
AVMutableVideoCompositionLayerInstruction *to = [AVMutableVideoCompositionLayerInstruction videoCompositionLayerInstructionWithAssetTrack:videoAssetTrack2]; | |
[to setOpacity:.5 atTime:kCMTimeZero]; | |
[to setTransform:CGAffineTransformScale(videoAssetTrack2.preferredTransform, .5, .5) atTime:kCMTimeZero]; | |
// Track A | |
NSURL *urlVideo = [NSURL URLWithString:@"file://localhost/Users/me/Movies/Temp/IMG_1383.MOV"]; | |
AVURLAsset *video = [AVURLAsset URLAssetWithURL:urlVideo options:options]; | |
AVMutableCompositionTrack *videoTrack = [composition addMutableTrackWithMediaType:AVMediaTypeVideo preferredTrackID:1]; | |
NSArray *videoAssetTracks = [video tracksWithMediaType:AVMediaTypeVideo]; | |
AVAssetTrack *videoAssetTrack = ([videoAssetTracks count] > 0 ? [videoAssetTracks objectAtIndex:0] : nil); | |
[videoTrack insertTimeRange:timeRange ofTrack:videoAssetTrack atTime:kCMTimeZero error:nil]; | |
AVMutableVideoCompositionLayerInstruction *from = [AVMutableVideoCompositionLayerInstruction videoCompositionLayerInstructionWithAssetTrack:videoAssetTrack]; | |
[from setOpacity:.5 atTime:kCMTimeZero]; | |
// Video Compostion | |
AVMutableVideoCompositionInstruction *transition = [AVMutableVideoCompositionInstruction videoCompositionInstruction]; | |
transition.backgroundColor = [[UIColor clearColor] CGColor]; | |
transition.timeRange = timeRange; | |
transition.layerInstructions = [NSArray arrayWithObjects:to, from, nil]; | |
videoComposition.instructions = [NSArray arrayWithObjects:transition, nil]; | |
videoComposition.frameDuration = CMTimeMake(1, 30); | |
videoComposition.renderSize = CGSizeMake(480, 360); | |
// Export | |
NSURL *outputURL = [NSURL URLWithString:@"file://localhost/Users/me/Movies/Temp/export.MOV"]; | |
AVAssetExportSession *exportSession = [[AVAssetExportSession alloc] initWithAsset:[[composition copy] autorelease] presetName:AVAssetExportPresetHighestQuality]; | |
[exportSession setOutputFileType:@"com.apple.quicktime-movie"]; | |
exportSession.outputURL = outputURL; | |
exportSession.videoComposition = videoComposition; | |
[exportSession exportAsynchronouslyWithCompletionHandler:nil]; | |
// Player | |
AVPlayerItem *playerItem = [AVPlayerItem playerItemWithAsset:composition]; | |
playerItem.videoComposition = videoComposition; | |
AVPlayer *player = [AVPlayer playerWithPlayerItem:playerItem]; | |
AVPlayerLayer *playerLayer = [AVPlayerLayer playerLayerWithPlayer:player]; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment