【问题标题】:Get downloadable URL(Resource URL) from DailyMotion Videos & other sites?从 DailyMotion 视频和其他网站获取可下载的 URL(资源 URL)?
【发布时间】:2015-05-28 13:10:52
【问题描述】:

我需要从在线视频托管网站(即 YouTube、DailyMotion、vimeo 等)下载视频到我的 iOS 应用程序。

最近我从UIWebView 获取视频 URL,因为用户尝试在其中播放任何视频。并在获取资源url(保存在某个在线路径的视频的url)后尝试下载。

我是在 YouTube 上通过以下方式完成的:

NSString *resourceUrlStr ;
NSString *urlStr  = @"function getURL() { return document.getElementsByTagName('VIDEO')[0].src; } getURL();";

 resourceUrlStr = [myWebViewObject stringByEvaluatingJavaScriptFromString: urlStr];

这东西只适用于 YouTube 视频,其中 resourceUrlStr 为我提供了视频资源的 url。

但是当我为 DailyMotion 和其他视频托管网站尝试这些东西时,它不会返回我的资源视频 url。

有什么猜测吗?还是想法?

【问题讨论】:

    标签: ios objective-c video download uiwebview


    【解决方案1】:

    有几种技术可以执行它。

    我最喜欢的技术是, 当 WebView 中的 Player 开始播放视频时,您需要设置以下代码进行通知:

    -(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)
    
    */
    }
    

    【讨论】:

    • 谢谢,对我有帮助:)
    【解决方案2】:

    #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

    【讨论】:

    • 通知名称在 Swift 3.0 中应该是 Notification.Name。作为旁注 AVPlayerItemBecameCurrentNotification 不会在 WKWebView 中触发。对于 UIWebView 这一切都很好。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-22
    • 2014-02-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-21
    • 1970-01-01
    相关资源
    最近更新 更多