【问题标题】:How to play instagram video using AVPlayer如何使用 AVPlayer 播放 Instagram 视频
【发布时间】:2017-01-04 04:53:12
【问题描述】:

我正在尝试从共享链接播放 Instagram 视频,我使用了以下代码,但它没有流式传输,是否需要任何额外的步骤?任何可以获取源视频 URL(如 Facebook 上的图形 API)的 API?

网址是这样的https://instagram.com/p/BOzamYMA-vb/

let videoURL = NSURL(string: "https://instagram.com/p/BOzamYMA-vb/")
    let player = AVPlayer(url: videoURL! as URL)
    let playerViewController = AVPlayerViewController()
    playerViewController.player = player
    self.present(playerViewController, animated: true) {
        playerViewController.player!.play()
    }

这会打开播放器但不播放视频 任何帮助表示赞赏。

【问题讨论】:

  • 以上网址不是视频网址,那个是网页网址,尝试从网页数据中解析视频网址
  • 是的,这正是 instagram 作为链接返回的内容

标签: ios swift xcode instagram instagram-api


【解决方案1】:

您正在向 AVPlayer 提供网页 URL,这不起作用,您必须使用视频本身的 URL。

使用Fuzi HTML 解析库,您可以从网页中获取视频 URL。

诀窍是在 HTML 中找到 URL。

对于 instagram,我们可以通过这个 xpath 找到它:"//meta[@property='og:video']/@content"

例子:

import Fuzi

let link = "https://instagram.com/p/BOzamYMA-vb/"
if let pageURL = URL(string: link),
    let data = try? Data(contentsOf: pageURL),
    let doc = try? HTMLDocument(data: data)
{
    let items = doc.xpath("//meta[@property='og:video']/@content")
    if let item = items.first,
        let url = URL(string: item.stringValue)
    {
        print(url)
    }
}

这会从页面“http://scontent-cdg2-1.cdninstagram.com/t50.2886-16/15872432_382094838793088_926533688240373760_n.mp4”获取 URL,您可以像往常一样使用它来下载或流式传输视频。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-04-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-05
    • 1970-01-01
    • 2018-04-13
    相关资源
    最近更新 更多