如何使用AVAssetExportSession保存视频时提高视频速度

时间:2014-01-13 04:46:06

标签: ios avfoundation avmutablecomposition avasset avvideocomposition

我有一个视频文件可以加载到AVAsset中。 我希望在播放此视频时更改速率并将其速度提高一倍,这样如果视频长达10秒,它将在5秒内加速完成。

以下是我正在尝试的代码,有人可以告诉我哪里出错了吗?

帧持续时间是我需要的属性,以实现我的目标。

AVAsset *anAsset = self.moviePlayer.currentItem.asset;


NSArray *compatiblePresets = [AVAssetExportSession exportPresetsCompatibleWithAsset:anAsset];
if ([compatiblePresets containsObject:AVAssetExportPreset640x480]) {
    AVAssetExportSession *exportSession = [[AVAssetExportSession alloc]
                                           initWithAsset:anAsset presetName:AVAssetExportPreset640x480];
    // Implementation continues.

    NSString *tmpStr = [[aclip selectedTakeUrl] stringByReplacingOccurrencesOfString:@".m4v" withString:@""];

    NSString *filePath = [NSString stringWithFormat:@"%@_applied.m4v", tmpStr];


    exportSession.outputURL = [NSURL fileURLWithPath:filePath];

    AVMutableVideoComposition *vidcomp = [AVMutableVideoComposition videoCompositionWithPropertiesOfAsset:anAsset];

    vidcomp.frameDuration = CMTimeMake(1, 24);
    //self.moviePlayer.currentItem.videoComposition = vidcomp;

    exportSession.videoComposition = vidcomp;
    //exportSession.videoComposition.frameDuration = CMTimeMake(1, 50);

    // what is the song URL before loading startRecordingViewController?
    NSLog(@"From Save settgins Choose Song -Song URL : %@", exportSession.outputURL);

    //   NSLog(@"start time%f, end time %f", CMTimeGetSeconds(self.startTime),CMTimeGetSeconds(self.endTime));

    exportSession.outputFileType = AVFileTypeMPEG4;

    //CMTimeRange range = CMTimeRangeMake(CMTimeMake(0.0, 1), CMTimeMake(diffTime/currentround,1));
    // startTime and endTime is the duration that we need to save.
    //exportSession.timeRange = range;


    [exportSession exportAsynchronouslyWithCompletionHandler:^{

        switch ([exportSession status]) {
            case AVAssetExportSessionStatusCompleted:
                NSLog(@"Export Completed");

                break;
            case AVAssetExportSessionStatusWaiting:
                NSLog(@"Export Waiting");
                break;
            case AVAssetExportSessionStatusExporting:
                NSLog(@"Export Exporting");
                break;
            case AVAssetExportSessionStatusFailed:
            {
                NSError *error = [exportSession error];
                NSLog(@"Export failed: %@", [error localizedDescription]);

                break;
            }
            case AVAssetExportSessionStatusCancelled:
                NSLog(@"Export canceled");

                break;
            default:
                break;
        }

        dispatch_async(dispatch_get_main_queue(), ^{

            //[activityIndicator stopAnimating];

            //CMTime range = CMTimeSubtract(self.endTime, self.startTime);
            //NSUInteger theLength = (int)CMTimeGetSeconds(range);

            //[ self.songPickedDelegate songPickerDidMakeSelection:self.songTitle :self.audioPath :theLength];





        });

        //[exportSession release];
    }];
}

1 个答案:

答案 0 :(得分:4)

您可以使用 - [AVMutableCompositionTrack scaleTimeRange:toDuration:]将整个资源从10秒缩放到5秒。 frameDuration属性将简单地执行帧速率转换。

相关问题