2015-05-15 81 views
0

我想在視頻中記錄自定義視圖,所以我使用AVFoundation按照下面的代碼。AVAssetExportSession視頻不保存爲縱向

if ([library videoAtPathIsCompatibleWithSavedPhotosAlbum:outputFileURL]) 
     { 


     /** Below code works fine (save in portrait orientation) 
     [library writeVideoAtPathToSavedPhotosAlbum:outputFileURL 
            completionBlock:^(NSURL *assetURL, NSError *error) 
     { 
      if (!error) 
      { 
       self.doneButton.userInteractionEnabled = YES; 
       [videoAddr addObject:assetURL]; 
       videoURL = outputFileURL; 
      } 
     }];*/ 



     AVMutableComposition *mixComposition = [[AVMutableComposition alloc] init]; 
     AVMutableCompositionTrack *track = [mixComposition addMutableTrackWithMediaType:AVMediaTypeVideo preferredTrackID:kCMPersistentTrackID_Invalid]; 

      AVAsset *asset = [AVAsset assetWithURL:outputFileURL]; 
      [track insertTimeRange:CMTimeRangeMake(kCMTimeZero, asset.duration) ofTrack:[[asset tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0] atTime:CMTimeMake(0, 1) error:nil]; 

     NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
     NSString *documentsDirectory = [paths objectAtIndex:0]; 
     NSString *myPathDocs = [documentsDirectory stringByAppendingPathComponent: 
           [NSString stringWithFormat:@"%@%d.mov",NSBundle.mainBundle.infoDictionary[@"CFBundleExecutable"],++videoCounter]]; 
     [[NSFileManager defaultManager] removeItemAtPath:myPathDocs error:nil]; 

     NSLog(@"movie added to path:%@",myPathDocs); 
     NSURL *url = [NSURL fileURLWithPath:myPathDocs]; 
     AVAssetExportSession *exporter = [[AVAssetExportSession alloc] initWithAsset:mixComposition 
                      presetName:AVAssetExportPresetHighestQuality]; 
     exporter.outputURL=url; 
     exporter.outputFileType = AVFileTypeQuickTimeMovie; 
     exporter.shouldOptimizeForNetworkUse = YES; 
     [exporter exportAsynchronouslyWithCompletionHandler:^{ 
      dispatch_async(dispatch_get_main_queue(), ^{ 
       self.doneButton.userInteractionEnabled = YES; 
       [videoAddr addObject:exporter.outputURL]; 
       videoURL = outputFileURL; 
       flagAutorotate = NO; 
      }); 
     }]; 
    } 

但是視頻只能以橫向模式保存。 我已經指this和下面的代碼

CGAffineTransform rotationTransform = CGAffineTransformMakeRotation(M_PI_2); 
    // CGAffineTransform rotateTranslate = CGAffineTransformTranslate(rotationTransform,360,0); 
    track.preferredTransform = rotationTransform; 

但添加這個沒有得到正確的結果後加入。 所以,請幫我解決這個問題...

回答

0

你應該使用AVMutableVideoCompositionInstruction和layerInstruction

AVMutableVideoCompositionInstruction *instruction = [AVMutableVideoCompositionInstruction videoCompositionInstruction]; 
instruction.timeRange = CMTimeRangeMake(kCMTimeZero, asset.duration); 

AVMutableVideoCompositionLayerInstruction *layerInstruction = [AVMutableVideoCompositionLayerInstruction videoCompositionLayerInstructionWithAssetTrack:compositionVideoTrack]; 

CGAffineTransform rotationTransform = CGAffineTransformMakeRotation(M_PI_2); 

[layerInstruction setTransform:rotationTransform atTime:kCMTimeZero]; 
instruction.layerInstructions = @[layerInstruction]; 

AVMutableVideoComposition *videoComposition = [AVMutableVideoComposition videoComposition]; 
videoComposition.renderSize = CGSizeMake(videoWidth, videoHeight); 
videoComposition.instructions = @[instruction]; 
videoComposition.frameDuration = CMTimeMake(1, 30); 

AVAssetExportSession *exporter = [[AVAssetExportSession alloc] initWithAsset:composition presetName:AVAssetExportPresetHighestQuality]; 
exporter.outputURL = exportURL; 
exporter.outputFileType = AVFileTypeQuickTimeMovie; 
exporter.videoComposition = videoComposition; 

[exporter exportAsynchronouslyWithCompletionHandler:^{ 

}]; 
+0

如何大小了一個視頻。[查看問題](http://stackoverflow.com/questions/33231675 /擴展的視頻 - 上 - 從-A-較小sizere尺度) –

相關問題