在多个分区中拆分视频

时间:2014-06-16 12:06:07

标签: ios objective-c video avassetwriter avasset

我在目标C中开发iOS App。

根据我的要求,我想将视频分成多个部分。

假设我有50秒的视频,我想将其分成5个10秒的视频。

如果你们有任何想法,请告诉我。

1 个答案:

答案 0 :(得分:0)

好问题。解决方案就在这里......

-(void)splitSecondVideo
{
if (did<splitdivide){

AVURLAsset *asset = [AVURLAsset URLAssetWithURL:_videourl options:nil];

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

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *myPathDocs;
CMTime starttime;
CMTime duration;

    myPathDocs =  [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"SplitFinalVideo%d.mov",did]];
    double endt=CMTimeGetSeconds([asset duration]);
    NSLog(@"All Duration : %f",endt);
    double divide=CMTimeGetSeconds([asset duration])/splitdivide;
    NSLog(@"All Duration : %f",divide);

    starttime = CMTimeMakeWithSeconds(divide*did, 1);
    duration = CMTimeMakeWithSeconds(divide, 1);

NSFileManager *fileManager=[[NSFileManager alloc]init];
NSError *error;
if ([fileManager fileExistsAtPath:myPathDocs] == YES) {
    [fileManager removeItemAtPath:myPathDocs error:&error];
}

exportSession.outputURL = [NSURL fileURLWithPath:myPathDocs];
exportSession.shouldOptimizeForNetworkUse = YES;
exportSession.outputFileType = AVFileTypeQuickTimeMovie;
// Trim to half duration

CMTimeRange secondrange = CMTimeRangeMake(starttime, duration);

exportSession.timeRange = secondrange;
[exportSession exportAsynchronouslyWithCompletionHandler:^(void)
 {
     [self exportDidFinish:exportSession];
     did++;

     [self splitSecondVideo];

 }];

}} 

- (void)exportDidFinish:(AVAssetExportSession*)session {
if (session.status == AVAssetExportSessionStatusCompleted) {
    NSURL *outputURL = session.outputURL;
    NSLog(@"Before Exported");
    [self SaveVideoAtPathWithURL:outputURL];
}} 

viewDidLoad方法中的 did = 0.0 。&amp; splitdivide 是您要创建视频部分的值。在您的问题中 splitdivide = 5 ; 注意:确实&amp;拆分鸿沟都是整数值

希望这有用....享受......