使用物镜C在iOS中录制慢动作视频

时间:2019-03-20 07:48:32

标签: ios objective-c camera avfoundation slowmotion

我正在尝试录制慢动作视频,我已经使用AVCaptureSession以240 fps的速度录制视频,并且为了获得慢动作效果,我正在使用scaleTimeRange:

代码如下:

AVURLAsset* videoAsset = [AVURLAsset URLAssetWithURL:URl options:nil];

//create mutable composition
AVMutableComposition *mixComposition = [AVMutableComposition composition];

AVMutableCompositionTrack *compositionVideoTrack = [mixComposition addMutableTrackWithMediaType:AVMediaTypeVideo
                                                                               preferredTrackID:kCMPersistentTrackID_Invalid];

AVMutableCompositionTrack *compositionAudioTrack = [mixComposition addMutableTrackWithMediaType:AVMediaTypeAudio
                                                                               preferredTrackID:kCMPersistentTrackID_Invalid];

NSError *videoInsertError = nil;
NSError *audioInsertError = nil;
BOOL videoInsertResult = [compositionVideoTrack insertTimeRange:CMTimeRangeMake(kCMTimeZero, videoAsset.duration)
                                                        ofTrack:[[videoAsset tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0]
                                                         atTime:kCMTimeZero
                                                          error:&videoInsertError];
BOOL audioInsertResult =  [compositionAudioTrack insertTimeRange:CMTimeRangeMake(kCMTimeZero, videoAsset.duration)
                                                         ofTrack:[[videoAsset tracksWithMediaType:AVMediaTypeAudio] objectAtIndex:0]
                                                          atTime:kCMTimeZero
                                                           error:&audioInsertError];
if (!videoInsertResult || nil != videoInsertError) {
    //handle error
    return;
}

if (!audioInsertResult || nil != audioInsertError) {
    //handle error
    return;
}

//slow down whole video by 3.0
double videoScaleFactor = 3.0;
CMTime videoDuration = videoAsset.duration;


[compositionVideoTrack scaleTimeRange:CMTimeRangeMake(CMTimeMake(1, videoDuration.timescale), CMTimeMake(videoDuration.value - 1, videoDuration.timescale))
                           toDuration:CMTimeMake(videoDuration.value*videoScaleFactor, videoDuration.timescale)];

但是输出不像iOS原生慢动作相机那样如何提高平滑度质量。

0 个答案:

没有答案