iOS旋转video的每一帧

我需要旋转一个video,因为iPhone后置摄像头正在录制,就好像它总是在左侧风景一样。 我需要物理旋转video,因为设置方向不适用于所有浏览器(例如Chrome)。 所以我设置了一些代码来读取录制的video并在旋转后保存。 我使用AVAssetExportSession这似乎工作得很好,除了两个问题: – 导出的video在开始时有一些黑色的框架 – 一些video不会被导出。

是否有可能对这个问题有所了解? 谢谢!

码:

AVAsset* asset = [AVURLAsset URLAssetWithURL: videoUrl options:nil]; AVMutableComposition *composition = [AVMutableComposition composition]; AVMutableCompositionTrack *compositionVideoTrack = [composition addMutableTrackWithMediaType:AVMediaTypeVideo preferredTrackID:kCMPersistentTrackID_Invalid]; AVMutableCompositionTrack *compositionAudioTrack = [composition addMutableTrackWithMediaType:AVMediaTypeAudio preferredTrackID:kCMPersistentTrackID_Invalid]; AVAssetTrack *videoTrack = [[asset tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0]; AVAssetTrack *audioTrack = [[asset tracksWithMediaType:AVMediaTypeAudio] objectAtIndex:0]; [compositionVideoTrack insertTimeRange:CMTimeRangeMake(kCMTimeZero, asset.duration) ofTrack:videoTrack atTime:kCMTimeZero error: &error]; [compositionVideoTrack insertTimeRange:CMTimeRangeMake(kCMTimeZero, asset.duration) ofTrack:audioTrack atTime:kCMTimeZero error: &error]; AVMutableVideoCompositionInstruction *instruction = [AVMutableVideoCompositionInstruction videoCompositionInstruction]; instruction.timeRange = CMTimeRangeMake( kCMTimeZero, asset.duration); AVMutableVideoCompositionLayerInstruction *layerInstruction = [AVMutableVideoCompositionLayerInstruction videoCompositionLayerInstructionWithAssetTrack:compositionVideoTrack]; [layerInstruction setTransform:transformToApply atTime:kCMTimeZero]; CGAffineTransform transformToApply = ///code to setup transformation [layerInstruction setTransform:transformToApply atTime:kCMTimeZero]; [layerInstruction setOpacity:0.0 atTime:asset.duration]; instruction.layerInstructions = [NSArray arrayWithObject: layerInstruction]; AVMutableVideoComposition *videoComposition = [AVMutableVideoComposition videoComposition]; videoComposition.instructions = [NSArray arrayWithObject: instruction]; videoComposition.frameDuration = CMTimeMake( 1, 600); videoComposition.renderScale = 1.0; videoComposition.renderSize = videoOutputSize; AVAssetExportSession *exportSession = [[AVAssetExportSession alloc] initWithAsset:asset presetName:AVAssetExportPresetMediumQuality] ; exportSession.outputURL = self.outputUrl; exportSession.outputFileType = AVFileTypeQuickTimeMovie; exportSession.videoComposition = videoComposition; exportSession.shouldOptimizeForNetworkUse = YES; exportSession.timeRange = CMTimeRangeMake(kCMTimeZero, asset.duration); __block BOOL finishedExporting = NO; [exportSession exportAsynchronouslyWithCompletionHandler:^{ // }];