2012-10-03 47 views
11

目前我正在处理一个处理视频的应用程序。 在我的应用程序中,用户可以修剪视频,我有一个用于选择开始时间和结束时间的自定义控件。我需要通过这两个值来裁剪视频。我尝试用UIVideoEditorController如下。修剪视频而不显示UIVideoEditorController?

UIVideoEditorController* videoEditor = [[[UIVideoEditorController alloc] init] autorelease]; 
    videoEditor.delegate = self; 
    NSString* videoPath = [[NSBundle mainBundle] pathForResource:@"test" ofType:@"MOV"]; 
    if ([UIVideoEditorController canEditVideoAtPath:videoPath]) 
    { 
     videoEditor.videoPath = videoPath; 
     [self presentModalViewController:videoEditor animated:YES]; 
    } 
    else 
    { 
     NSLog(@"can't edit video at %@", videoPath); 
    } 

但问题是上面的代码会显示苹果的视频编辑器控件,用户可以对该视图执行一些操作。我不想显示此视图,因为我已经在MPMoviePlayer上显示了视频,并收到了用于在自定义控件上剪辑视频的用户输入(开始时间结束时间)。 如何修剪视频而不显示UIVideoEditorController

+1

u能为我提供乌尔修剪视频,在这里用户可以选择开始和结束时间码? –

回答

17

最后我找到了解决方案。

我们可以使用AVAssetExportSession来修剪视频而不显示UIVideoEditorController

我的代码是这样的:

- (void)splitVideo:(NSString *)outputURL 
{ 

    @try 
    { 
     NSString *videoBundleURL = [[NSBundle mainBundle] pathForResource:@"Video_Album" ofType:@"mp4"]; 

     AVAsset *asset = [[AVURLAsset alloc] initWithURL:[NSURL fileURLWithPath:videoBundleURL] options:nil]; 

     NSArray *compatiblePresets = [AVAssetExportSession exportPresetsCompatibleWithAsset:asset]; 

     if ([compatiblePresets containsObject:AVAssetExportPresetLowQuality]) 
     { 

      [self trimVideo:outputURL assetObject:asset]; 

     } 
     videoBundleURL = nil; 

     [asset release]; 
     asset = nil; 

     compatiblePresets = nil; 
    } 
    @catch (NSException * e) 
    { 
     NSLog(@"Exception Name:%@ Reason:%@",[e name],[e reason]); 
    } 
} 

这种方法修剪视频

- (void)trimVideo:(NSString *)outputURL assetObject:(AVAsset *)asset 
    { 

    @try 
    { 

     AVAssetExportSession *exportSession = [[AVAssetExportSession alloc]initWithAsset:asset presetName:AVAssetExportPresetLowQuality]; 

     exportSession.outputURL = [NSURL fileURLWithPath:outputURL]; 

     exportSession.outputFileType = AVFileTypeQuickTimeMovie; 

     CMTime start = CMTimeMakeWithSeconds(splitedDetails.startTime, 1); 

     CMTime duration = CMTimeMakeWithSeconds((splitedDetails.stopTime - splitedDetails.startTime), 1); 

     CMTimeRange range = CMTimeRangeMake(start, duration); 

     exportSession.timeRange = range; 

     exportSession.outputFileType = AVFileTypeQuickTimeMovie; 

     [self checkExportSessionStatus:exportSession]; 

     [exportSession release]; 
     exportSession = nil; 

    } 
    @catch (NSException * e) 
    { 
     NSLog(@"Exception Name:%@ Reason:%@",[e name],[e reason]); 
    } 
} 

此方法检查修整的状态:

- (void)checkExportSessionStatus:(AVAssetExportSession *)exportSession 
    { 

    [exportSession exportAsynchronouslyWithCompletionHandler:^(void) 
    { 

     switch ([exportSession status]) 
      { 

      case AVAssetExportSessionStatusCompleted: 

       NSLog(@"Export Completed"); 
       break; 

      case AVAssetExportSessionStatusFailed: 

       NSLog(@"Error in exporting"); 
       break; 

      default: 
       break; 

     } 
    }]; 
} 

我打电话splitVideo方法从导出按钮操作方法并通过输出UR L作为参数。

+0

什么是输出url – Warewolf

+1

@Khoool:outputUrl用于写入修剪后的视频。这是一个在文件目录中的文件路径 –

+0

我使用相同的代码,但接收错误:导出NSString * path = [NSSearchPathForDirectoriesInDomains(NSDocumentationDirectory,NSUserDomainMask,YES)objectAtIndex:0]时出错。 path = [path stringByAppendingPathComponent:@“new.mov”]; NSLog(@“保存视频的路径是%@”,路径); [self splitVideo:path]; – Warewolf

2

我们可以导入AVFoundation/AVFoundation.h

-(BOOL)trimVideofile 
{ 

    float videoStartTime;//define start time of video 
    float videoEndTime;//define end time of video 
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; 
    [dateFormatter setDateFormat:@"yyyy-MM-dd_HH-mm-ss"]; 
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES); 
    NSString *libraryCachesDirectory = [paths objectAtIndex:0]; 
    libraryCachesDirectory = [libraryCachesDirectory stringByAppendingPathComponent:@"Caches"]; 
    NSString *OutputFilePath = [libraryCachesDirectory stringByAppendingFormat:@"/output_%@.mov", [dateFormatter stringFromDate:[NSDate date]]]; 
    NSURL *videoFileOutput = [NSURL fileURLWithPath:OutputFilePath]; 
    NSURL *videoFileInput;//<Path of orignal Video file> 

    if (!videoFileInput || !videoFileOutput) 
    { 
     return NO; 
    } 

    [[NSFileManager defaultManager] removeItemAtURL:videoFileOutput error:NULL]; 
    AVAsset *asset = [AVAsset assetWithURL:videoFileInput]; 

    AVAssetExportSession *exportSession = [AVAssetExportSession exportSessionWithAsset:asset 
                      presetName:AVAssetExportPresetLowQuality]; 
    if (exportSession == nil) 
    { 
     return NO; 
    } 
    CMTime startTime = CMTimeMake((int)(floor(videoStartTime * 100)), 100); 
    CMTime stopTime = CMTimeMake((int)(ceil(videoEndTime * 100)), 100); 
    CMTimeRange exportTimeRange = CMTimeRangeFromTimeToTime(startTime, stopTime); 

    exportSession.outputURL = videoFileOutput; 
    exportSession.timeRange = exportTimeRange; 
    exportSession.outputFileType = AVFileTypeQuickTimeMovie; 

    [exportSession exportAsynchronouslyWithCompletionHandler:^ 
    { 
     if (AVAssetExportSessionStatusCompleted == exportSession.status) 
     { 
      NSLog(@"Export OK"); 
     } 
     else if (AVAssetExportSessionStatusFailed == exportSession.status) 
     { 
      NSLog(@"Export failed: %@", [[exportSession error] localizedDescription]); 
     } 
    }]; 
    return YES; 
} 
+0

这个答案和上面一样,复制答案的优点是什么 –

+0

这不是复制答案。在这种情况下,我们正在使用另一种不同的解决方案。 –

+0

在上面的答案中,它也使用'AVAssetExportSession'。这里也是同样的事情,那有什么区别? –