Eugene Trapeznikov
Eugene Trapeznikov

Reputation: 3240

How to get frame from video on iOS

In my app I want to take frames from a video for filtering them. I try to take frame frim video by time offset. This is my code:

- (UIImage*)getVideoFrameForTime:(NSDate*)time {

    CGImageRef thumbnailImageRef = NULL;

    NSError *igError = nil;

    NSTimeInterval timeinterval = [time timeIntervalSinceDate:self.videoFilterStart];

    CMTime atTime = CMTimeMakeWithSeconds(timeinterval, 1000);
    thumbnailImageRef =
    [self.assetImageGenerator copyCGImageAtTime:atTime
                                     actualTime:NULL
                                          error:&igError];

    if (!thumbnailImageRef) {
        NSLog(@"thumbnailImageGenerationError %@", igError );
    }

    UIImage *image = thumbnailImageRef ? [[UIImage alloc] initWithCGImage:thumbnailImageRef] : nil;

    return image;
}

Unfortunately, I see only frames which located on integer seconds: 1, 2, 3.. Even when time interval is non-integer (1.5, etc).

How to get frames at any non-integer interval?

Upvotes: 1

Views: 1011

Answers (3)

Marouane Lanouari
Marouane Lanouari

Reputation: 467

Use this project to get more frame details The corresponding project on github: iFrameExtractor.git

Upvotes: 1

Eugene Trapeznikov
Eugene Trapeznikov

Reputation: 3240

Thnx to @shallowThought I found an answer in this question Grab frames from video using Swift

You just need to add this two lines

assetImgGenerate.requestedTimeToleranceAfter = kCMTimeZero;
assetImgGenerate.requestedTimeToleranceBefore = kCMTimeZero;

Upvotes: 1

TheAmateurProgrammer
TheAmateurProgrammer

Reputation: 9392

If I remember correctly NSDate's accuracy only goes up to the second which explains why frames are only take on integer seconds. You'll have to use a different type of input to get frames at non-integer seconds.

Upvotes: 0

Related Questions