Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to determine if a given URL link is a video or image?

I'm trying to take a given URL entered by user and determine if the URL is pointing to a image or a video.

Example use case:

When a user paste in the URL of a YouTube video, on save the page will auto display the embedded YouTube player.

When a user posts URL of a picture in Flickr, on save, the page will auto display a smaller version of the Flickr image.

like image 604
Teo Choong Ping Avatar asked Jan 23 '09 02:01

Teo Choong Ping


People also ask

How do you know if an image is a video or URL?

To check if a url is an image, call the test() method on a regular expression that matches an image extension at the end of a string, e.g. . png or . jpg . The test() method will check if the url ends with an image extension and will return true if it does.

What is a URL link image?

What is an image URL? A URL is a web address that specifies location. Therefore, an image URL is a web address that specifies the location of an image. Having an image URL makes it easy to share. In particular, it simplifies the process because recipients don't have to download it.

How do I find a video URL?

Locate a URL using a browser on a computerIn your browser, open YouTube. Find and click the video whose URL you want to see. The URL of the video will be in the address bar.


2 Answers

You can fetch the URL and see Content-type from the response.

You can use the HTTP Client from apache, it helps you to fetch the content of the URL and you can use it to navigate the redirects. For instance try to fetch the following:

http://www.youtube.com/watch?v=d4LkTstvUL4

Will return an HTML containing the video. After a while you'll find out the video is here:

http://www.youtube.com/v/d4LkTstvUL4

But if you fetch that page you will get a redirect:

HTTP/1.0 302 Redirect
Date: Fri, 23 Jan 2009 02:25:37 GMT
Content-Type: text/plain
Expires: Fri, 23 Jan 2009 02:25:37 GMT
Cache-Control: no-cache
Server: Apache
X-Content-Type-Options: nosniff
Set-Cookie: VISITOR_INFO1_LIVE=sQc75zc-QSU; path=/; domain=.youtube.com; expires=
Set-Cookie: VISITOR_INFO1_LIVE=sQc75zc-QSU; path=/; domain=.youtube.com; expires=
Location: http://www.youtube.com/swf/l.swf?swf=http%3A//s.ytimg.com/yt/swf/cps-vf
L4&rel=1&eurl=&iurl=http%3A//i1.ytimg.com/vi/d4LkTstvUL4/hqdefault.jpg&sk=Z_TM3JF
e_get_video_info=1&load_modules=1

So, what you have to do is to fetch the URL and examine it, until you get final content

This section explains how to handle the redirects.

like image 63
OscarRyz Avatar answered Sep 28 '22 15:09

OscarRyz


Issue an HTTP HEAD request so you can examine the HTTP headers that come back without having to first download the entire document. Showing a non-programmatic case under Linux using "curl":

$ curl --head http://stackoverflow.com/Content/Img/stackoverflow-logo-250.png
HTTP/1.1 200 OK
Cache-Control: max-age=28800
Content-Length: 3428
Content-Type: image/png
Last-Modified: Fri, 16 Jan 2009 09:35:30 GMT
Accept-Ranges: bytes
ETag: "98f590c5bd77c91:0"
Server: Microsoft-IIS/7.0
Date: Fri, 23 Jan 2009 03:55:39 GMT

You can see here from the Content-Type that this is an image. You can use HTTPClient from Apache from Java to do the HTTP Head request.

If you want to download the content for sure, then just issue the HTTP GET (using Httpclient) and use the same HTTP Header to determine the content type.

like image 44
Eddie Avatar answered Sep 28 '22 14:09

Eddie