播放和保存视频文件

时间:2013-01-08 10:34:10

标签: iphone objective-c

我录制了一个视频并将其保存到文档目录中。 现在我想将视频编辑为慢速,快速或正常速度。 我能够快速和正常的视频速度。 但我无法做到视频速度慢。它总是转换为正常速度。 这是我的代码

    AVMutableComposition *composition = [AVMutableComposition composition];
    AVURLAsset * sourceAsset = [[AVURLAsset alloc] initWithURL:[NSURL fileURLWithPath:inputVideoPath] options:nil];
    AVMutableCompositionTrack *compositionVideoTrack = [composition addMutableTrackWithMediaType:AVMediaTypeVideo preferredTrackID:kCMPersistentTrackID_Invalid];
    BOOL ok = NO;

    AVAssetTrack * sourceVideoTrack = [[sourceAsset tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0];

    CMTimeRange x = CMTimeRangeMake(kCMTimeZero, [sourceAsset duration]);

    ok = [compositionVideoTrack insertTimeRange:x ofTrack:sourceVideoTrack  atTime:kCMTimeZero error:nil];
    CMTime fiveSecondsIn;
    if([Cell.text isEqualToString:@"Fast"]){
        fiveSecondsIn = CMTimeMake(10,1);
    }
    else if([Cell.text isEqualToString:@"Normal"]){
        fiveSecondsIn = CMTimeMake(1,1);
    }
    else if([Cell.textLabel.text isEqualToString:@"Slow"]){
        fiveSecondsIn = CMTimeMake(1,.5);
    }


    CMTimeRange video_timeRange = CMTimeRangeMake(kCMTimeZero,sourceAsset.duration);


    [compositionVideoTrack  scaleTimeRange:video_timeRange toDuration:fiveSecondsIn];


    if([[NSFileManager defaultManager] fileExistsAtPath:filePath]){
         [[NSFileManager defaultManager] removeItemAtPath:filePath error:nil];
    }

    NSURL    *exportUrl = [NSURL  fileURLWithPath:filePath];
    AVAssetExportSession *exporter = [[AVAssetExportSession alloc]initWithAsset:composition presetName:AVAssetExportPresetHighestQuality];
    exporter.outputURL=exportUrl;
    exporter.outputFileType = @"com.apple.quicktime-movie";
    [exporter exportAsynchronouslyWithCompletionHandler:^(void){

       [self performSelectorOnMainThread:@selector(playnewVideo) withObject:nil waitUntilDone:NO];

    }];

0 个答案:

没有答案