0

我正在研究合并视频功能。

在我的应用程序中,我有一个带有多个轨道的视频(共有 10 个轨道、9 个视频轨道和一个音频轨道是常见的)。现在我想从这多个音轨中获取 3 个视频。第一个视频与 1,4,7 轨道结合,第二个视频与 2,5,8 轨道结合,第三个视频与 3,6,9 轨道结合,音频轨道对于这三个视频是常见的。

我想使用以下代码执行此操作。

 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;

使用此代码创建 3 个单独的视频,但每个视频有 3 个不同的轨道。现在,我的问题是如何创建只有一个轨道的视频?

4

1 回答 1

1
 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);
于 2013-12-09T12:35:56.990 回答