Get video URL from AVPlayer in UIWebView

2019-06-16 15:19发布

I am trying to detect URL of current video playing from UIWebView, with this method :

func webView(_ webView: UIWebView, shouldStartLoadWith request: URLRequest, navigationType: UIWebViewNavigationType) -> Bool
    {

        NotificationCenter.default.addObserver(self, selector: #selector(playerItemBecameCurrent(notification:)), name: NSNotification.Name("AVPlayerItemBecameCurrentNotification"), object: nil)

        //urlTextField.text = webView.request?.url?.absoluteString

        return true
    }



  func playerItemBecameCurrent(notification:Notification)  {

        let playerItem: AVPlayerItem? = notification.object as? AVPlayerItem
        if playerItem == nil {
            return
        }
        // Break down the AVPlayerItem to get to the path
        let asset: AVURLAsset? = (playerItem?.asset as? AVURLAsset)
        let url: URL? = asset?.url
        let path: String? = url?.absoluteString
        print(path!)

    }

This code works but the video URL doesn't contain any video file extension: for example here is YouTube and Vimeo video ULRs :

https://r5---sn-ab5l6nzr.googlevideo.com/videoplayback?mime=video%2Fmp4&requiressl=yes&clen=27257208&mn=sn-ab5l6nzr&mm=31&mv=m&mt=1499534990&key=yt6&ms=au&source=youtube&ip=107.182.226.163&sparams=clen%2Cdur%2Cei%2Cgir%2Cid%2Cinitcwndbps%2Cip%2Cipbits%2Citag%2Clmt%2Cmime%2Cmm%2Cmn%2Cms%2Cmv%2Cpl%2Cratebypass%2Crequiressl%2Csource%2Cexpire&initcwndbps=3565000&id=o-ACG22m-TtwyC8tSG_AHUJk3qOPvhUm1X_-qRjy07pjIx&ei=-hZhWZXcJ_aV8gS2tbCQDg&lmt=1497638262979584&ratebypass=yes&gir=yes&pl=25&expire=1499556698&dur=316.093&signature=94C8ED3E4AF64A7AC9F1B7E226454823B236CA55.C18F5F5D9D314B73EDD01DFC4EA3CEA1621AC6C7&ipbits=0&itag=18&cpn=Wb3RPNfJRcFx5P-x&c=MWEB&cver=1.20170706&ptk=TiTV2010%2Buser&oid=1nI2Jk44kRu4HLE6kGDUrA&ptchn=E2VhHmwp3Y_wZvgz_LJvfg&pltype=content

and Vimeo :

https://36skyfiregce-vimeo.akamaized.net/exp=1499539063~acl=%2F222646835%2F%2A~hmac=dc3caeb64501f537e2795ee8e121aed75a7eed9ba4e1b921fe92c757fe06b314/222646835/video/778109097,778109103,778109102,778109101/master.m3u8?f=dash 

2条回答
【Aperson】
2楼-- · 2019-06-16 15:45

You can get MIME type and raw data like this:

func playerItemBecameCurrent(notification:Notification)  {
        guard let playerItem = notification.object as? AVPlayerItem,
            let asset = playerItem.asset as? AVURLAsset else {return}
        let url = asset.url
        var response: URLResponse? = nil
        var request = URLRequest(url: url)
        request.httpMethod = "HEAD"
        try? NSURLConnection.sendSynchronousRequest(request, returning: &response)
        print(response?.mimeType)
        //not the best way to do it
        //let data = try? Data(contentsOf: asset.url)
    }

Then you can choose appropriate extension that matches MIME type. You can get all supported types by calling AVURLAsset.audiovisualMIMETypes()

查看更多
Evening l夕情丶
3楼-- · 2019-06-16 15:57

Firstly i want to tell you you're coding is working fine.

Mirror Server

It is because during the uploading, downloading and streaming process google is using a mirror server with domain name googlevideo.com.

You accessing the link video while in streaming state. You will get that mirror link only. Youtube videos resided inside this mirror server.

Same like that Vimeo is using mirror server akamaized.net

Refer the SO POST For clarification

Decoding The Video Link From Mirror URL

So in this condition what we need to have is unique id of video in youtube and same can be accessed by appending the url https://www.youtube.com/watch?v=id_here

For decoding the unique id from this mirror url is impossible as by SO Answer

They have added this mirror server concept, is for having some security.

As per youtube T&C.. section 5B here -- http://www.youtube.com/static?template=terms

Downloading YouTube content is not in compliance with the YouTube Tearms of Service.

There are some private API like PSYouTubeExtractor.

I think you need to have URL of video for downloading purpose.

查看更多
登录 后发表回答