1

I am trying to extract 2 frames per second from a video using generateCGImagesAsynchronouslyForTimes. But my app crashes. I am monitoring the memory usage but its not going any way up than 14 mb.

Here is the code:

- (void) createImagesFromVideoURL:(NSURL *) videoUrl atFPS: (int) reqiuredFPS completionBlock: (void(^) (NSMutableArray *frames, CGSize frameSize)) block
{
    NSMutableArray *requiredFrames = [[NSMutableArray alloc] init];

    AVURLAsset *asset = [[AVURLAsset alloc] initWithURL:videoUrl options:nil];

    AVAssetImageGenerator *generator = [[AVAssetImageGenerator alloc] initWithAsset:asset];

    generator.requestedTimeToleranceAfter =  kCMTimeZero;

    generator.requestedTimeToleranceBefore =  kCMTimeZero;

    generator.appliesPreferredTrackTransform = YES;

    UIImage *sampleGeneratedImage;

    for (Float64 i = 0; i < CMTimeGetSeconds(asset.duration) *  reqiuredFPS ; i++)
    {
        CMTime time = CMTimeMake(i, reqiuredFPS);

        NSError *err;

        CMTime actualTime;

        CGImageRef image = [generator copyCGImageAtTime:time actualTime:&actualTime error:&err];

        if (! err)
        {
            sampleGeneratedImage = [[UIImage alloc] initWithCGImage:image];
            break;
        }
    }

    //Get the maximum size from 1 frame
    generator.maximumSize = [self getMultipleOf16AspectRatioForCurrentFrameSize:sampleGeneratedImage.size];

    NSMutableArray *requestedFrameTimes = [[NSMutableArray alloc] init];

    for (Float64 i = 0; i < CMTimeGetSeconds(asset.duration) *  reqiuredFPS ; i++)
    {
        CMTime time = CMTimeMake(i, reqiuredFPS);

        [requestedFrameTimes addObject:[NSValue valueWithCMTime:time]];
    }

    [generator generateCGImagesAsynchronouslyForTimes:[requestedFrameTimes copy] completionHandler:^(CMTime requestedTime, CGImageRef  _Nullable image, CMTime actualTime, AVAssetImageGeneratorResult result, NSError * _Nullable error) {

        if (image)
        {
            UIImage *generatedImage = [UIImage imageWithCGImage:image];

            [requiredFrames addObject:generatedImage];
        }

        if (CMTimeCompare(requestedTime, [[requestedFrameTimes lastObject] CMTimeValue]) == 0)
        {
            NSLog(@"Image processing complete");

            dispatch_async(dispatch_get_main_queue(), ^{
                block(requiredFrames, generator.maximumSize);
            });
        }

        else
        {
            NSLog(@"Getting frame at %lld", actualTime.value/actualTime.timescale);
        }
    }];
}
nr5
  • 4,228
  • 8
  • 42
  • 82
  • What is the crash that you are getting and which line causes it? – Tamás Sengel Jul 04 '17 at 15:51
  • Nothing it dies on me when extracting 33 or 34 th frame. Wihout any error – nr5 Jul 04 '17 at 15:59
  • this will not answer Your question, but You should release `CGImage` if you call `copyCGImageAtTime` – Tiko Jul 04 '17 at 18:58
  • You can't retain all those images/frames without running out of memory. Make sure to only retain a few at a time. – Rhythmic Fistman Jul 04 '17 at 23:57
  • But I am using `copyCGImageAtTime` only once to get frame size. For rest of the frames, `generateCGImagesAsynchronouslyForTimes` is used. – nr5 Jul 05 '17 at 06:36
  • @Tiko Any suggestion how can I check out the exact error? Via instruments or something like that. Debugger is not giving any detail. – nr5 Jul 05 '17 at 14:46

0 Answers0