我需要从在线视频托管网站(即YouTube,DailyMotion,vimeo等)下载视频到我的iOS应用程序。
最近,当用户尝试播放其中的任何视频时,我会从UIWebView
获取视频网址。获得资源网址(保存在某个在线路径的视频网址)并尝试下载后。
我是通过以下内容在YouTube上完成的:
NSString *resourceUrlStr ;
NSString *urlStr = @"function getURL() { return document.getElementsByTagName('VIDEO')[0].src; } getURL();";
resourceUrlStr = [myWebViewObject stringByEvaluatingJavaScriptFromString: urlStr];
此内容仅适用于YouTube视频,其中resourceUrlStr为我提供了视频资源的网址。
但是当我为DailyMotion&其他视频托管网站,它不会返回我的资源视频网址。
任何猜测?还是想法?
答案 0 :(得分:0)
有几种技术可以执行它。
我喜欢的技巧是, 当WebView中的播放器开始播放视频时您需要在下面设置通知代码:
-(void)viewWillAppear:(BOOL)animated{
[super viewWillAppear:YES];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(myVideoPlayedInFullscreenFirstTime:)
name:@"AVPlayerItemBecameCurrentNotification"
object:nil];
}
-(void)myVideoPlayedInFullscreenFirstTime:(NSNotification*)aNotification {
AVPlayerItem *playerItem = [aNotification object];
if(playerItem == nil) return;
// Break down the AVPlayerItem to get to the path
AVURLAsset *asset = (AVURLAsset*)[playerItem asset];
NSURL *downloadebaleVideoUrl = [asset URL];
/* now, You can download video by above URL.
In some cases it will give you “.m3u” file location.
You can go through it & get All videos from that list.
For DailyMotion there is one library on GitHub:
[For DailyMotion](https://github.com/oikyn/ExtractVideoURLManager)
*/
}
答案 1 :(得分:0)
#KunalParekh建议的Swift版解决方案是:
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(true)
NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(self.myVideoPlayedInFullscreenFirstTime), name: "AVPlayerItemBecameCurrentNotification", object: nil)
}
func myVideoPlayedInFullscreenFirstTime(aNotification: NSNotification) {
var playerItem = aNotification.object
if playerItem == nil {
return
}
let asset:AVURLAsset = playerItem?.asset as! AVURLAsset
var downloadebaleVideoUrl = asset.URL
print("downloadable video url is: \(downloadebaleVideoUrl)")
}
请记得导入AVFoundation