当我尝试将两个视频与前置摄像头和另一个带有后置摄像头合并时,其中一个视频的方向存在问题,我使用此代码:
- (void)combineMultibleVideoSegments { NSError *错误;
AVMutableComposition *composition = [AVMutableComposition composition];
AVMutableCompositionTrack *compositionVideoTrack = [composition addMutableTrackWithMediaType:AVMediaTypeVideo preferredTrackID:kCMPersistentTrackID_Invalid];
AVMutableCompositionTrack *compositionAudioTrack = [composition addMutableTrackWithMediaType:AVMediaTypeAudio preferredTrackID:kCMPersistentTrackID_Invalid];
AVMutableVideoComposition *videoComposition = [AVMutableVideoComposition videoComposition];
videoComposition.frameDuration = CMTimeMake(1,30);
videoComposition.renderScale = 1.0;
AVMutableVideoCompositionLayerInstruction *layerInstruction = [AVMutableVideoCompositionLayerInstruction videoCompositionLayerInstructionWithAssetTrack:compositionVideoTrack];
CMTime currentTime = kCMTimeZero;
CGFloat videoTrackY = 0.0;
CGFloat videoTrackWidth=0.0;
for (int i = 0; i<videosPathsArray.count; i++)
{
NSURL *videoURL = [NSURL fileURLWithPath:[videosPathsArray objectAtIndex:i]];
AVURLAsset* sourceAsset = [[AVURLAsset alloc]initWithURL:videoURL options:nil];
NSLog(@"path = %@" , [videosPathsArray objectAtIndex:i]);
NSError *error = nil;
BOOL ok = NO;
AVAssetTrack *sourceVideoTrack = [[sourceAsset tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0];
AVAssetTrack *sourceAudioTrack = [[sourceAsset tracksWithMediaType:AVMediaTypeAudio] objectAtIndex:0];
videoTrackY = sourceVideoTrack.naturalSize.height;
videoTrackWidth = sourceVideoTrack.naturalSize.width;
if ([[VideosReocrdCamera objectAtIndex:i] isEqual:@"Yes"])
{
CGAffineTransform rotationTransform = CGAffineTransformMakeRotation(M_PI/2);
CGAffineTransform rotateTranslate = CGAffineTransformTranslate(rotationTransform,320,0);
[compositionVideoTrack setPreferredTransform:sourceVideoTrack.preferredTransform];
[layerInstruction setTransform:rotateTranslate atTime:kCMTimeZero];
}
else
{
CGAffineTransform rotationTransform = CGAffineTransformMakeRotation(0.0);
CGAffineTransform rotateTranslate = CGAffineTransformTranslate(rotationTransform,320,0);
[compositionVideoTrack setPreferredTransform:rotationTransform];
[layerInstruction setTransform:rotateTranslate atTime:kCMTimeZero];
}
[compositionAudioTrack insertTimeRange:CMTimeRangeMake(kCMTimeZero, [sourceAsset duration]) ofTrack:sourceAudioTrack atTime:currentTime error:&error];
ok = [compositionVideoTrack insertTimeRange:CMTimeRangeMake(kCMTimeZero, [sourceAsset duration]) ofTrack:sourceVideoTrack atTime:currentTime error:&error];
currentTime = CMTimeAdd(currentTime, [sourceAsset duration]);
if (!ok) {
// Deal with the error.
NSLog(@"something went wrong");
}
NSLog(@"\n source asset duration is %f \n source vid track timerange is %f %f \n composition duration is %f \n composition vid track time range is %f %f",CMTimeGetSeconds([sourceAsset duration]), CMTimeGetSeconds(sourceVideoTrack.timeRange.start),CMTimeGetSeconds(sourceVideoTrack.timeRange.duration),CMTimeGetSeconds([composition duration]), CMTimeGetSeconds(compositionVideoTrack.timeRange.start),CMTimeGetSeconds(compositionVideoTrack.timeRange.duration));
}
所以任何人都可以告诉我如何解决这个问题