Rohan
Rohan

Reputation: 2935

Get / Make thumbnail image from video url getting in API response

I am getting video URLs in the response from server (i.e from API not stored in local-device) as follwos ;

videos": [
    "<iframe width=\\"560\\" height=\\"315\\" src=\\"//www.youtube.com/embed/-OB7y6ELDks?list=UUYv2cbGZE2SmAYPDZQRKiig\\" frameborder=\\"0\\" allowfullscreen></iframe>",
    "<iframe width=\\"560\\" height=\\"315\\" src=\\"//www.youtube.com/embed/igcIaNi-eHA?list=UUYv2cbGZE2SmAYPDZQRKiig\\" frameborder=\\"0\\" allowfullscreen></iframe>",
    "<iframe width=\\"560\\" height=\\"315\\" src=\\"//www.youtube.com/embed/3kGNMUfajJc?list=UUYv2cbGZE2SmAYPDZQRKiig\\" frameborder=\\"0\\" allowfullscreen></iframe>"    
],

Currently it contains only youtube videos, but it is not sure, it may also contain url videos from other resources (i.e other than youtube).

I extracted youtube urls from this response. And want to display thumbnails of these videos in the UICollectionView.

How can i do this?

I searched a lot about this but could not get any proper solution.

Please help me.

Thanks..

Upvotes: 0

Views: 3870

Answers (4)

AzSh
AzSh

Reputation: 135

Check this answer How do I get a YouTube video thumbnail from the YouTube API?

Each YouTube video has 4 generated images. They are predictably formatted as follows:

http://img.youtube.com/vi/<insert-youtube-video-id-here>/0.jpg
http://img.youtube.com/vi/<insert-youtube-video-id-here>/1.jpg
http://img.youtube.com/vi/<insert-youtube-video-id-here>/2.jpg
http://img.youtube.com/vi/<insert-youtube-video-id-here>/3.jpg

The first one in the list is a full size image and others are thumbnail images. The default thumbnail image (ie. one of 1.jpg, 2.jpg, 3.jpg) is:

http://img.youtube.com/vi/<insert-youtube-video-id-here>/default.jpg

Upvotes: 1

Rohan
Rohan

Reputation: 2935

I have solved my problem using following code.

So I am attaching code here for others reference ;

-(void)setImage:(NSString*)urlResponse
{
    NSString *youtubeUrl = [NSString stringWithFormat:@"%@",[self parseVideoHTMLUrl: urlResponse]];

    NSString *videoThumbUrl = [self getYoutubeVideoThumbnail: youtubeUrl];

    NSURL* videoURL =[NSURL URLWithString:videoThumbUrl];

    [btn_photo sd_setImageWithURL:videoURL forState:UIControlStateNormal]; 
}

-(NSString *)parseVideoHTMLUrl:(NSString *)videoUrl
{
    NSRegularExpression *regex = [NSRegularExpression
                                  regularExpressionWithPattern:@";//(.+?)\\&quot;"
                                  options:NSRegularExpressionCaseInsensitive
                                  error:nil];
    NSTextCheckingResult *textCheckingResult = [regex firstMatchInString:videoUrl options:0 range:NSMakeRange(0, videoUrl.length)];

    NSString *url = [videoUrl substringWithRange:[textCheckingResult rangeAtIndex:1]];

    return url;
}

-(NSString*)getYoutubeVideoThumbnail:(NSString*)youTubeUrl
{
    NSString* video_id = @"";

    if (youTubeUrl.length > 0)
    {
        NSError *error = NULL;
        NSRegularExpression *regex =
        [NSRegularExpression regularExpressionWithPattern:@"(?<=watch\\?v=|/videos/|embed\\/)[^#\\&\\?]*"
                                                  options:NSRegularExpressionCaseInsensitive
                                                    error:&error];
        NSTextCheckingResult *match = [regex firstMatchInString:youTubeUrl
                                                        options:0
                                                          range:NSMakeRange(0, [youTubeUrl length])];
        if (match)
        {
            NSRange videoIDRange = [match rangeAtIndex:0];
            video_id = [youTubeUrl substringWithRange:videoIDRange];

            NSLog(@"%@",video_id);
        }
    }

    NSString* thumbImageUrl = [NSString stringWithFormat:@"http://img.youtube.com/vi/%@/default.jpg",video_id];

    return thumbImageUrl;
}

Upvotes: 1

Alap Anerao
Alap Anerao

Reputation: 2153

try this one

-(void)generateImage
{
AVURLAsset *asset=[[AVURLAsset alloc] initWithURL:self.url options:nil];
AVAssetImageGenerator *generator = [[AVAssetImageGenerator alloc] initWithAsset:asset];
generator.appliesPreferredTrackTransform=TRUE;
[asset release];
CMTime thumbTime = CMTimeMakeWithSeconds(0,30);

AVAssetImageGeneratorCompletionHandler handler = ^(CMTime requestedTime, CGImageRef im, CMTime actualTime, AVAssetImageGeneratorResult result, NSError *error){
    if (result != AVAssetImageGeneratorSucceeded) {
        NSLog(@"couldn't generate thumbnail, error:%@", error);
    }
    [button setImage:[UIImage imageWithCGImage:im] forState:UIControlStateNormal];
    thumbImg=[[UIImage imageWithCGImage:im] retain];
    [generator release];
};

CGSize maxSize = CGSizeMake(320, 180);
generator.maximumSize = maxSize;
[generator generateCGImagesAsynchronouslyForTimes:[NSArray arrayWithObject:[NSValue valueWithCMTime:thumbTime]] completionHandler:handler];
}

Update

try below

AVAsset *asset = [AVAsset assetWithURL:sourceURL];
AVAssetImageGenerator *imageGenerator = [[AVAssetImageGenerator alloc]initWithAsset:asset];
CMTime time = CMTimeMake(1, 1);
CGImageRef imageRef = [imageGenerator copyCGImageAtTime:time actualTime:NULL error:NULL];
UIImage *thumbnail = [UIImage imageWithCGImage:imageRef];
CGImageRelease(imageRef);

Upvotes: 1

Alap Anerao
Alap Anerao

Reputation: 2153

NSURL *videoURL = [NSURL fileURLWithPath:url];

MPMoviePlayerController *player = [[MPMoviePlayerController alloc] initWithContentURL:videoURL];

UIImage *thumbnail = [player thumbnailImageAtTime:1.0 timeOption:MPMovieTimeOptionNearestKeyFrame];

//Stop player
[player stop];
[player release];

Upvotes: 0

Related Questions