Pergunta

I have working on merge video functionality.

In my application I have one video with multiple tracks (total 10 tracks 9 video tracks and one audio track is common ). Now I want to get 3 video from this multiple Tracks. First video is combine with 1,4,7 track and Second video is combine with 2,5,8 track and Third video is combine with 3,6,9 track and and Audio track is common for this three video.

I want to do this using following code.

 AVPlayerItem* playerItem = [AVPlayerItem playerItemWithURL:[NSURL fileURLWithPath:urlSubItem.path]];
 NSLog(@"%@",playerItem);
 NSArray* arrTracks = [playerItem.asset tracksWithMediaType:AVMediaTypeVideo];

 NSLog(@"%@",arrTracks);
 NSArray* arrTracksText = [playerItem.asset tracksWithMediaType:AVMediaTypeText];
 NSArray* arrTracksAudio = [playerItem.asset tracksWithMediaType:AVMediaTypeAudio];
 NSLog(@"%@:%@",arrTracks,arrTracksAudio);
 for (int i=0; i<3; i++) {
     AVMutableComposition* mixComposition = [AVMutableComposition composition];
     AVMutableCompositionTrack *compositionCommentaryTrack = [mixComposition addMutableTrackWithMediaType:AVMediaTypeAudio                                                         preferredTrackID:kCMPersistentTrackID_Invalid];
     [compositionCommentaryTrack insertTimeRange:CMTimeRangeMake(kCMTimeZero, playerItem.asset.duration) ofTrack:[[playerItem.asset tracksWithMediaType:AVMediaTypeAudio] objectAtIndex:0] atTime:kCMTimeZero error:nil];

     CMTime currentTime = kCMTimeZero;
     for (int k=0;k<[arrTracks count];k++) {
          NSLog(@"%d",k%3);
          if(k%3==i){
              AVAssetTrack* trackCombineVideo = [arrTracks objectAtIndex:k];    
              AVMutableCompositionTrack *compositionVideoTrack = [mixComposition addMutableTrackWithMediaType:AVMediaTypeVideo                                                                              preferredTrackID:kCMPersistentTrackID_Invalid];
              NSError* errors;
              [compositionVideoTrack insertTimeRange:CMTimeRangeMake(currentTime, trackCombineVideo.timeRange.duration)  ofTrack:[arrTracks objectAtIndex:k] atTime:currentTime error:&errors];
               if (errors) {
                   NSLog(@"wait error:%@",errors);
               }
               currentTime = trackCombineVideo.timeRange.duration;
            }
        }

         AVAssetExportSession* _assetExport = [[AVAssetExportSession alloc] initWithAsset:mixComposition presetName:AVAssetExportPresetPassthrough];
        _assetExport.outputFileType = @"com.apple.quicktime-movie";
        NSLog(@"file type %@",_assetExport.outputFileType);
        _assetExport.outputURL = exportUrl // Document Directory Path;
       _assetExport.shouldOptimizeForNetworkUse = YES;

Using this code 3 separate video is create but each video have a 3 different track. Now, my question is how to create video with only one track ?

Foi útil?

Solução

 AVMutableCompositionTrack *compositionVideoTrack = [mixComposition
 addMutableTrackWithMediaType:AVMediaTypeVideo preferredTrackID:kCMPersistentTrackID_Invalid];

 for (int k=0;k<[arrTracks count];k++) {
      NSLog(@"%d",k%3);
      if(k%3==i){
          AVAssetTrack* trackCombineVideo = [arrTracks objectAtIndex:k];    

          [compositionVideoTrack insertTimeRange:CMTimeRangeMake(currentTime, trackCombineVideo.timeRange.duration)  ofTrack:[arrTracks objectAtIndex:k] atTime:currentTime error:&errors];
           if (errors) {
               NSLog(@"wait error:%@",errors);
           }
           currentTime = trackCombineVideo.timeRange.duration;
        }
    }

     AVAssetExportSession* _assetExport = [[AVAssetExportSession alloc] initWithAsset:mixComposition presetName:AVAssetExportPreset1280x720];
    _assetExport.outputFileType = @"com.apple.quicktime-movie";
    NSLog(@"file type %@",_assetExport.outputFileType);
    _assetExport.outputURL = exportUrl // Document Directory Path;
   _assetExport.shouldOptimizeForNetworkUse = YES;
   _assetExport.timeRange = CMTimeRangeMake(kCMTimeZero, playerItem.asset.duration);
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top