我正在使用 AVAssetExportSession 将多个视频录制连接到一个条目中。
由于用户可以在录制期间在前置和后置摄像头之间切换,因此录制的方向会切换,这样如果录制包含一个使用前置摄像头的剪辑和一个使用后置摄像头的剪辑,则连接的视频将显示第一个剪辑使用预期的方向和倒置的第二个(反之亦然)。
我知道我需要在导出器上设置 videoComposition 指令,但我对将任何旋转应用于导出的视频并不感到高兴。
在下面的示例中,我设置了 90 度的虚拟旋转变换,只是为了尝试强制进行全面更改,但我导出的视频不受影响。. . 我猜我错过了一些明显的东西,但我看不到什么,谁能看到我的错误?
AVMutableComposition *composition = [AVMutableComposition composition];
AVAssetExportSession *assetExport = [AVAssetExportSession exportSessionWithAsset:composition presetName:AVAssetExportPresetPassthrough];
NSMutableArray *instructions = [NSMutableArray new];
AVMutableCompositionTrack *compositionVideoTrack = [composition addMutableTrackWithMediaType:AVMediaTypeVideo preferredTrackID:kCMPersistentTrackID_Invalid];
AVMutableCompositionTrack *compositionAudioTrack = [composition addMutableTrackWithMediaType:AVMediaTypeAudio preferredTrackID:kCMPersistentTrackID_Invalid];
CGAffineTransform transformToApply=CGAffineTransformMakeRotation(90.0);
for (NSURL *path in filePaths) {
NSLog(@"%@", path);
AVURLAsset *asset =[AVURLAsset assetWithURL:path];
CMTime atTime = composition.duration;
[compositionVideoTrack insertTimeRange:CMTimeRangeMake(kCMTimeZero, asset.duration)
ofTrack:[[asset tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0]
atTime:atTime error:&error];
AVMutableVideoCompositionLayerInstruction *layerInstruction = [AVMutableVideoCompositionLayerInstruction videoCompositionLayerInstructionWithAssetTrack:[[asset tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0]];
[layerInstruction setTransform:transformToApply atTime:kCMTimeZero];
AVMutableVideoCompositionInstruction * videoTrackInstruction = [AVMutableVideoCompositionInstruction videoCompositionInstruction];
videoTrackInstruction.timeRange = CMTimeRangeMake(atTime, asset.duration);
videoTrackInstruction.layerInstructions = [NSArray arrayWithObject:layerInstruction];
[instructions addObject:videoTrackInstruction];
[compositionAudioTrack insertTimeRange:CMTimeRangeMake(kCMTimeZero, asset.duration)
ofTrack:[[asset tracksWithMediaType:AVMediaTypeAudio] objectAtIndex:0]
atTime:atTime error:&error];
}
AVMutableVideoComposition *videoComposition = [AVMutableVideoComposition videoComposition];
videoComposition.instructions = instructions;
videoComposition.frameDuration = CMTimeMake(1, compositionVideoTrack.naturalTimeScale);
videoComposition.renderSize = compositionVideoTrack.naturalSize;
assetExport.videoComposition = videoComposition;