从Youtube YTPlayerView获取当前帧 [英] Get a current frame from Youtube YTPlayerView

查看:116
本文介绍了从Youtube YTPlayerView获取当前帧的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人知道在iOS上制作youtube视频的屏幕截图的可行解决方案吗?标准的"UIView to UIImage"解决方案(如对当前图形上下文或视图层进行快照)不起作用,因为我只得到一个黑色的,空的视频播放器,上面带有Youtube徽标,而没有实际的视频.

Does anyone know a working solution to make a screenshot of a youtube video on iOS ? A standard "UIView to UIImage" solutions like snapshotting current graphic context or a layer of the view don't work as I get only a black, empty video player with Youtube logo on it, without the actual video.

我尝试对视图的presentationLayer进行快照,但是随后出现BAD_ACCESS错误.我听说它与UIWebView(用于显示youtube播放器)中的CSS/HTML5有关.

I tried to snapshot the view's presentationLayer but then I get the BAD_ACCESS error. I heard it has something to do with CSS/HTML5 inside the UIWebView (which is used to display the youtube player).

有人成功吗?

推荐答案

Bartek Uchanski,再次感谢您,您的回答向我指明了正确的方向.

Bartek Uchanski, thank you again, your response pointed me in the right direction.

对于我的特定应用程序,我仍然需要依靠Google的YouTube iOS Player帮助器API进行视频播放,但是要提取缩略图(似乎直接用Google YouTube API无法实现),我加载了单独的阴影" mp4流从YouTube,同时依赖XCDYouTubeClient库和AVAsset/AVFoundation框架提取并保存缩略图. (不幸的是[player thumbnailImageAtTime:]已过时,因此我的解决方法如下.)

For my particular application I still needed to rely on Google's YouTube iOS Player Helper API for video playback, however to extract a thumbnail (which seems to be impossible directly with the Google YouTube API) I loaded a separate "shadow" mp4 stream from YouTube, relying on both the XCDYouTubeClient library and AVAsset/AVFoundation framework to extract and save a thumbnail image. (Unfortunately [player thumbnailImageAtTime:] is deprecated hence my workaround below.)

1)使用XCDYouTubeClient API提取例如来自YouTube 11个字符的视频标识符的mp4视频流URL;您还可以解析YouTube视频的HTML页面以获取相同的流URL,但是我发现使用XCDYouTubeClient API更加容易.

1) Use the XCDYouTubeClient API to extract an e.g. mp4 video stream URL from a YouTube 11 character video identifier; you could also parse the YouTube video's HTML page for the same stream URL(s) but I found it (much) easier to simply use the XCDYouTubeClient API.

2)检索到视频流的URL后,请使用AVAsset/AVFoundation框架提取缩略图.

2) Once you've retrieved the URL to the video stream then use the AVAsset/AVFoundation framework to extract a thumbnail.

(我在这里简化了我的实际应用程序代码,但一般概念如下).

(I've shorthanded my actual app code here, but the general concept is as follows.)

NSString *videoid = @"9bZkp7q19f0"; // youtube 11 character video identifier
[[XCDYouTubeClient defaultClient] getVideoWithIdentifier:videoid completionHandler:^(XCDYouTubeVideo *video, NSError *error) {
  if (video) {
    // available youtube urls; e.g. itag=160 mp4 video only, 256x144
    if (video.streamURLs[[NSNumber numberWithInt:160]]) {
      // grab thumbnail from specified video stream @ specified time, e.g. @ 30 secs
      AVAsset *asset = [AVAsset assetWithURL:[NSURL URLWithString:[[video.streamURLs[[NSNumber numberWithInt:160]] description] stringByRemovingPercentEncoding]]];
      AVAssetImageGenerator *imageGenerator = [[AVAssetImageGenerator alloc] initWithAsset:asset];
      CGImageRef imageRef = [imageGenerator copyCGImageAtTime:CMTimeMakeWithSeconds(30.0,NSEC_PER_SEC) actualTime:NULL error:NULL];
      UIImage *ytvideo_thumbnail = [UIImage imageWithCGImage:imageRef];
      CGImageRelease(imageRef);
      // save thumbnail to camera roll
      UIImageWriteToSavedPhotosAlbum(ytvideo_thumbnail,nil,nil,nil);
    }
  }
}];

这篇关于从Youtube YTPlayerView获取当前帧的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