Skip to content

Instantly share code, notes, and snippets.

@ayappaP
Created April 7, 2021 10:07
Show Gist options
  • Save ayappaP/8326e2abc71a9ed2b4e988ddb9c766a6 to your computer and use it in GitHub Desktop.
Save ayappaP/8326e2abc71a9ed2b4e988ddb9c766a6 to your computer and use it in GitHub Desktop.
/*! videojs-resolution-switcher - v0.4.2 - 2021-04-07
* Copyright (c) 2021 [object Object]; Licensed Apache-2.0 */
!function(){"use strict";var a=null;a="undefined"==typeof window.videojs&&"function"==typeof require?require("video.js"):window.videojs,function(a,b){var c,d={ui:!0},e=b.getComponent("MenuItem"),f=b.extend(e,{constructor:function(a,c){c.selectable=!0,e.call(this,a,c),this.src=c.src,a.on("resolutionchange",b.bind(this,this.update))}});f.prototype.handleClick=function(a){e.prototype.handleClick.call(this,a),this.player_.currentResolution(this.options_.label)},f.prototype.update=function(){var a=this.player_.currentResolution();this.selected(this.options_.label===a.label)},e.registerComponent("ResolutionMenuItem",f);var g=b.getComponent("MenuButton"),h=b.extend(g,{constructor:function(a,c){if(this.label=document.createElement("span"),c.label="Quality",g.call(this,a,c),this.el().setAttribute("aria-label","Quality"),this.controlText("Quality"),c.dynamicLabel)b.addClass(this.label,"vjs-resolution-button-label"),this.el().appendChild(this.label);else{var d=document.createElement("span");b.addClass(d,"vjs-menu-icon"),this.el().appendChild(d)}a.on("updateSources",b.bind(this,this.update))}});h.prototype.createItems=function(){var a=[],b=this.sources&&this.sources.label||{};for(var c in b)b.hasOwnProperty(c)&&a.push(new f(this.player_,{label:c,src:b[c],selected:c===(!!this.currentSelection&&this.currentSelection.label)}));return a},h.prototype.update=function(){return this.sources=this.player_.getGroupedSrc(),this.currentSelection=this.player_.currentResolution(),this.label.innerHTML=this.currentSelection?this.currentSelection.label:"",g.prototype.update.call(this)},h.prototype.buildCSSClass=function(){return g.prototype.buildCSSClass.call(this)+" vjs-resolution-button"},g.registerComponent("ResolutionMenuButton",h),c=function(a){function c(a,b){return a.res&&b.res?+b.res-+a.res:0}function e(a){var b={label:{},res:{},type:{}};return a.map(function(a){f(b,"label",a),f(b,"res",a),f(b,"type",a),g(b,"label",a),g(b,"res",a),g(b,"type",a)}),b}function f(a,b,c){null==a[b][c[b]]&&(a[b][c[b]]=[])}function g(a,b,c){a[b][c[b]].push(c)}function i(a,b){var c=k["default"],d="";return"high"===c?(c=b[0].res,d=b[0].label):"low"!==c&&null!=c&&a.res[c]?a.res[c]&&(d=a.res[c][0].label):(c=b[b.length-1].res,d=b[b.length-1].label),{res:c,label:d,sources:a.res[c]}}function j(a){var b={highres:{res:1080,label:"1080",yt:"highres"},hd1080:{res:1080,label:"1080",yt:"hd1080"},hd720:{res:720,label:"720",yt:"hd720"},large:{res:480,label:"480",yt:"large"},medium:{res:360,label:"360",yt:"medium"},small:{res:240,label:"240",yt:"small"},tiny:{res:144,label:"144",yt:"tiny"},auto:{res:0,label:"auto",yt:"auto"}},c=function(b,c,d){return a.tech_.ytPlayer.setPlaybackQuality(c[0]._yt),a.trigger("updateSources"),a};k.customSourcePicker=c,a.tech_.ytPlayer.setPlaybackQuality("auto"),a.tech_.ytPlayer.addEventListener("onPlaybackQualityChange",function(d){for(var e in b)if(e.yt===d.data)return void a.currentResolution(e.label,c)}),a.one("play",function(){var d=a.tech_.ytPlayer.getAvailableQualityLevels(),f=[];d.map(function(c){f.push({src:a.src().src,type:a.src().type,label:b[c].label,res:b[c].res,_yt:b[c].yt})}),a.groupedSrc=e(f);var g={label:"auto",res:0,sources:a.groupedSrc.label.auto};this.currentResolutionState={label:g.label,sources:g.sources},a.trigger("updateSources"),a.setSourcesSanitized(g.sources,g.label,c)})}var k=b.mergeOptions(d,a),l=this;l.updateSrc=function(a){if(!a)return l.src();a=a.filter(function(a){try{return""!==l.canPlayType(a.type)}catch(b){return!0}}),this.currentSources=a.sort(c),this.groupedSrc=e(this.currentSources);var b=i(this.groupedSrc,this.currentSources);return this.currentResolutionState={label:b.label,sources:b.sources},l.trigger("updateSources"),l.setSourcesSanitized(b.sources,b.label),l.trigger("resolutionchange"),l},l.currentResolution=function(a,b){if(null==a)return this.currentResolutionState;if(this.groupedSrc&&this.groupedSrc.label&&this.groupedSrc.label[a]){var c=this.groupedSrc.label[a],d=l.currentTime(),e=l.paused();!e&&this.player_.options_.bigPlayButton&&this.player_.bigPlayButton.hide();var f="loadeddata";return"Youtube"!==this.player_.techName_&&"none"===this.player_.preload()&&"Flash"!==this.player_.techName_&&(f="timeupdate"),l.setSourcesSanitized(c,a,b||k.customSourcePicker).one(f,function(){l.currentTime(d),l.handleTechSeeked_(),e||l.play().handleTechSeeked_(),l.trigger("resolutionchange")}),l}},l.getGroupedSrc=function(){return this.groupedSrc},l.setSourcesSanitized=function(a,b,c){return this.currentResolutionState={label:b,sources:a},"function"==typeof c?c(l,a,b):(l.src(a.map(function(a){return{src:a.src,type:a.type,res:a.res}})),l)},l.ready(function(){if(k.ui){var a=new h(l,k);l.controlBar.resolutionSwitcher=l.controlBar.el_.insertBefore(a.el_,l.controlBar.getChild("fullscreenToggle").el_),l.controlBar.resolutionSwitcher.dispose=function(){this.parentNode.removeChild(this)}}l.options_.sources.length>1&&l.updateSrc(l.options_.sources),"Youtube"===l.techName_&&j(l)})},b.plugin("videoJsResolutionSwitcher",c)}(window,a)}();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment