2

은 내가 편집하고 변경 가능한 조성물에 추가 한 4 개 비디오 파일이 있습니다. 나는 trackID = 1 수출됩니다와여러 트랙을 내보내기 : AVMutableComposition 및 AVAssetExportSession

<AVAssetExportSession: 0x60800001da50, asset = <AVMutableComposition: 0x6080002240a0 tracks = (
    "<AVMutableCompositionTrack: 0x600000224ca0 trackID = 1, mediaType = vide, editCount = 8>", 
    "<AVMutableCompositionTrack: 0x600000226da0 trackID = 2, mediaType = vide, editCount = 10>", 
    "<AVMutableCompositionTrack: 0x60000023e180 trackID = 3, mediaType = vide, editCount = 3>", 
    "<AVMutableCompositionTrack: 0x60000023e500 trackID = 4, mediaType = vide, editCount = 7>" 
)>, presetName = AVAssetExportPreset1280x720, outputFileType = (null) 

에게 첫 번째 트랙을 수출됩니다 아래 트랙의 목록에서 첫 번째 트랙을 수출하고 때, 그러나, 파일을 내보내려면 수출 세션을 사용하려합니다. 내보내기 세션 소스는 다음과 같습니다.

// Create path to output file 
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
    NSString *documentsDirectory = [paths objectAtIndex:0]; 
    NSString *myPathDocs = [documentsDirectory stringByAppendingPathComponent: 
          [NSString stringWithFormat:@"ProcessedVideo-%d.mov", arc4random() % 1000]]; 
    NSURL *url = [NSURL fileURLWithPath:myPathDocs]; 

    AVAssetExportSession *exporter = [[AVAssetExportSession alloc] initWithAsset:batchComposition presetName:AVAssetExportPreset1280x720]; 

    NSLog(@"%@", exporter); 

    exporter.outputURL = url; 
    exporter.outputFileType = AVFileTypeQuickTimeMovie; 

    [exporter exportAsynchronouslyWithCompletionHandler:^(void) { 
     switch (exporter.status) { 
      case AVAssetExportSessionStatusCompleted: 
       NSLog(@"Completed"); 
       break; 
      case AVAssetExportSessionStatusFailed: 
       NSLog(@"Failed:%@",exporter.error); 
       break; 
      case AVAssetExportSessionStatusCancelled: 
       NSLog(@"Canceled:%@",exporter.error); 
       break; 
      default: 
       break; 
     } 
    }]; 

내보내기 세션을 사용하여 4 개의 트랙을 하나의 .mov 파일로 내보내려면 어떻게해야합니까?

답변

1

덕분에 그의 도움을 jlw합니다. 내가 문제를 발견 한 이유는 변경 가능한 컴포지션에 여러 개의 비디오 트랙을 추가했기 때문입니다. 대신해야 할 일은 단일 비디오 트랙으로 만들어졌으며 다른 자산 트랙의 모든 편집 내용을 단일 비디오 트랙에 적용했습니다. AVAssetExportSession은 jlw에서 언급 한대로 단일 트랙 만 내 보냅니다.

요약 :

  1. 하여 작성 변경 가능한 구성
  2. 만들기 변경 가능한 구성 트랙
  3. 구성에 자산 트랙을 적용 (insertTimeRange)
  4. 수출
2

AVAssetExportSession 하나 NSURL로 내보낼 수 있습니다. 4 개의 개별 파일을 내보내려면 4 번 내보내기해야합니다.

+0

이상적으로 4 개의 트랙을 하나의 비디오 출력으로 내보내고 4 개의 개별 비디오 파일로 내보내는 것이 좋습니다. – Edwin

+1

4 개의 트랙을 하나의 컴포지션에 넣고 하나의 파일로 내보낼 수 있지만, 재생하는 데 사용하는 플레이어가 둘 이상의 비디오 트랙이있는 비디오 파일을 해석 할 수 있어야합니다. iOS의 사진 앱은 하나의 트랙 만 재생할 수 있으며 트랙간에 전환 할 수는 없습니다. – jlw

+0

Mac에서 QuickTime 플레이어를 사용하고 있습니다. 나는 그것이 최고의 트랙을 여러 트랙으로 재생할 수있을 것이라고 생각한다. – Edwin

1

내가이 생각하는 가변 구성 새로운 AVMutableComposition을 만들 필요가 없습니다. 대신 AVVideoComposition?을 수출 업체의 videoComposition 속성에 할당하면됩니다.

guard let playerItem = getPlayerItem() as? AVPlayerItem else { 
    return 
} 

let asset = playerItem.asset 
let videoComposition: AVVideoComposition? = playerItem.videoComposition 

let path = NSTemporaryDirectory().stringByAppendingFormat("/video.mov") 
if NSFileManager.defaultManager().fileExistsAtPath(path) { 
    do { 
     try NSFileManager.defaultManager().removeItemAtPath(path) 
    } catch { 
     print("Temporary file removing error.") 
    } 
} 
let outputURL = NSURL.fileURLWithPath(path) 

guard let exporter = AVAssetExportSession(asset: asset, 
              presetName: AVAssetExportPresetHighestQuality) else { 
              return 
} 

exporter.outputURL = outputURL 
exporter.outputFileType = AVFileTypeQuickTimeMovie 
exporter.shouldOptimizeForNetworkUse = true 
exporter.videoComposition = videoComposition // 

exporter.exportAsynchronouslyWithCompletionHandler { 
    PHPhotoLibrary.sharedPhotoLibrary().performChanges({ 
     PHAssetChangeRequest.creationRequestForAssetFromVideoAtFileURL(outputURL) 
    }) { (success: Bool, error: NSError?) -> Void in 
     if success { 
     } else { 
     } 
    } 
}