我正在使用AVFoundation Framework,objective-C和Xcode 7.1構建iOS應用程序。我正在做的是獲得錄製的視頻,添加文本圖層並導出。我的問題是,當我在肖像上錄製時,導出的視頻在風景中!我一直在掙扎幾個小時,無法解決它。我發現了類似的問題,但沒有任何答案對我有幫助。 這裏是我的代碼:AVMutableComposition旋轉錄制的視頻
AVURLAsset* videoAsset = [[AVURLAsset alloc]initWithURL:self.videoURL options:nil];
AVMutableComposition* mixComposition = [AVMutableComposition composition];
AVMutableCompositionTrack *compositionVideoTrack = [mixComposition addMutableTrackWithMediaType:AVMediaTypeVideo preferredTrackID:kCMPersistentTrackID_Invalid];
AVMutableCompositionTrack* compositionAudioTrack = [mixComposition addMutableTrackWithMediaType:AVMediaTypeAudio preferredTrackID:kCMPersistentTrackID_Invalid];
CMTime insertTime = kCMTimeZero;
insertTime = CMTimeAdd(insertTime, compositionVideoTrack.timeRange.duration);
AVAssetTrack *clipVideoTrack = [[videoAsset tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0];
// [compositionVideoTrack insertTimeRange:CMTimeRangeMake(kCMTimeZero, videoAsset.duration)
// ofTrack:clipVideoTrack
// atTime:kCMTimeZero error:nil];
AVAssetTrack* audioAssetTrack = [[videoAsset tracksWithMediaType:AVMediaTypeAudio] objectAtIndex:0];
[compositionVideoTrack insertTimeRange:CMTimeRangeMake(kCMTimeZero, clipVideoTrack.timeRange.duration) ofTrack:clipVideoTrack atTime:insertTime error:nil];
[compositionAudioTrack insertTimeRange:CMTimeRangeMake(kCMTimeZero, audioAssetTrack.timeRange.duration) ofTrack:audioAssetTrack atTime:insertTime error:nil];
[compositionVideoTrack setPreferredTransform:[[[videoAsset tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0] preferredTransform]];
CGSize videoSize = [clipVideoTrack naturalSize];
CALayer *parentLayer = [CALayer layer];
CALayer *videoLayer = [CALayer layer];
parentLayer.frame = CGRectMake(0, 0, videoSize.width, videoSize.height);
videoLayer.frame = CGRectMake(0, 0, videoSize.width, videoSize.height);
CATextLayer *subtitle1Text = [[CATextLayer alloc] init];
[subtitle1Text setFont:@"Helvetica"];
[subtitle1Text setFontSize:32];
[subtitle1Text setFrame:CGRectMake(0, 0, videoSize.width, 100)];
[subtitle1Text setString:self.titleField.text];
[subtitle1Text setAlignmentMode:kCAAlignmentCenter];
[subtitle1Text setForegroundColor:[[UIColor blackColor] CGColor]];
subtitle1Text.backgroundColor = [UIColor whiteColor].CGColor;
CALayer *overlayLayer = [CALayer layer];
[overlayLayer addSublayer:subtitle1Text];
overlayLayer.frame = CGRectMake(0, 0, videoSize.width, videoSize.height);
[overlayLayer setMasksToBounds:YES];
parentLayer.frame = CGRectMake(0, 0, videoSize.width, videoSize.height);
videoLayer.frame = CGRectMake(0, 0, videoSize.width, videoSize.height);
[parentLayer addSublayer:videoLayer];
[parentLayer addSublayer:overlayLayer];
float startTime = 0.5f;
float duration = 4.0f;
overlayLayer.opacity = 0.0;
CABasicAnimation *myAnimation = [CABasicAnimation animationWithKeyPath:@"opacity"];
[myAnimation setBeginTime: startTime];
[myAnimation setDuration: duration];
[myAnimation setFromValue:[NSNumber numberWithFloat:0.5]];
[myAnimation setToValue:[NSNumber numberWithFloat:1.0]];
[myAnimation setRemovedOnCompletion:NO];
[overlayLayer addAnimation:myAnimation forKey:@"myUniqueAnimationKey"];
AVMutableVideoComposition* videoComp = [AVMutableVideoComposition videoComposition];
videoComp.renderSize = videoSize;
videoComp.frameDuration = CMTimeMake(1, 30);
videoComp.animationTool = [AVVideoCompositionCoreAnimationTool videoCompositionCoreAnimationToolWithPostProcessingAsVideoLayer:videoLayer inLayer:parentLayer];
AVMutableVideoCompositionInstruction *instruction = [AVMutableVideoCompositionInstruction videoCompositionInstruction];
instruction.timeRange = CMTimeRangeMake(kCMTimeZero, [mixComposition duration]);
AVAssetTrack *videoTrack = [[mixComposition tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0];
AVMutableVideoCompositionLayerInstruction* layerInstruction = [AVMutableVideoCompositionLayerInstruction videoCompositionLayerInstructionWithAssetTrack:videoTrack];
instruction.layerInstructions = [NSArray arrayWithObject:layerInstruction];
videoComp.instructions = [NSArray arrayWithObject: instruction];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString* videoFileName = [NSString stringWithFormat:@"textOverlayVideo-%@.mov",[NSDate date]];
NSString *myPathDocs = [documentsDirectory stringByAppendingPathComponent:videoFileName];
fileURL = [NSURL fileURLWithPath:myPathDocs];
AVAssetExportSession* exporter = [[AVAssetExportSession alloc] initWithAsset:mixComposition presetName:AVAssetExportPresetHighestQuality];
exporter.outputURL = fileURL;
exporter.videoComposition = videoComp;
exporter.outputFileType = AVFileTypeQuickTimeMovie;
exporter.shouldOptimizeForNetworkUse = YES;
[exporter exportAsynchronouslyWithCompletionHandler:^{
dispatch_async(dispatch_get_main_queue(), ^{
switch (exporter.status)
{
case AVAssetExportSessionStatusFailed:
NSLog(@"AVAssetExportSessionStatusFailed");
break;
case AVAssetExportSessionStatusCompleted:
NSLog(@"AVAssetExportSessionStatusCompleted");
//[self syncFile];
imageData = [NSData dataWithContentsOfURL:fileURL];
[self send];
break;
case AVAssetExportSessionStatusWaiting:
NSLog(@"AVAssetExportSessionStatusWaiting");
break;
default:
break;
}
});
}];}
謝謝大家。
嘗試過,但還是一樣。 – Katerina