2013-06-11 87 views
0

我有一個來自設備攝像頭的視頻文件 - 例如存儲爲/private/var/mobile/Media/DCIM/100APPLE/IMG_0203.MOV,我需要將剪輯的前10秒這個視頻。我可以使用哪些API或庫?在iPhone上剪切視頻

+1

我不知道是否它越獄相關問題。 –

回答

1

我發現與標準的API解決方案:AVAssetExportSession

- (void)getTrimmedVideoForFile:(NSString *)filePath withInfo:(NSArray *)info 
{ 
//[[NSFileManager defaultManager] removeItemAtURL:outputURL error:nil]; 
AVURLAsset *asset = [AVURLAsset URLAssetWithURL:[NSURL fileURLWithPath:filePath] options:nil]; 
AVAssetExportSession *exportSession = [[AVAssetExportSession alloc] initWithAsset:asset presetName:AVAssetExportPresetLowQuality]; 

// NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSAllDomainsMask, YES); 
// NSString *outputURL = paths[0]; 
NSFileManager *manager = [NSFileManager defaultManager]; 
// [manager createDirectoryAtPath:outputURL withIntermediateDirectories:YES attributes:nil error:nil]; 
// outputURL = [outputURL stringByAppendingPathComponent:@"output.mp4"]; 
NSString *outputURL = [NSString stringWithFormat:@"/tmp/%@.mp4", [info objectAtIndex:2]]; 
NSLog(@"OUTPUT: %@", outputURL); 
// Remove Existing File 
// [manager removeItemAtPath:outputURL error:nil]; 

if (![manager fileExistsAtPath:outputURL]) { 
    exportSession.outputURL = [NSURL fileURLWithPath:outputURL]; 
    exportSession.shouldOptimizeForNetworkUse = YES; 
    exportSession.outputFileType = AVFileTypeQuickTimeMovie; 
    CMTime start = kCMTimeZero; 
    CMTime duration = kCMTimeIndefinite; 
    if ([[NSString stringWithFormat:@"%@", [info objectAtIndex:3]] floatValue] > 20.0) { 
     start = CMTimeMakeWithSeconds(1.0, 600); 
     duration = CMTimeMakeWithSeconds(10.0, 600); 
    } 

    CMTimeRange range = CMTimeRangeMake(start, duration); 
    exportSession.timeRange = range; 
    [exportSession exportAsynchronouslyWithCompletionHandler:^(void) { 
     switch (exportSession.status) { 
      case AVAssetExportSessionStatusCompleted: 
       NSLog(@"Export Complete %d %@", exportSession.status, exportSession.error); 
       [self sendVideoPreview:info]; 
       break; 
      case AVAssetExportSessionStatusFailed: 
       NSLog(@"Failed:%@",exportSession.error); 
       // [self addToDelayed:info withAction:@"add"]; 
       break; 
      case AVAssetExportSessionStatusCancelled: 
       NSLog(@"Canceled:%@",exportSession.error); 
       // [self addToDelayed:info withAction:@"add"]; 
       break; 
      default: 
       break; 
     } 
    }]; 
} else { 
    [self sendVideoPreview:info]; 
} 
+0

你在信息中扮演什麼角色,你在哪裏稱此方法 –