本文介绍了从视频网址中提取缩略图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须从视频中提取缩略图(来自网址)并使用此代码:

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];

AVURLAsset *asset = [[AVURLAsset alloc] initWithURL:url options:nil];
AVAssetImageGenerator *imageGenerator = [[AVAssetImageGenerator alloc]initWithAsset:asset];
[imageGenerator setRequestedTimeToleranceBefore:kCMTimeZero];
[imageGenerator setRequestedTimeToleranceAfter:kCMTimeZero];

CGImageRef imageRef = [imageGenerator copyCGImageAtTime:playerCurrentTime actualTime:&actualtime error:&error];
UIImage *thumbnail = [UIImage imageWithCGImage:imageRef];

CGImageRelease(imageRef);

但有时我在copyCGImageAtTime时出错,并且没有生成缩略图。错误是:错误保存图像错误域= AVFoundationErrorDomain代码= -11800操作无法完成(OSStatus错误-12792。),NSLocalizedFailureReason =发生未知错误(-12792)}

But sometime I have an error with copyCGImageAtTime and the thumbnail is not generated. The error is: Error save image Error Domain=AVFoundationErrorDomain Code=-11800 "The operation could not be completed"(OSStatus error -12792.)", NSLocalizedFailureReason=An unknown error occurred (-12792)}

这是,我已经阅读了一个解决方案,但是如果使用fileURLWithPath:而不是URLWithString:该方法在url的末尾添加一个 - file:// localhost /,使url无效。所以我不知道我能做什么。

Here is the link which I have read a solution but if a use fileURLWithPath: instead of URLWithString: the method add to the end of the url an "-- file://localhost/" that invalidates the url. So I don't know what I can do.

推荐答案

如果你正在使用 MPMoviePlayerController 然后你可以使用这段代码从视频网址生成缩略图。

If you are using MPMoviePlayerController then you can use this code to generate thumbnail from video URL.

NSString *stringUrl = video.stringurl;
NSURL *url = [NSURL URLWithString:stringUrl];
MPMoviePlayerController *player = [[MPMoviePlayerController alloc] initWithContentURL:url];
UIImage *thumbnail = [player thumbnailImageAtTime:1.0 timeOption:MPMovieTimeOptionNearestKeyFrame];

但是,使用此代码,播放器将开始自动播放音频。因此,您必须使用以下代码停止播放器:

But, using this code, player will start autoplaying audio. So, you have to stop the player with this code :

//Player autoplays audio on init
[player stop];






更新:

错误可能是由于使用 URLWithString 。我认为你应该使用 -fileURLWithPath 而不是 URLWithString

The Error is probably due to use of URLWithString. I think you should use -fileURLWithPath instead of URLWithString.

示例代码:

NSString *stringUrl = video.stringurl;
NSURL *vidURL = [NSURL fileURLWithPath:stringUrl];

AVURLAsset *asset = [[AVURLAsset alloc] initWithURL:vidURL options:nil];
AVAssetImageGenerator *generate = [[AVAssetImageGenerator alloc] initWithAsset:asset];

NSError *err = NULL;
CMTime time = CMTimeMake(1, 60);

CGImageRef imgRef = [generate copyCGImageAtTime:time actualTime:NULL error:&err];
UIImage *thumbnail = [UIImage imageWithCGImage:imgRef];

这篇关于从视频网址中提取缩略图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 12:15