Jan49
Jan49

Reputation: 1818

Getting AVFoundationErrorDomain Code=-11829 "Cannot Open while trying generateCGImagesAsynchronouslyForTimes

i am developing an app where the recorded videos uploading to youtube. But i need to show thumb image for user and there is a trimming features also before uploading action.

For some of the recorded files i am getting an error while creating thumbnail images using generateCGImagesAsynchronouslyForTimes

 -(void)generateThumbnailsForAsset:(AVAsset *)asset thumbnailCount:(int)thumbnailCount andCompletionHandler:(void (^)(NSArray* thumbnailsArray))completionHandler
{
NSMutableArray *imagesArray = [[NSMutableArray alloc] init];
_imageGenerator = [[AVAssetImageGenerator alloc] initWithAsset:asset];

KMSDebugLog(@"generateThumbnailsForAsset");
if (thumbnailCount == 1)
{
    _imageGenerator.maximumSize = PREVIEW_IMAGE_SIZE;
}
else
{
    _imageGenerator.maximumSize = THUMBNAIL_SIZE;
}

CMTime duration = asset.duration;
AVAssetTrack *videoAssetTrack= [[asset tracksWithMediaType:AVMediaTypeVideo] lastObject];
CGAffineTransform videoTransform = videoAssetTrack.preferredTransform;

UIImageOrientation videoOrientation= UIImageOrientationUp;
if(videoTransform.a == -1.0 && videoTransform.b == 0 && videoTransform.c == 0 && videoTransform.d == -1.0)
{
    videoOrientation= UIImageOrientationDown;
}
CMTimeValue intervalSeconds = duration.value / thumbnailCount;
KMSDebugLog(@"duration.value :%lld  duration.timescale:%d",duration.value,duration.timescale);
CMTime time = kCMTimeZero;
NSMutableArray *times = [NSMutableArray array];
for (NSUInteger i = 0; i < thumbnailCount; i++) {
    [times addObject:[NSValue valueWithCMTime:time]];
    time = CMTimeAdd(time, CMTimeMake(intervalSeconds, duration.timescale));
}


/*[self.imageGenerator generateCGImagesAsynchronouslyForTimes:times completionHandler:^(CMTime requestedTime,
                                                                                      CGImageRef cgImage,
                                                                                      CMTime actualTime,
                                                                                      AVAssetImageGeneratorResult result,
                                                                                      NSError *error)*/
 [self.imageGenerator generateCGImagesAsynchronouslyForTimes:times completionHandler:^(CMTime requestedTime,
                                                                                       CGImageRef cgImage,
                                                                                       CMTime actualTime,
                                                                                       AVAssetImageGeneratorResult result,
                                                                                       NSError *error)

 {
     if (error)
     {
         KMSDebugLog(@"generateCGImagesAsynchronouslyForTimes Error: %@",error);
         completionHandler(imagesArray);
     }
     else
     {
         if (cgImage)
         {
             UIImage *image = [UIImage imageWithCGImage:cgImage];

             //Orientation support
             //UIImage *image = [UIImage imageWithCGImage:cgImage scale:1.0 orientation:videoOrientation];
             // NSData *imageData = [AVCaptureStillImageOutput jpegStillImageNSDataRepresentation:imageDataSampleBuffer];
             UIImage *rotatedImage = image;
             if(videoTransform.a == -1.0 && videoTransform.b == 0 && videoTransform.c == 0 && videoTransform.d == -1.0)
             {
                 rotatedImage = [self imageRotatedByDegrees:image deg:180];
             }

             [imagesArray addObject:rotatedImage];

         }

         if (imagesArray.count == thumbnailCount)
         {
             dispatch_async(dispatch_get_main_queue(), ^{
                 completionHandler(imagesArray);
             });
         }
     }
 }];
 }

ERROR

 Line: 265, generateCGImagesAsynchronouslyForTimes Error: Error Domain=AVFoundationErrorDomain
 Code=-11829 "Cannot Open" UserInfo={NSLocalizedDescription=Cannot Open,
 NSUnderlyingError=0x176f08b0 {Error Domain=NSOSStatusErrorDomain Code=-12848 "(null)"}, 
NSLocalizedFailureReason=This media may be damaged.}

i can't figure out the issue, Any help will be appreciated. thanks

Upvotes: 2

Views: 3778

Answers (2)

Julie Zhou
Julie Zhou

Reputation: 126

I ran into "Domain=AVFoundationErrorDomain Code=-11829" when the video is empty, check your video file

Upvotes: 0

Jan49
Jan49

Reputation: 1818

I got the answer. I just added some delay after record finished.

   [self performSelector:@selector(UpdateVideoAndConfigureScreenForURL:) withObject:videoURL afterDelay:0.2];

Upvotes: 1

Related Questions