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

I am trying to accept the given URL entered by the user and determine if the URL is pointing to an image or video.

Usage example:

When the user inserts the URL of the YouTube video, the built-in YouTube player is automatically displayed when the page is saved.

When a user submits the image URL to Flickr, the page automatically displays a smaller version of the Flickr image when it is saved.

+5
source share
6 answers

You can get the url and see the type of content from the response.

HTTP Client apache, URL-, , , :

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

HTML, . , :

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

, :

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

, , URL- ,

, .

+9

HTTP HEAD, HTTP, . Linux "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

Content-Type, . HTTPClient Apache Java, HTTP Head.

, HTTP GET ( Httpclient) HTTP-, .

+6

? HTML-, ?

+3

apache.

HttpURLConnection urlConnection;
String urlString = "http://www.youtube.com/v/oHg5SJYRHA0";
try {
    urlConnection = (HttpURLConnection) new URL(urlString).openConnection();
    urlConnection.setInstanceFollowRedirects(true);
    HttpURLConnection.setFollowRedirects(true);

    int status = urlConnection.getResponseCode();
    if (status >= 300 && status <= 307) {
        urlString = urlConnection.getHeaderField("Location");
        urlConnection = (HttpURLConnection) new URL(urlString).openConnection();
        System.out.println("Redirect to URL : " + urlString);
    }
    String contentType = urlConnection.getHeaderField("Content-Type");
    if (contentType.startsWith("image/")) {
        //do something with an image
    } else if (contentType.equals("application/x-shockwave-flash")) {
        //do something with a video
        //} else ...
    }
    System.out.println(contentType);
} catch (IOException e) {
    e.printStackTrace();
}

mkyong.com

+1

curl , .

curl -s -v -r0-499 -o test  http://stackoverflow.com/content/img/so/logo.png
* About to connect() to stackoverflow.com port 80 (#0)
*   Trying 69.59.196.211... connected
* Connected to stackoverflow.com (69.59.196.211) port 80 (#0)
> GET /content/img/so/logo.png HTTP/1.1
> Range: bytes=0-499
> User-Agent: curl/7.19.4 (i386-apple-darwin9.6.0) libcurl/7.19.4 zlib/1.2.3
> Host: stackoverflow.com
> Accept: */*
> 
< HTTP/1.1 206 Partial Content
< Cache-Control: max-age=604800
< Content-Type: image/png
< Content-Range: bytes 0-499/3438
< Last-Modified: Fri, 05 Jun 2009 06:52:35 GMT
< Accept-Ranges: bytes
< ETag: "25dd4b35aae5c91:0"
< Server: Microsoft-IIS/7.0
< Date: Fri, 19 Jun 2009 19:39:43 GMT
< Content-Length: 500
< 
{ [data not shown]
* Connection #0 to host stackoverflow.com left intact
* Closing connection #0

:

$ file test
test: PNG image data, 250 x 61, 8-bit colormap, non-interlaced

mime: image/png, 3438 , - 250 x 61 PNG .

0

Fast video indexer is a video capture software that can automatically capture video clips from a list of videos and create index web pages, index images or a list of images.

0
source

All Articles