检查 URL 是视频 URL 还是照片 URL

Check if a URL is a video URL or PHOTO URL

我有一个文本视图,用户应该在其中输入 url。首先,我要检查 URL 的有效性。确认有效性后,我需要检查它是视频 url 还是照片 url。我有两个数组,包含所有可能的视频(MP4、MOV 等)和照片(JPG、PNG)文件格式,如果 url 包含这两个数组中的任何元素,我会收到一条适当的消息。

这是一个 Instagram 视频示例 link.. 例如:https://www.instagram.com/p/B-PilcbFy2w/

以下 Link 是有效视频 link 但它不包含任何视频文件格式,例如(MOV、MP4 等)。我怎样才能验证这样的 links?

我知道正则表达式的概念来验证这些事情,但为此我将不得不实现一堆 IF 语句,这不是一种有效的方法。示例视频 Link 只是一个例子。我需要验证所有类型的视频 link,无论它们来自社交媒体平台还是其他平台。帮助重新分级这将非常感激。

这是我的示例代码..

@IBOutlet weak var textVW: UITextView!

@IBAction func validateButton(_ sender: UIButton) {


    let userInputURL = URL(string: textVW.text)

    if userInputURL != nil && userInputURL?.scheme != nil && userInputURL?.host != nil {
        //  - a scheme (like http://)
        //  - a host (like whosebug.com)

        print("Valid URL..")

        // Now checking url type, if its a video or image url..
        checkURLType(inputURL: userInputURL!)

    }

    else {
        print("Invalid URL..")
    }
}

func checkURLType(inputURL : URL) {

    // Most commom image types..
    let imageExtensions = ["png", "jpg", "gif", "tif"]

    // Most cmmon video types..
    let videoExtensions = ["WEBM", "MPG", "MPEG", "MPE", "MP4", "M4P", "M4V", "AVI", "WMV", "MOV"]

    let url: URL? = NSURL(fileURLWithPath: inputURL.path) as URL
    let pathExtention = url?.pathExtension

    if imageExtensions.contains(pathExtention!)
        {
            print("Image URL: \(String(describing: url))")

        }

    else if videoExtensions.contains(pathExtention!)
        {
             print("Video URL: \(String(describing: url))")
        }

        else
        {
             print("Does Not Exist: \(String(describing: url))")
        }


}

简短的回答,正如您已经发现的那样,您不能以可靠的方式使用 URL 来确定 returned ressource

长答案:URL 中的 "extension" 根本没有任何意义。将 if 视为对客户端的提示,但 Web 服务器可能会做它想做的事情。 以 https://www.acme.com 为例:根本没有文件扩展名,并且根据其配置,网络服务器决定向 return 提供什么资源。通常,这将是一个 HTML 文档,例如"index.html" 之类的。 更重要的是,https://www.acme.com/index.php 有一个 php 扩展名,但是网络服务器通常 return 一个 HTML 文档,而不是 PHP 脚本.

获取信息的唯一方法是检查 URLResponsemimeType 属性。

如果您只对 header 数据感兴趣(在下载所有内容之前),您应该检查这个答案:Get HTTP header fields only on iPhone

Instagram 不包含您正在查找的文件格式。 相反,您可以使用一个简单的技巧来确认 link 是否代表 一段视频或一张照片。 那就是你提供的 Instagram link: https://www.instagram.com/p/B-PilcbFy2w/

当我们在 Instagram post 的 link 末尾添加 ?__a=1 时,我们将得到如下内容:

