2013-06-25 29 views
7

Tôi muốn sử dụng UIImagePickerController để quay nhiều video clip và sau đó ghép chúng lại với nhau thành một video duy nhất, tương tự như cách ứng dụng Vine thực hiện. Bất cứ ai có thể chỉ cho tôi đi đúng hướng để làm thế nào tôi có thể kết hợp các video clip vào một video duy nhất?Ghép nối các video clip với nhau thành một video duy nhất như Vine

Trả lời

0

Dưới đây là ví dụ về cách bạn ghép các video lại với nhau, một phần dựa trên blog của Ray Wenderlich.

-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info 
{ 
    [appDelegate.playerLayer removeFromSuperlayer]; 

    if (self.firstAsset) 
    { 
    self.secondAssetURL = [info objectForKey:UIImagePickerControllerMediaURL]; 
    self.secondAsset = [AVAsset assetWithURL:self.secondAssetURL]; 
    } 
    else 
    { 
    self.firstAssetURL = [info objectForKey:UIImagePickerControllerMediaURL]; 
    self.firstAsset = [AVAsset assetWithURL:self.firstAssetURL]; 
    } 

    if (self.secondAsset) 
    { 
    CMTime recordTime = CMTimeMakeWithSeconds(self.recordTime, NSEC_PER_SEC); 
    AVMutableComposition *mixComposition = [AVMutableComposition new]; 

    AVMutableCompositionTrack *videoTrack = [mixComposition addMutableTrackWithMediaType:AVMediaTypeVideo preferredTrackID:kCMPersistentTrackID_Invalid]; 
    [videoTrack insertTimeRange:CMTimeRangeMake(kCMTimeZero, self.firstAsset.duration) ofTrack:[[self.firstAsset tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0] atTime:kCMTimeZero error:nil]; 
    [videoTrack insertTimeRange:CMTimeRangeMake(kCMTimeZero, self.secondAsset.duration) 
         ofTrack:[[self.secondAsset tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0] atTime:recordTime error:nil]; 

    AVURLAsset *firstAudioAsset = [[AVURLAsset alloc] initWithURL:self.firstAssetURL options:nil]; 
    AVURLAsset *secondAudioAsset = [[AVURLAsset alloc] initWithURL:self.secondAssetURL options:nil]; 

    AVAssetTrack *firstAudioTrack = [[firstAudioAsset tracksWithMediaType:AVMediaTypeAudio] objectAtIndex:0]; 
    AVAssetTrack *secondAudioTrack = [[secondAudioAsset tracksWithMediaType:AVMediaTypeAudio] objectAtIndex:0]; 

    AVMutableCompositionTrack *compositionAudioTrack = [mixComposition addMutableTrackWithMediaType:AVMediaTypeAudio preferredTrackID:kCMPersistentTrackID_Invalid]; 
    [compositionAudioTrack insertTimeRange:CMTimeRangeMake(kCMTimeZero, firstAudioAsset.duration) ofTrack:firstAudioTrack atTime:kCMTimeZero error:nil]; 
    [compositionAudioTrack insertTimeRange:CMTimeRangeMake(kCMTimeZero, secondAudioAsset.duration) ofTrack:secondAudioTrack atTime:recordTime error:nil]; 

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
    NSString *documentsDirectory = [paths objectAtIndex:0]; 
    NSString *myPathDocs = [documentsDirectory stringByAppendingPathComponent: 
          [NSString stringWithFormat:@"mergeVideo-%d.mov",arc4random() % 1000]]; 
    NSURL *url = [NSURL fileURLWithPath:myPathDocs]; 
    // 5 - Create exporter 
    AVAssetExportSession *exporter = [[AVAssetExportSession alloc] initWithAsset:mixComposition 
                     presetName:AVAssetExportPresetHighestQuality]; 
    exporter.outputURL=url; 
    exporter.outputFileType = AVFileTypeQuickTimeMovie; 
    exporter.shouldOptimizeForNetworkUse = YES; 
    [exporter exportAsynchronouslyWithCompletionHandler:^{ 
     NSLog(@"we are now exporting"); 
     int exportStatus = exporter.status; 
     switch (exportStatus) { 
     case AVAssetExportSessionStatusFailed: { 
      // log error to text view 
      NSError *exportError = exporter.error; 
      NSLog (@"AVAssetExportSessionStatusFailed: %@", exportError); 
      break; 
     } 
     case AVAssetExportSessionStatusCompleted: { 
      NSLog (@"AVAssetExportSessionStatusCompleted"); 
      self.firstAssetURL = exporter.outputURL; 
      self.firstAsset = [AVAsset assetWithURL:self.firstAssetURL]; 
      dispatch_async(dispatch_get_main_queue(), ^{ 
      [self layoutVideo:exporter.outputURL]; 
      }); 
      break; 
     } 
     default: { NSLog (@"didn't get export status"); break;} 
     } 

    }]; 
    } 
    else 
    { 
    [self layoutVideo:[info objectForKey:@"UIImagePickerControllerMediaURL"]]; 
    } 

} 
Các vấn đề liên quan