2013-06-11 66 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

你在信息中扮演什么角色,你在哪里称此方法 –