{"graphql":{"shortcode_media":{"__typename":"GraphImage",
"id":"2275681914071665931",
"shortcode":"B-U2KTOBc0L",
"dimensions":{"height":1080,
"width":1080},
"gating_info":null,
"fact_check_overall_rating":null,
"fact_check_information":null,
"sensitivity_friction_info":null,
"media_preview":"ACoq2XcL+PbvQ24KW6kDIHr7VDbKW/eN15C+w9fx/lUm/PIb/wAdNIb0GfaApxtYj1Cn/CnQzGXqrLgZ5GPwpd5/vf8AjpoLn+9/46aYiaimIc98/gR/On0ARxD92o6fKP5U3Jx/H+lMtX3QqRyQMEdORxT9vsf++v8A69A2Lk/7f5Cjn/b/ACH6UbT6H/vr/wCvRtPof++qBDkyeTu/4Fj+lPqNF29AR9Tnp0qSgDMg/djcw+R+cdSD/eq4EDjICFT0PNSOMg1nQsVn2g4BA4HT8qnbQrfUv7D3C/r/AIUeX/srn8f8KmoqiSJUA6gA9sVJS0UAf//Z",
"display_url":"https://instagram.fbah6-1.fna.fbcdn.net/v/t51.2885-15/e35/91790764_531125317547482_7904098713138015435_n.jpg?_nc_ht=instagram.fbah6-1.fna.fbcdn.net&_nc_cat=110&_nc_ohc=X8nt1F5JsVAAX_cmJQe&oh=a608b3496a9bef59e9d372ce339e496f&oe=5EB63B42",
"display_resources":[{"src":"https://instagram.fbah6-1.fna.fbcdn.net/v/t51.2885-15/sh0.08/e35/s640x640/91790764_531125317547482_7904098713138015435_n.jpg?_nc_ht=instagram.fbah6-1.fna.fbcdn.net&_nc_cat=110&_nc_ohc=X8nt1F5JsVAAX_cmJQe&oh=04905a5d307b7d70d9912630b68bced5&oe=5EB648A5",
"config_width":640,
"config_height":640},
{"src":"https://instagram.fbah6-1.fna.fbcdn.net/v/t51.2885-15/sh0.08/e35/s750x750/91790764_531125317547482_7904098713138015435_n.jpg?_nc_ht=instagram.fbah6-1.fna.fbcdn.net&_nc_cat=110&_nc_ohc=X8nt1F5JsVAAX_cmJQe&oh=e5efae91c8fef2f14c4abbf5bf50a709&oe=5EB6B025",
"config_width":750,
"config_height":750},
{"src":"https://instagram.fbah6-1.fna.fbcdn.net/v/t51.2885-15/e35/91790764_531125317547482_7904098713138015435_n.jpg?_nc_ht=instagram.fbah6-1.fna.fbcdn.net&_nc_cat=110&_nc_ohc=X8nt1F5JsVAAX_cmJQe&oh=a608b3496a9bef59e9d372ce339e496f&oe=5EB63B42",
"config_width":1080,
"config_height":1080}],
"accessibility_caption":"Photo by \ud83c\udde7\ud83c\udded Dallaspresso on March 29,
 2020. Image may contain: text",
"is_video":false,
"tracking_token":"eyJ2ZXJzaW9uIjo1LCJwYXlsb2FkIjp7ImlzX2FuYWx5dGljc190cmFja2VkIjp0cnVlLCJ1dWlkIjoiMmRmOGE3M2Q1OTljNDA0YWI1OTBiYzEwMjRjMDEzNDIyMjc1NjgxOTE0MDcxNjY1OTMxIn0sInNpZ25hdHVyZSI6IiJ9",
"edge_media_to_tagged_user":{"edges":[]},
"edge_media_to_caption":{"edges":[{"node":{"text":"\u062f\u0644\u0627\u0633\u0628\u0631\u064a\u0633\u0648.. \u062f\u0644\u0644 \u062d\u0648\u0627\u0633\u0643\n.\n\n#Dallaspresso\n#nespresso #photography #Bahrain #\u0627\u0644\u0628\u062d\u0631\u064a\u0646 #\u0642\u0647\u0648\u0629 #\u0627\u0644\u0633\u0639\u0648\u062f\u064a\u0629 #\u0627\u0644\u0643\u0648\u064a\u062a #\u0627\u0644\u0639\u0631\u0627\u0642  #\u0645\u0635\u0631 #\u0639\u0645\u0627\u0646 #\u062f\u0644\u0627\u0633\u0628\u0631\u064a\u0633\u0648 #\u0639\u0645\u0627\u0646 #\u0645\u0646\u0634\u0646 #\u0646\u0633\u0628\u0631\u064a\u0633\u0648 #\u0627\u0644\u062d\u0628 #\u0643\u0628\u0633\u0648\u0644\u0627\u062a_\u0642\u0647\u0648\u0647 #\u0643\u0628\u0633\u0648\u0644\u0627\u062a_\u0642\u0647\u0648\u0629"}}]},
"caption_is_edited":false,
"has_ranked_comments":false,
"edge_media_to_parent_comment":{"count":2,
"page_info":{"has_next_page":false,
"end_cursor":null},
"edges":[{"node":{"id":"17873033503626679",
"text":"\u0627\u0644\u0635\u0631\u0627\u062d\u0647 \u0645\u0646\u062a\u062c\u0627\u062a \u062e\u064a\u0627\u0644\u064a\u0647 \u0627\u0646\u0627 \u0632\u0628\u0648\u0646 \u062f\u0627\u0645 \u0648\u064a\u0627\u0627\u0627\u0643\u0645 \u0645\u0646 \u0627\u0648\u0644 \u0645\u0627\u0646\u0632\u0644\u062a\u0648\u0646 \u0627\u0644\u0645\u0646\u062a\u062c\u0627\u062a \u0643\u0644 \u0645\u0646\u062a\u062c \u0627\u0644\u064a\u0647 \u0645\u0630\u0627\u0642 \u0627\u0631\u0648\u0639 \u0645\u0646 \u0627\u0644\u062b\u0627\u0646\u064a \u0648\u0646\u062a\u0638\u0631 \u0645\u0646\u0643\u0645 \u0627\u0644\u0645\u0632\u064a\u062f\u2764\ufe0f",
"created_at":1585548401,
"did_report_as_spam":false,
"owner":{"id":"6983355053",
"is_verified":false,
"profile_pic_url":"https://instagram.fbah6-1.fna.fbcdn.net/v/t51.2885-19/s150x150/62439219_378925886148443_2705326417154932736_n.jpg?_nc_ht=instagram.fbah6-1.fna.fbcdn.net&_nc_ohc=_b_clBY3nsgAX9hxHqp&oh=69c82b500e15d3e3f926e0057bbb1a93&oe=5EB84221",
"username":"hassanjasaim"},
"viewer_has_liked":false,
"edge_liked_by":{"count":1},
"is_restricted_pending":false,
"edge_threaded_comments":{"count":0,
"page_info":{"has_next_page":false,
"end_cursor":null},
"edges":[]}}},
{"node":{"id":"18103267534188420",
"text":"\u0627\u0646\u0627 \u0627\u0648\u0644 \u0645\u0631\u0629 \u0627\u062a\u0639\u0627\u0645\u0644 \u0645\u0639\u0627\u0643\u0645.. \u0627\u0644\u0635\u0631\u0627\u062d\u0629 \u0641\u0627\u062c\u0626\u062a\u0648\u0646\u064a \u0628\u0627\u0644\u0633\u0631\u0639\u0629 \u0648\u062e\u062f\u0645\u062a\u0643\u0645 \u0627\u0644\u0631\u0627\u0626\u0639\u0629.. \u062e\u0644\u0627\u0644 \u0623\u0642\u0644 \u0645\u0646 \u0661\u0662 \u0633\u0627\u0639\u0629 \u0648\u0635\u0644 \u0644\u064a \u0627\u0644\u0637\u0644\u0628.. \u0648\u0627\u0646 \u0634\u0627\u0621 \u0627\u0644\u0644\u0647 \u0628\u064a\u0643\u0648\u0646 \u062a\u0639\u0627\u0645\u0644\u0646\u0627 \u062f\u0627\u0626\u0645 \u0645\u0639\u0627\u0643\u0645.. \u0634\u0627\u0643\u0631\u064a\u0646 \u0644\u0643\u0645 \u0627\u0647\u062a\u0645\u0627\u0645\u0643\u0645 \u0628\u0631\u0636\u0627 \u0627\u0644\u0632\u0628\u0627\u0626\u0646.. \u0627\u0644\u0644\u0647 \u064a\u0648\u0641\u0642\u0643\u0645\ud83d\udc96",
"created_at":1585658629,
"did_report_as_spam":false,
"owner":{"id":"266197895",
"is_verified":false,
"profile_pic_url":"https://instagram.fbah6-1.fna.fbcdn.net/v/t51.2885-19/10727387_322891407903875_1650333521_a.jpg?_nc_ht=instagram.fbah6-1.fna.fbcdn.net&_nc_ohc=-r1CI1ZAVwgAX9XRWAW&oh=a58ed12c2f770c9f790bb77d6ff3038d&oe=5EB9954D",
"username":"amooor440"},
"viewer_has_liked":false,
"edge_liked_by":{"count":0},
"is_restricted_pending":false,
"edge_threaded_comments":{"count":0,
"page_info":{"has_next_page":false,
"end_cursor":null},
"edges":[]}}}]},
"edge_media_to_hoisted_comment":{"edges":[]},
"edge_media_preview_comment":{"count":2,
"edges":[{"node":{"id":"17873033503626679",
"text":"\u0627\u0644\u0635\u0631\u0627\u062d\u0647 \u0645\u0646\u062a\u062c\u0627\u062a \u062e\u064a\u0627\u0644\u064a\u0647 \u0627\u0646\u0627 \u0632\u0628\u0648\u0646 \u062f\u0627\u0645 \u0648\u064a\u0627\u0627\u0627\u0643\u0645 \u0645\u0646 \u0627\u0648\u0644 \u0645\u0627\u0646\u0632\u0644\u062a\u0648\u0646 \u0627\u0644\u0645\u0646\u062a\u062c\u0627\u062a \u0643\u0644 \u0645\u0646\u062a\u062c \u0627\u0644\u064a\u0647 \u0645\u0630\u0627\u0642 \u0627\u0631\u0648\u0639 \u0645\u0646 \u0627\u0644\u062b\u0627\u0646\u064a \u0648\u0646\u062a\u0638\u0631 \u0645\u0646\u0643\u0645 \u0627\u0644\u0645\u0632\u064a\u062f\u2764\ufe0f",
"created_at":1585548401,
"did_report_as_spam":false,
"owner":{"id":"6983355053",
"is_verified":false,
"profile_pic_url":"https://instagram.fbah6-1.fna.fbcdn.net/v/t51.2885-19/s150x150/62439219_378925886148443_2705326417154932736_n.jpg?_nc_ht=instagram.fbah6-1.fna.fbcdn.net&_nc_ohc=_b_clBY3nsgAX9hxHqp&oh=69c82b500e15d3e3f926e0057bbb1a93&oe=5EB84221",
"username":"hassanjasaim"},
"viewer_has_liked":false,
"edge_liked_by":{"count":1},
"is_restricted_pending":false}},
{"node":{"id":"18103267534188420",
"text":"\u0627\u0646\u0627 \u0627\u0648\u0644 \u0645\u0631\u0629 \u0627\u062a\u0639\u0627\u0645\u0644 \u0645\u0639\u0627\u0643\u0645.. \u0627\u0644\u0635\u0631\u0627\u062d\u0629 \u0641\u0627\u062c\u0626\u062a\u0648\u0646\u064a \u0628\u0627\u0644\u0633\u0631\u0639\u0629 \u0648\u062e\u062f\u0645\u062a\u0643\u0645 \u0627\u0644\u0631\u0627\u0626\u0639\u0629.. \u062e\u0644\u0627\u0644 \u0623\u0642\u0644 \u0645\u0646 \u0661\u0662 \u0633\u0627\u0639\u0629 \u0648\u0635\u0644 \u0644\u064a \u0627\u0644\u0637\u0644\u0628.. \u0648\u0627\u0646 \u0634\u0627\u0621 \u0627\u0644\u0644\u0647 \u0628\u064a\u0643\u0648\u0646 \u062a\u0639\u0627\u0645\u0644\u0646\u0627 \u062f\u0627\u0626\u0645 \u0645\u0639\u0627\u0643\u0645.. \u0634\u0627\u0643\u0631\u064a\u0646 \u0644\u0643\u0645 \u0627\u0647\u062a\u0645\u0627\u0645\u0643\u0645 \u0628\u0631\u0636\u0627 \u0627\u0644\u0632\u0628\u0627\u0626\u0646.. \u0627\u0644\u0644\u0647 \u064a\u0648\u0641\u0642\u0643\u0645\ud83d\udc96",
"created_at":1585658629,
"did_report_as_spam":false,
"owner":{"id":"266197895",
"is_verified":false,
"profile_pic_url":"https://instagram.fbah6-1.fna.fbcdn.net/v/t51.2885-19/10727387_322891407903875_1650333521_a.jpg?_nc_ht=instagram.fbah6-1.fna.fbcdn.net&_nc_ohc=-r1CI1ZAVwgAX9XRWAW&oh=a58ed12c2f770c9f790bb77d6ff3038d&oe=5EB9954D",
"username":"amooor440"},
"viewer_has_liked":false,
"edge_liked_by":{"count":0},
"is_restricted_pending":false}}]},
"comments_disabled":false,
"commenting_disabled_for_viewer":false,
"taken_at_timestamp":1585502445,
"edge_media_preview_like":{"count":27,
"edges":[]},
"edge_media_to_sponsor_user":{"edges":[]},
"location":null,
"viewer_has_liked":false,
"viewer_has_saved":false,
"viewer_has_saved_to_collection":false,
"viewer_in_photo_of_you":false,
"viewer_can_reshare":true,
"owner":{"id":"8634941354",
"is_verified":false,
"profile_pic_url":"https://instagram.fbah6-1.fna.fbcdn.net/v/t51.2885-19/s150x150/50091870_301664920553287_8948101179057897472_n.jpg?_nc_ht=instagram.fbah6-1.fna.fbcdn.net&_nc_ohc=gOUeaMbAyA8AX8wKTVK&oh=f2b7e5c953d8b19fa5aaf787019ecc56&oe=5EB97A7A",
"username":"dallaspressobh",
"blocked_by_viewer":false,
"restricted_by_viewer":null,
"followed_by_viewer":false,
"full_name":"\ud83c\udde7\ud83c\udded Dallaspresso",
"has_blocked_viewer":false,
"is_private":false,
"is_unpublished":false,
"requested_by_viewer":false,
"edge_owner_to_timeline_media":{"count":155}},
"is_ad":false,
"edge_web_media_to_related_media":{"edges":[]},
"edge_related_profiles":{"edges":[]}}}}

您可以轻松搜索元素"is_video",如果为假则post是图片,否则是视频。 如果有帮助,请标记为已回答。