2015-07-12 44 views
1

我的應用程序捕獲一個視頻剪輯3秒鐘,並以編程方式我想從記錄的3秒剪輯創建一個15秒的剪輯,循環5次。最後必須在CameraRoll中保存15秒的剪輯。從.mov文件創建`CMSampleBufferRef`

我有我的3秒視頻剪輯通過AVCaptureMovieFileOutput,我有它的NSURL從委託當前在NSTemporaryDirectory()

我使用AVAssetWriterInput來循環它。但它要求CMSampleBufferRef,如:

[writerInput appendSampleBuffer:sampleBuffer]; 

如何將我在NSTemporaryDirectory嘖嘖這CMSampleBufferRef從視頻()?

我看過代碼轉換UIImageCMSampleBufferRef,但我可以找到任何視頻文件。

任何建議都會有幫助。 :)

回答

2

最後,我使用AVMutableComposition修復了我的問題。這是我的代碼:

AVMutableComposition *mixComposition = [AVMutableComposition new]; 
AVMutableCompositionTrack *mutableCompVideoTrack = [mixComposition addMutableTrackWithMediaType:AVMediaTypeVideo preferredTrackID:kCMPersistentTrackID_Invalid]; 

AVURLAsset *videoAsset = [[AVURLAsset alloc]initWithURL:3SecFileURL options:nil]; 
CMTimeRange video_timeRange = CMTimeRangeMake(kCMTimeZero, [videoAsset duration]); 

CGAffineTransform rotationTransform = CGAffineTransformMakeRotation(M_PI_2); 
[mutableCompVideoTrack setPreferredTransform:rotationTransform]; 

CMTime currentCMTime = kCMTimeZero; 

for (NSInteger count = 0 ; count < 5 ; count++) 
{ 
    [mutableCompVideoTrack insertTimeRange:video_timeRange ofTrack:[[videoAsset tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0] atTime:currentCMTime error:nil]; 
    currentCMTime = CMTimeAdd(currentCMTime, [videoAsset duration]); 
} 

NSString *fullMoviePath = [NSTemporaryDirectory() stringByAppendingPathComponent:[@"moviefull" stringByAppendingPathExtension:@"mov"]]; 
NSURL *finalVideoFileURL = [NSURL fileURLWithPath:fullMoviePath]; 

AVAssetExportSession *exportSession = [[AVAssetExportSession alloc] initWithAsset:mixComposition presetName:AVAssetExportPresetPassthrough]; 
[exportSession setOutputFileType:AVFileTypeQuickTimeMovie]; 
[exportSession setOutputURL:finalVideoFileURL]; 

CMTimeValue val = [mixComposition duration].value; 
CMTime start = CMTimeMake(0, 1); 
CMTime duration = CMTimeMake(val, 1); 
CMTimeRange range = CMTimeRangeMake(start, duration); 
[exportSession setTimeRange:range]; 

[exportSession exportAsynchronouslyWithCompletionHandler:^{ 

    switch ([exportSession status]) 
    { 
     case AVAssetExportSessionStatusFailed: 
     { 
      NSLog(@"Export failed: %@ %@", [[exportSession error] localizedDescription], [[exportSession error]debugDescription]); 
     } 
     case AVAssetExportSessionStatusCancelled: 
     { 
      NSLog(@"Export canceled"); 
      break; 
     } 
     case AVAssetExportSessionStatusCompleted: 
     { 
      NSLog(@"Export complete!"); 
     } 

     default: NSLog(@"default"); 
    } 
}]; 
1

看看AVAssetReader,這可以返回一個CMSampleBufferRef。請記住,您需要操作您的工作方法的時間戳。