Extract thumbnail from video url

前端 未结 6 696
悲哀的现实
悲哀的现实 2021-01-05 15:57

I have to extract a thumbnail from a video (from url) and I use this code:

NSString *stringUrl = video.stringurl;
NSURL *url = [NSURL URLWithString:stringUrl         


        
6条回答
  •  挽巷
    挽巷 (楼主)
    2021-01-05 16:48

    I'm trying as well to grab a screenshot from an HLS variable bitrate stream, I.E. M3U8, and none of the methods provided here worked for me.

    I did succeed at the end. First you need to attach an AVPlayerItemVideoOutput to your player:

     self.playerAV = [AVPlayer playerWithURL:localURL];
    
        NSDictionary* settings = @{ (id)kCVPixelBufferPixelFormatTypeKey : [NSNumber numberWithInt:kCVPixelFormatType_32BGRA] };
        AVPlayerItemVideoOutput* output = [[AVPlayerItemVideoOutput alloc] initWithPixelBufferAttributes:settings];
        [self.playerAV.currentItem addOutput:output];
    

    Now when you wanna grab the screenshot:

      CVPixelBufferRef pixelBuffer = [output copyPixelBufferForItemTime:player.currentTime itemTimeForDisplay:nil];
                CIImage *ciImage = [CIImage imageWithCVPixelBuffer:pixelBuffer];
    
                CIContext *temporaryContext = [CIContext contextWithOptions:nil];
                CGImageRef videoImage = [temporaryContext
                                         createCGImage:ciImage
                                         fromRect:CGRectMake(0, 0,
                                                             CVPixelBufferGetWidth(pixelBuffer),
                                                             CVPixelBufferGetHeight(pixelBuffer))];
    
                image = [UIImage imageWithCGImage:videoImage];
                image = [image cropImageToSize:maxSize withProportionDiffLargerThan:IMAGE_PROPORTION_DIFF];
    
                if ( videoImage )
                {
                    CGImageRelease(videoImage);
                }
    

提交回复
热议问题