youtube视频的缩略图

时间:2013-09-20 07:00:45

标签: iphone ios video

我正在尝试为youtube视频创建缩略图,但我收到以下错误: -

Error Domain=AVFoundationErrorDomain Code=-11850 "Operation Stopped" UserInfo=0xa07ac00            NSLocalizedDescription=Operation Stopped, NSUnderlyingError=0xa07e580 "The operation couldn’t   be completed. (OSStatus error -12939.)", NSLocalizedFailureReason=The server is not correctly configured.

请帮助我从youtube链接创建YouTube视频的缩略图。

用于创建缩略图的代码如下: -

NSURL *url = [NSURL URLWithString:strVideoURL];

AVURLAsset *asset=[[AVURLAsset alloc] initWithURL:url options:nil];
AVAssetImageGenerator *generator = [[AVAssetImageGenerator alloc] initWithAsset:asset];
generator.appliesPreferredTrackTransform=TRUE;

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);
    }
    // TODO Do something with the image
    NSLog(@"CGImage---- %@", im);
    UIImage *thumbnail=[UIImage imageWithCGImage:im];

    UIImageView *imageView=[[UIImageView alloc] initWithFrame:CGRectMake(30, 50, 120, 120)];
    imageView.backgroundColor=[UIColor redColor];
    imageView.image=thumbnail;
    [image addSubview:imageView];

    UIView *view=(UIView*)[self.view viewWithTag:10];
    [view removeFromSuperview];
};

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

1 个答案:

答案 0 :(得分:1)

虽然你的问题没有你的任何源代码所以我不知道你哪里出错了。试试这个,它为我工作。

#define YOUTUBE_URL @"https://gdata.youtube.com/feeds/api/videos?v=2&author=%@&orderby=published"

NSString *userName = @"ipl";
_url = [NSURL URLWithString:[NSString stringWithFormat:YOUTUBE_URL,userName]];

-(void) fetchYoutubeThumbnails {
    NSURLRequest *_request = [[NSURLRequest alloc] initWithURL: _url];
    NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:_request delegate:self];
    [connection start];
}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {

    [recievedData appendData:data]; //recievedData is a class variable of type NSMutableData
}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection {

    NSString *xmlString = [[NSString alloc] initWithData:recievedData encoding:NSUTF8StringEncoding];
    NSDictionary *result = [XMLReader dictionaryForXMLString:xmlString error:Nil];

    if(result.count > 0) {

        NSMutableArray *items = [[result objectForKey:@"feed"] objectForKey:@"entry"];
        for(int i = items.count -1; i >= 0; i--) {
            NSDictionary *item = [items objectAtIndex:i];
            NSString *videoThumbnaulUrl = [[[[item valueForKey:@"media:group"] valueForKey:@"media:thumbnail"] objectAtIndex:0] valueForKey:@"url"];
    // Use this url to get the thumbnail.  

    }
}