MMTManoury
MMTManoury

Reputation: 21

How can I check an URL is a video URL

I want to create a project that can see online videos without downloading with ExoPlayer.

ExoPlayer supports many formats like these I want to supoort all these formats(Progressive container formats). I searched and I couldn't find anything

I want to check whether the user Entered URL is a supported URL or not.Any help would be appreciate .

I'm using ExoPlayer.You can use ExoPlayer try-catch

I tried these codes but not working because some URLs are HTTPS or some URI,s includes some characters after the format

String URL = et_url.getText().toString();
    if(URL.matches("http://[a-zA-Z0-9._-]+.[a-z]/[a-zA-Z0-9._-].mp4"))
    {

    }
    else if (URL.matches("http://[a-zA-Z0-9._-]+.[a-z]/[a-zA-Z0-9._-].mkv"))
    {

    }

Upvotes: 2

Views: 9396

Answers (2)

Al-Amin
Al-Amin

Reputation: 1387

Simply you can solve this using this way -

 String fileType = getFileTypeFromURL("https://sample-videos.com/video123/mp4/720/big_buck_bunny_720p_1mb.mp4");
        if(fileType.equals("video")){
            // Do your task here
        }



private String getFileTypeFromURL(String url){
        String[] splitedArray = url.split("\\.");
        String lastValueOfArray = splitedArray[splitedArray.length-1];
        if(lastValueOfArray.equals("mp4") || lastValueOfArray.equals("flv") || lastValueOfArray.equals("m4a") || lastValueOfArray.equals("3gp") || lastValueOfArray.equals("mkv")){
            return "video";
        }else if(lastValueOfArray.equals("mp3") || lastValueOfArray.equals("ogg")){
            return "audio";
        }else if(lastValueOfArray.equals("jpg") || lastValueOfArray.equals("png") || lastValueOfArray.equals("gif")){
            return "photo";
        }else{
            return "";
        }
    }

Edit:

You can check this by using headers information -

URL obj = new URL(VIDEO_URL);
URLConnection conn = obj.openConnection();

//Get all headers
Map<String, List<String>> map = conn.getHeaderFields();
for (Map.Entry<String, List<String>> entry : map.entrySet()) {
    System.out.println("Key : " + entry.getKey() +
                 " ,Value : " + entry.getValue());
}

//get header by 'key'
String content_type = conn.getHeaderField("Content-Type");

Upvotes: 0

viz
viz

Reputation: 1277

You could "probably" check if a URL locates a video resource by the trailing file extension regex you are trying to compose.

But theoretically, the URL representation itself won't tell you if a given resource on the network that a URL(or more broadly, a URI) is pointing to is a video or not. The URL can be anything like http://can_you_tell_this_is_a_video and still locate a video.

Also, it's hard/impossible to tell if a video is actually "playable" by the player(supported encoding, file format, etc.), even though you knew that that's a video resource. The simpler way to solve the problem would be to just try feeding the player with whatever URL you receive from the user. And if the player says it's not playable, then proceed with the next step.

On the other hand, if you just need to identify if a URL is locating is a video resource, then the straightforward way is to do a HTTP HEAD request at the URL with a HTTP client, and check the Content-Type header to see if the resource is among the common video MIME types.

To give an example, if you do a HEAD request on https://sample-videos.com/video123/mp4/720/big_buck_bunny_720p_1mb.mp4(you can test it here), the HTTP response will look like below(the server needs to support HEAD requests and provide the correct content-type header, normally they should):

HTTP/1.1 200 OK
Keep-Alive: timeout=5, max=100
Connection: Keep-Alive
Accept-Ranges: bytes
Content-Length: 1055736
Content-Type: video/mp4
Date: Wed, 22 May 2019 20:51:28 GMT
Last-Modified: Fri, 17 Jun 2016 17:43:54 GMT
Server: Apache

With this response, now you can check the Content-Type header value - as you can see, in this example the media type of the resource is video/mp4.

Upvotes: 5

Related Questions