Nrc
Nrc

Reputation: 9787

How to know if a url is audio or video?

I get some urls from xml rss feed. The rss makes no distinction to audio or video files, they all come from the same tag. How can I distinguish if it is a audio or video url to provide the right to play it?

I have the url stored in a var

var url = "http://www.someWeb/someFile.mp3";

I tried to make a search with jQuery:

var search = url.search(/.mp3|.m4a/i);
if (search > -1){
    $('#check').text("it is audio");
}else {
    $('#check').text("it is video");
}

it works most of the cases but it could be more types of audio files, there could always be some types of audio, without extension...

Is there a better way to know if a url is a audio or file?

Upvotes: 0

Views: 4077

Answers (4)

Guilherme Dellagustin
Guilherme Dellagustin

Reputation: 83

This is a bit of a dificult one. I faced the same issue, loading content from an RSS feed, that could contain either video or audio.

There is no way to know what is the content without actually requesting it, and as I'm consuming content from external websites, I can't know for sure if they support the HEAD method.

what I'm currently doing is assuming the URL ends in a filename, taking the extension and using the browser ready version of https://github.com/broofa/node-mime to get the corresponding MIME type.

After getting the MIME type I check if it starts with either 'video' or 'audio'.

Here is the code:

function isVideoByURL(url) {
    var filename = url.split('/').pop().split('#')[0].split('?')[0];

    if(filename) {
        var ext = filename.split('.').pop();
        var mimeType = mime.getType(ext);

        return mimeType.startsWith('video');
    }

    return false;
}

Upvotes: 0

scalloty
scalloty

Reputation: 118

You must get HTTP header fields (Content-Type for this case). Look here: http://en.wikipedia.org/wiki/List_of_HTTP_header_fields

Upvotes: 0

Flosculus
Flosculus

Reputation: 6946

This is a shot in the dark, but if you need to know the content type before loading it, you can try a HEAD request:

http://www.w3.org/Protocols/rfc2616/rfc2616-sec9.html#sec9.4

This is only if the content service supports it though. Of course if you wrote the service then you can implement this yourself. Simply send the headers, but omit the actual content.

On the receiving end the response headers should include 'Content-Type'.

From this you can extract the type of content being delivered. It typically looks like this:

text/plain
image/jpeg
video/mp4
audio/mp3

It is simple enough to determine the general media from the prefix.

Upvotes: 0

JMillner
JMillner

Reputation: 49

You might be able to use the php function get_headers(). You can test this out by using it to grab one of your links and doing a print_r() to see what you get under the "Content-type" key.

If that works, just go ahead and create an switch case for all your file types and you should be good to go

http://php.net/manual/en/function.get-headers.php

Upvotes: 1

Related Questions