Check if a URL is a video URL or PHOTO URL

2.5k Views Asked by At

I have a textview in which a user is supposed to enter a url. First I am checking the validity of the URL. Once the validity is confirmed, I need to check if It's a video url or photo url. I have two arrays of all possible video (MP4, MOV etc) and photo (JPG, PNG) file formats and I get an appropriate message if the url contains any element from those two arrays.

Here is a sample instagram video link.. For example: https://www.instagram.com/p/B-PilcbFy2w/

The following Link is a valid video link but it does not contain any video file formats like (MOV, MP4 etc). How can I validate such links?

I know the concept of regular expressions to validate such things but for that I'll have to implement a bunch of IF statements which is not an efficient way. The sample video Link is just an example. I need to validate all kind of video links whether they are from social media platforms or whatever. Help regrading this will really be appreciated.

Here is my sample code..

@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 stackoverflow.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))")
        }


}
2

There are 2 best solutions below

2
Amjad Shubbar On BEST ANSWER

Instagram does not include the file format as you are looking for. Instead, you can use a simple trick to confirm if the link is representing a video or a photo. Thats the Instagram link you provided: https://www.instagram.com/p/B-PilcbFy2w/

When we add ?__a=1 at the end of the link of an Instagram post we will get something like this:

{"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":[]}}}}

you can easily search for the element "is_video", if it is false then the post is a picture, otherwise it is a video. please mark answered if helped.

2
Andreas Oetjen On

Short answer, as you already found out, you cannot use the URL in a reliable way to determine the returned ressource.

Long answer: The "extension" in a URL has no meaning at all. Think of if as a hint to the client, but the web server might do what it wants to do. Take https://www.acme.com for example: there is no file extension at all, and depending on it's configuration, the web server decides what ressource to return. Typically, this will be a HTML document, e.g. "index.html" or something. Even more, https://www.acme.com/index.php has a php extension, but the web server will typically return an HTML document, not a PHP script.

The only way to get the information is to check the mimeType property of the URLResponse.

If you are only interested in the header data (before donwloading all the content), you should check this answer: Get HTTP header fields only on iPhone