bratsun
11/11/2014 - 12:55 PM

Turn Vimeo or YouTube URL into embedded video

Turn Vimeo or YouTube URL into embedded video

.video-embed {
  height: 0;
  padding-bottom: 50.5% !important;
  padding-top: 25px;
  position: relative;
  width: 100% !important;
}

.video-embed iframe {
  position: absolute;
  top: 0;
  width: 100% !important;
  height: 100% !important;
}
// vimeo links

$('a[href*="vimeo.com"]').each(function() {
  $this = $(this);
  var href = '//player.vimeo.com/video/' + $this.attr('href').split('/').pop();
      
  $this.before('<div class="video-embed"><iframe src="//player.vimeo.com/video/'+$this.attr('href').split('/').pop()+'?autoplay=0&amp;title=0&amp;byline=0&amp;color=1A9691&amp;portrait=0&amp;hd_off=0" width="600" height="337.5" frameborder="0" webkitallowfullscreen mozallowfullscreen allowfullscreen></iframe></div>');
  
});

// youtube links

function getVideoId(url){
    if(url.indexOf('?') != -1 ) {
        var query = decodeURI(url).split('?')[1];
        //console.log(query);
        var params = query.split('&');
        for(var i=0,l = params.length;i<l;i++)
            if(params[i].indexOf('v=') === 0)
                return params[i].replace('v=','');
    } else if (url.indexOf('youtu.be') != -1) {
        return decodeURI(url).split('youtu.be/')[1];
    }
    return null;
}

$('a[href*="youtube.com"]').each(function() {
    var t = $(this);
    var yid = getVideoId(t.attr('href'));
    t.before('<div class="video-embed"><iframe src="http://www.youtube.com/embed/' + yid +'?rel=0&autoplay=0" width="600" height="337.5" frameborder="0" webkitallowfullscreen mozallowfullscreen allowfullscreen></iframe></div>');
  
});