我正在使用 AVPlayer 从服务器播放视频。
AVPlayer *player = [AVPlayer playerWithURL:videoURL];
AVPlayerViewController *playerViewController = [AVPlayerViewController new];
playerViewController.player = player;
[self presentViewController:playerViewController animated:YES completion:nil];
现在我想捕捉多个图像,而视频正在播放并保存。 我怎么能这样做??
答案 0 :(得分:0)
使用MPMoviePlayerController
存在从视频中获取帧的更简单方法。以下代码在time:5
MPMoviePlayerController *movie = [[MPMoviePlayerController alloc]
initWithContentURL:[NSURL URLWithString:@"someVideo.mp4"]];
UIImage *singleFrameImage = [movie thumbnailImageAtTime:5
timeOption:MPMovieTimeOptionExact];
为了从视频中获取多个图像,可以使用以下实现。
1)注册通知,以便在处理每个请求时接收图像。
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(MPMoviePlayerThumbnailImageRequestDidFinishNotification:) name:MPMoviePlayerThumbnailImageRequestDidFinishNotification object:moviePlayer];
2)使用所需的时间初始化MPMoviePlayerController
以获取图像。
MPMoviePlayerController *movie = [[MPMoviePlayerController alloc]
initWithContentURL [NSURL URLWithString:@"someVideo.mp4"]];
NSNumber time1 = 15;
NSNumber time2 = 16;
NSNumber time3 = 17;
NSArray *times = [NSArray arrayWithObjects:time1,time2,time3,nil];
[movie requestThumbnailImagesAtTimes:times timeOption:MPMovieTimeOptionExact];
3)选择器接收图像
-(void)MPMoviePlayerThumbnailImageRequestDidFinishNotification: (NSNotification*)note
{
NSDictionary * userInfo = [note userInfo];
UIImage *image = (UIImage *)[userInfo objectForKey:MPMoviePlayerThumbnailImageKey];
}
答案 1 :(得分:0)
请使用此代码,您可以使用此
从视频中获取缩略图AVAsset *asset = [AVAsset assetWithURL:URl];
AVAssetImageGenerator *imageGenerator = [[AVAssetImageGenerator alloc]initWithAsset:asset];
imageGenerator.appliesPreferredTrackTransform=TRUE;
CMTime time = CMTimeMake(1, 1);
CGImageRef imageRef = [imageGenerator copyCGImageAtTime:time actualTime:NULL error:NULL];
UIImage *thumbnail = [UIImage imageWithCGImage:imageRef];
NSData *imagedata = [self compressImage:thumbnail];
UIImage *imag = [UIImage imageWithData:imagedata];//this is image from video at time interval 1 second
您可以获取多个图像来设置多个时间间隔