Get video infos Youtube - Vimeo - Dailymotion

Get video infos Youtube - Vimeo - Dailymotion

by Kelly Hawker

HTML

Add Youtube, Video, Dailymotion URL
<br>
EX: https://www.youtube.com/watch?v=Z18qh5wy3fY
<br><br>
<form id="vidForm" name="input" method="get">Video URL:
    <input type="text" name="vidURL">
    <input type="submit" value="Submit">
</form>
<div id="video_Infos"></div>

JavaScript

$("#vidForm").submit(function (e) {
    e.preventDefault();
    var VideoUrl = $("#vidForm input[name=vidURL]").val();
    if (VideoUrl) {
        checkUrl(VideoUrl);
    } else {
        alert('Enter a full video url');
    }

});

function checkUrl(VideoUrl) {

    var testLoc = document.createElement('a');
    testLoc.href = VideoUrl.toLowerCase();
    url = testLoc.hostname;
    var what;
    if (url.indexOf('youtube.com') !== -1) {
        getYoutubeId(VideoUrl);
        //what='Youtube';
    }
    if (url.indexOf('vimeo.com') !== -1) {
        getVimedoId(VideoUrl);
        //what='Youtube';
    }
    if (url.indexOf('dailymotion.com') !== -1) {
        getDailyMotionId(VideoUrl);
        //what='Youtube';
    }

}

function getYoutubeId(url) {
    //alert(url);
    var video_id = url.split('v=')[1];
    var ampersandPosition = video_id.indexOf('&');
    if (ampersandPosition != -1) {
        video_id = video_id.substring(0, ampersandPosition);
    }
    //alert(video_id);
    YTvideoInfos(video_id);
}

function getVimedoId(url) {

    var regExp = /^.+vimeo.com\/(\d+)($|\/)/;
    var match = url.match(regExp);

    if (match) {
        //alert(match[1]);
        VMvideoInfos(match[1]);
    } else {
        alert("not a good vimeo url");
    }
}



function getDailyMotionId(url) {
    var m = url.match(/^.+dailymotion.com\/(video|hub)\/([^_]+)[^#]*(#video=([^_&]+))?/);
    if (m !== null) {
        if (m[4] !== undefined) {
            //return m[4];
            DMvideoInfos(m[4]);
        }
        //return m[2];
        DMvideoInfos(m[2]);
    }
    //return null;
}


function YTvideoInfos(video_id) {

    $.getJSON('//i.ytimg.com/vi/' + video_id + '/0.jpg', function (data, status, xhr) {
        //alert(data.data.title);
        var title = data.data.title;
        var description = data.data.description;
        var thumbnail = data.data.thumbnail['hqDefault'];
        $("#video_Infos").html('Title: ' + title + '</br>description: ' + description +...