Recording Slow Motion Video in iOS using objective C

245 Views Asked by At

I am trying to record slow motion video I have used AVCaptureSession to record the video at 240 fps and for slow motion effect I am using scaleTimeRange:

here is the code:

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)];

but the output is not like iOS native slow motion camera how to improve the smoothness quality.

0

There are 0 best solutions below