I am pulling my hair out over this.
I try to concatenate videos and it just won't do what I want it to do. Specifically, I have videos that have a different orientation and I try to set it right with a Layer instruction. Alas, not matter what I try, it has no effect...
I read every tutorial, tried to implement the APLCompositionDebugView provided by Apple (which basically looks ok), to no avail... I'm ready to throw everything overboard...
Here's my code:
self.videoComposition = [AVMutableVideoComposition videoComposition];
self.videoComposition.renderSize = CGSizeMake(480, 320);
self.videoComposition.frameDuration = CMTimeMake(1, 30);
NSMutableArray *videoCompositionInstructions = [[NSMutableArray alloc] init];
AVMutableComposition *theMutableComposition = [AVMutableComposition composition];
AVMutableCompositionTrack *compositionVideoTrack = [theMutableComposition addMutableTrackWithMediaType:AVMediaTypeVideo preferredTrackID:kCMPersistentTrackID_Invalid];
AVMutableCompositionTrack *compositionAudioTrack = [theMutableComposition addMutableTrackWithMediaType:AVMediaTypeAudio preferredTrackID:kCMPersistentTrackID_Invalid];
AVMutableVideoCompositionLayerInstruction *passThroughLayer = [AVMutableVideoCompositionLayerInstruction videoCompositionLayerInstructionWithAssetTrack:compositionVideoTrack];
CMTime titleDuration = CMTimeMakeWithSeconds(5, 600);
CMTimeRange titleRange = CMTimeRangeMake(kCMTimeZero, titleDuration);
[compositionVideoTrack insertEmptyTimeRange:titleRange];
[compositionAudioTrack insertEmptyTimeRange:titleRange];
CMTime insertPoint = [[transitionTimes lastObject] CMTimeValue];
CMTime totalTime = CMTimeMakeWithSeconds(5, 600);
for(NSDictionary *clip in collection[@"clips"]){
NSString *movieName = [NSString stringWithFormat:@"collection_%li/recording_%li_%li.MOV", (long)editCollection, editCollection, [clip[@"clipID"] longValue]];
NSURL *assetUrl = [NSURL fileURLWithPath:[usefulStuff pathForFile: movieName]];
AVURLAsset *videoAsset = [AVURLAsset URLAssetWithURL:assetUrl options:nil];
totalTime = CMTimeAdd(totalTime, videoAsset.duration);
AVAssetTrack *clipVideoTrack = [[videoAsset tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0];
AVAssetTrack *clipAudioTrack = [[videoAsset tracksWithMediaType:AVMediaTypeAudio] objectAtIndex:0];
NSError *error;
[compositionVideoTrack insertTimeRange:clipVideoTrack.timeRange ofTrack:clipVideoTrack atTime:insertPoint error:&error]; // Add video
[compositionAudioTrack insertTimeRange:clipVideoTrack.timeRange ofTrack:clipAudioTrack atTime:insertPoint error:&error]; // Add audio
[passThroughLayer setTransform:clipVideoTrack.preferredTransform atTime:insertPoint]; ////// This should supposedly set the video in the right orientation at the given time...
insertPoint = CMTimeAdd(insertPoint, videoAsset.duration);
}
AVMutableVideoCompositionInstruction *passThroughInstruction = [AVMutableVideoCompositionInstruction videoCompositionInstruction];
passThroughInstruction.timeRange = CMTimeRangeMake(kCMTimeZero, totalTime);
NSLog(@"Total time b %f", CMTimeGetSeconds(totalTime));
passThroughInstruction.layerInstructions = [NSArray arrayWithObject:passThroughLayer];
[videoCompositionInstructions addObject:passThroughInstruction];
self.videoComposition.instructions = videoCompositionInstructions;
HEEEEEEELP! :)