GithubHelp home page GithubHelp logo

kmoskwiak / videojs-resolution-switcher Goto Github PK

View Code? Open in Web Editor NEW
402.0 34.0 244.0 3.39 MB

Resolution switcher adds the ability to select the video quality in video.js player.

Home Page: https://kmoskwiak.github.io/videojs-resolution-switcher/

License: Other

JavaScript 93.16% HTML 3.40% CSS 3.44%
videojs-plugin video-player resolution player

videojs-resolution-switcher's Introduction

Video.js Resolution Switcher Build Status

Resolution switcher for video.js v5

Example

Working examples of the plugin you can check out if you're having trouble. Or check out this demo.

Getting Started

Install plugin with

npm

npm i videojs-resolution-switcher

or bower

bower install videojs-resolution-switcher

Setup sources dynamically:

<video id='video' class="video-js vjs-default-skin"></video>
<script src="video.js"></script>
<script src="videojs-resolution-switcher.js"></script>
<script>
  videojs('video', {
    controls: true,
    plugins: {
        videoJsResolutionSwitcher: {
          default: 'high',
          dynamicLabel: true
        }
      }
  }, function(){
  
    // Add dynamically sources via updateSrc method
    player.updateSrc([
        {
          src: 'http://media.xiph.org/mango/tears_of_steel_1080p.webm',
          type: 'video/webm',
          label: '360'
        },
        {
          src: 'http://mirrorblender.top-ix.org/movies/sintel-1024-surround.mp4',
          type: 'video/mp4',
          label: '720'
        }
      ])

      player.on('resolutionchange', function(){
        console.info('Source changed to %s', player.src())
      })
      
  })
</script>

Or use <source> tags:

<video id="video" class="video-js vjs-default-skin" width="1000" controls data-setup='{}'>
   <source src="http://mirrorblender.top-ix.org/movies/sintel-1024-surround.mp4" type='video/mp4' label='SD' />
   <source src="http://media.xiph.org/mango/tears_of_steel_1080p.webm" type='video/webm' label='HD'/>
</video>
<script>
  videojs('video').videoJsResolutionSwitcher()
</script>

YouTube tech

YouTube tech form https://github.com/eXon/videojs-youtube

<video id='video' class="video-js vjs-default-skin"></video>
<script src="../lib/videojs-resolution-switcher.js"></script>
<script>
	videojs('video', {
		controls: true,
		techOrder:  ["youtube"],
		sources: [{ "type": "video/youtube", "src": "https://www.youtube.com/watch?v=iD_MyDbP_ZE"}],
		plugins: {
			videoJsResolutionSwitcher: {
				default: 'low',
				dynamicLabel: true
			}
		}
	}, function(){
		var player = this;
		player.on('resolutionchange', function(){
			console.info('Source changed')
		})
	});

</script>

Flash tech

When using flash tech preload="auto" is required.

Source options

Sources can passed to player using updateSrc method or <source> tag as shown above. Avalible options for each source are:

  • label - String (required) is shown in menu (ex. 'SD' or '360p')
  • res - Number is resolution of video used for sorting (ex. 360 or 1080)

Plugin options

You can pass options to plugin like this:

videojs('video', {
      controls: true,
      muted: true,
      width: 1000,
      plugins: {
        videoJsResolutionSwitcher: {
          default: 'low'
        }
      }
    }, function(){
      // this is player
    })

Avalible options:

  • default - {Number}|'low'|'high' - default resolution. If any Number is passed plugin will try to choose source based on res parameter. If low or high is passed, plugin will choose respectively worse or best resolution (if res parameter is specified). If res parameter is not specified plugin assumes that sources array is sorted from best to worse.
  • dynamicLabel - {Boolean} - if true current label will be displayed in control bar. By default gear icon is displayed.
  • customSourcePicker - {Function} - custom function for selecting source.
  • ui - {Boolean} - If set to false button will not be displayed in control bar. Default is true.

Methods

updateSrc([source])

Returns video.js player object if used as setter. If source is not passed it acts like player.src()

// Update video sources
player.updateSrc([
  { type: "video/mp4", src: "http://www.example.com/path/to/video.mp4", label: 'SD' },
  { type: "video/mp4", src: "http://www.example.com/path/to/video.mp4", label: 'HD' },
  { type: "video/mp4", src: "http://www.example.com/path/to/video.mp4", label: '4k' }
])

PARAMETERS:

name type required description
source array no array of sources

currentResolution([label], [customSourcePicker])

If used as getter returns current resolution object:

  {
    label: 'SD', // current label
    sources: [
      { type: "video/webm", src: "http://www.example.com/path/to/video.webm", label: 'SD' },
      { type: "video/mp4", src: "http://www.example.com/path/to/video.mp4", label: 'SD' }
    ] // array of sources with current label
  }

If used as setter returns video.js player object.

// Get current resolution
player.currentResolution(); // returns object {label '', sources: []}

// Set resolution
player.currentResolution('SD'); // returns videojs player object

PARAMETERS:

name type required description
label string no label name
customSourcePicker function no cutom function to choose source

customSourcePicker

If there is more than one source with the same label, player will choose source automatically. This behavior can be changed if customSourcePicker is passed.

customSourcePicker must return player object.

player.currentResolution('SD', function(_player, _sources, _label){
  return _player.src(_sources[0]); \\ Always select first source in array
});

customSourcePicker accepst 3 arguments.

name type required description
player Object yes videojs player object
sources Array no array of sources
label String no name of label

customSourcePicker may be passed in options when player is initialized:

var myCustomSrcPicker = function(_p, _s, _l){
  // select any source you want
  return _p.src(selectedSource);
}

videojs('video', {
      controls: true,
      muted: true,
      width: 1000,
      plugins: {
        videoJsResolutionSwitcher: {
          default: 'low',
          customSourcePicker: myCustomSrcPicker
        }
      }
    }, function(){
      // this is player
    })

getGroupedSrc()

Returns sources grouped by label, resolution and type.

Events

resolutionchange EVENT

Fired when resolution is changed

videojs-resolution-switcher's People

Contributors

aydinkn avatar filecage avatar gkralik avatar h1d avatar hartman avatar kmoskwiak avatar mysamimi avatar paladox avatar yuri-qq avatar zalastax avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

videojs-resolution-switcher's Issues

Resolution YouTube no change

Hello I tried to change the YouTube video resolution but does not seem to work, even in the sample site...

If I try to put the resolution 144 and 720p there are no changes.. I tried both with Chrome that with Edge

Bower support

Bower support would be nice so that this package could be managed as a frontend component.

playbackRate not maintained when switching sources

It doesn't look to me like the playback rate is being maintained when switching plugins. If I set the rate to 2.0, and then change the resolution for high to low, the player still reports the playback rate as being 2.0x, however the video is only playing at 1.0x.

player.controlBar is undefined

It looks like at the time that videoJsResolutionSwitcher is being loaded into a player (the function is being called), the player hasn't yet defined the controlBar object. This results in a no method error resolutionSwitcher is not defined on undefined. It looks as though the player.updateSrc is being called immediately when this function is executed which is triggering the issue.

If the body of videoJsResolutionSwitcher is wrapped with a player.ready(...), everything appears to work as intended.

Am I the only one seeing this issue?

Video.js: v5.0.0
Resolution Switcher: v0.1.1

Doesnt seem to change source

Hi

Really want to use this but not sure what i am doing wrong it loads the plugin fine and adds it to the control bar.

But when i change the resolution nothing seems to change, i have my hls video encoded in 360, 480, 720,1080

If i switch it to 360 i would expect it to be pixellated but it remains the same?

If i click auto i get.

Uncaught TypeError: Cannot read property 'bind' of undefined

I am using video js

My setup

var videoPlayer = videojs("video", {

                    plugins: {
                        videoJsResolutionSwitcher: {
                            dynamicLabel: true 
                        }
                    } 
                });

                videoPlayer.updateSrc({ 
                    type: "application/x-mpegURL", src: video 
                }, {hls: true});

                videoPlayer.on('resolutionchange', function(){
                  console.info('Source changed to %s', videoPlayer.src())
                });

Any suggestions where i might be going wrong?

Thanks

Not an issue; YouTube feature support?

Hey, great plugin. I'm trying to make it work with videojs-youtube but to no avail. Would you care to add support for it? Right now -- I plan to make the toggle reload with new YouTube options, it should work. I'll post when I have something.

Allow settings the formats in pixels

Hi please add support for settings formats in pixel wise since hd and sd and 4k but other formats not really have a name so please allow users to customise it.

Add API to get current resolution

Adding the following function would be nice: getCurrentResolution()

Another possibility would be to add the current resolution to the player element (data-current-res) or to pass the selected resolution into the resolutionchange event.

Usecase
I would like to store the last resolution of the user (via the resolutionchange event) in a cookie.

Does not work with iOS (9 or minor?) on iPhone

Hi,
I've noticed some behavior which I have to say I'm not sure if it is a bug or just the lack of how iOS treats such implementations.
When using your plugin on iOS on the iPhone the native Player shows up and no resolution can be chosen.

So, any suggestions if it is a bug of the plugin or just because of the native video player of iOS? To be clear, this just happens on iPhone not on iPad.

Player freezes on source switch

When switching sources, the player "freezes" temporarily. The entire player frame goes black, and is unresponsive until it is clicked on, at which point the proper playback resumes. This behavior is able to be replicated on the live demo page, as well as my own webserver when using the multiple source tag method. I am not sure what additional information will help with this bug, but please let me know and I will provide whatever you need. Thanks!

Cannot read property 'NAME' of undefined

The player works well with resolution plugin but error is on console.
videojs-resolution-switcher.js:362 Uncaught TypeError: Cannot read property 'NAME' of undefined

In hls playback, manually changing resolution should flush previous resolution buffer

Test case:

1- Playback a HLS playlist with videojs-resolution-switcher (hls-branch) loaded.
2- The player will start to load segments ahead of the current time, creating a buffer.
3- Change the resolution manually.
4- The new resolution will be played only after the buffer of the previous resolution.

This is a issue specially if the previous resolution has a low bitrate, so the buffer will be long and the new resolution will take too long to change. The user may think that the resolution switcher is not working.

So I think that when the resolution is changed manually, the buffer must be flushed.

Any thoughts?

Dispose function not available for resolutionSwitcher

Hey,

I have got the problem that I want to use the updateSrc function to dynamically change the videos source specific to the page you visit but I always get this error:
player.controlBar.resolutionSwitcher.dispose is not a function

And it is really that simple that the resolutionSwitcher Object does not have the dispose function attached and also the control bar of videojs otherwise I would have just disposed this.
I am using the newest stable version of videojs which is 5.4.6.

I am initializing the player like this:

var video_player=videojs( '#video_player', { controls: true, "autoplay": true,width:"650",
    plugins: {
        videoJsResolutionSwitcher: {
          default: 'high',
          dynamicLabel: true
        }
      },"preload": "auto" });

I visit my first video and it works perfectly and loads the video. When going to another video now the player should dynamically load the new video but now the dispose function would be run to remove the old selectors but like already said the dispose function is not found.
What am I doing wrong?

Plugin doesn't work with IE 9

I tested latest version of the plugin with IE 9 and got the following error.

Object doesn't support property or method 'registerComponent'

I tracked this down bit and found out that registerComponent is called for MenuItem and MenuButton objects.

MenuItem.registerComponent('ResolutionMenuItem', ResolutionMenuItem);
MenuButton.registerComponent('ResolutionMenuButton', ResolutionMenuButton);

I tested changing those registerComponent lines to videojs.registerComponent and it seemed to work also in IE 9.

videojs.registerComponent('ResolutionMenuItem', ResolutionMenuItem);
videojs.registerComponent('ResolutionMenuButton', ResolutionMenuButton);

The following is said at the video.js components documentation.

The player class and all control classes inherit from the Component class, or a subclass of Component.

So this sounds like there could be a bug in video.js inheritance of Component class when using IE 9. Still, do you think it would be ok to change those registerComponent method calls from MenuItem and MenuButton to videojs object in order to make the plugin IE 9 compatible?

Support downloading videos

Hi please add the support for downloading videos through this plugin please.

YouTube I doint think we can support it for that but downloading from our own websites or other websites that allow downloading.

Dosent work

It dosent register properly because videoJsResolutionSwitcher is not a function it is videoJsResolutionSwitcher = function(option)

it should be function videoJsResolutionSwitcher(option) but that makes the error go away a new one comes up so please test a fix you propose to use please.

How to initialize multiple player?

Hi,

i'm trying to initialize multiple player dynamically.

var rand = Math.floor((Math.random() * 100) + 1); vjs('video_'+rand, { plugins: { resolutions: true } });

The video-elements receive unique ids, afterwards video.js should do its magic with the resolution-switcher.
Video.js is working, but you are unable to see the controls withe the resolution-switcher in every video-element after the first one.

Regards

Class vjs-selected isn't applied

Hi,

till the ~5.5.0 version class vjs-selected was working, here is an example:

1

Later, class vjs-selected isn't applied (5.9.0):

2

Question: building

New to videojs.
Downloaded and installed npm, node, grunt. Cloned videojs.
Installed the plugins I am interested in...

npm i videojs-resolution-switcher
npm install –save videojs-contrib-hls
npm install –save videojs-overlay
npm install –save videojs-watermark

I notice the instructions are slightly different for this plugin. I wonder if I need to be concerned.
I notice this plugin does not have a dist folder.
When I change to the folder with Gruntfile and run grunt dist, I get:

>> Local Npm module "grunt-contrib-clean" not found. Is it installed?
>> Local Npm module "grunt-contrib-concat" not found. Is it installed?
>> Local Npm module "grunt-contrib-uglify" not found. Is it installed?
>> Local Npm module "grunt-contrib-qunit" not found. Is it installed?
>> Local Npm module "grunt-contrib-jshint" not found. Is it installed?
>> Local Npm module "grunt-contrib-watch" not found. Is it installed?
Warning: Task "dist" not found. Use --force to continue.
Aborted due to warnings.

But when I "grunt dist" from the other plugin folders I get a videojs build (but not a new copy of the plugin in the plugin dist folder).

I am sure this is my own ignorance of grunt, npm or the plugin architecture/setup. And I begin a research mission now. But since the resolution switcher is different from the others (and more important!) I thought to make sure there is nothing special I need to know to get going on the right foot.

Thank you for any pointers.

Shared scope when using multiple players on one page

I reported a scope issue I was having with several plugins I'd written cross-talking when there were multiple players on the same page.

videojs/video.js#2997 (comment)

The reduced test case I built to show the issue also demonstrated that I was getting a similar issue with a 3rd party plugin, the videojs-resolution-switcher, which is what lead me to believe it might be an issue with videojs itself, and not the plugins.

The reduced test case is here: http://videojs-plugin-scope-issue.s3-website-us-east-1.amazonaws.com/

The steps to reproduce:

  • Start and then pause both videos, noting that both are SD resolution showing 480p placeholder footage
  • Open the browser dev console
  • Observe that player1 and player2 are separate players with different IDs and target elements
  • type: player1.currentResolution('HD');
  • Observe that player2 switches resolutions instead of player1

In terms of my plugins, it turned out I'd mistakenly named some variables containing data identically to the name of the plugins, for example, "annotations" is both the plugin function and a variable containing a list of annotations.

I believe the same might be the case in the resolution switcher. I think this line might be the culprit since it sits at the module level, rather than inside the initializer
https://github.com/kmoskwiak/videojs-resolution-switcher/blob/master/lib/videojs-resolution-switcher.js#L22

Support for Vimeo

I was wondering if we'd be able to get support for Vimeo?
With YouTube supported it hopefully wouldn't be to large of a hassle.

Create git tags for releases

Bower works significantly better if there is a git tag associated with a release. That allows a developer to define a particular version of the library to use in the bower.json. Without tags, git ref hashes have to be looked up and used.

Naming of element to prepend the controlBar child

As i scratched my head over this for some time now it should be mentioned somewhere that, to make the plugin work when using the controlBar -> children property like so:

var options = { plugins: { videoJsResolutionSwitcher: { default: 'SD', dynamicLabel: true } } ,controlBar: { children: { playToggle : true, CurrentTimeDisplay: true, TimeDivider: true, DurationDisplay: true, ProgressControl: true, resolutionSwitcher: {}, volumeMenuButton: { inline: false, vertical: true }, CustomControlsSpacer: true, SubtitlesButton: true, fullscreenToggle: {} } }, inactivityTimeout: 0, reportTouchActivity: true }; videojs("my-video", options);

the fullscreenToggle element needs to be named exactly like this (i mean camelcase). I had my children definition named FullscreenToggle and only after studying the source code of the videojs-resolution-switcher.js found out that the line of code where the child is added to the controlBar it says:

player.controlBar.resolutionSwitcher = player.controlBar.el_.insertBefore(menuButton.el_, player.controlBar.getChild('fullscreenToggle').el_);

The only thing i am wondering about is that it is working when no children setup value is given. So, all in all this is not really an issue, just something i wanted to point out to help others when using this plugin which is, all in all, a great one.

Can the default resolution be chosen automatically based on the user bandwidth?

Lately, I try videojs to setup video in my website.

It is great and easy to setting up different resolution videos by this plugin.

However, I want the default resolution can be selected dynamically based on the user bandwidth like Youtube or Vimeo videos. I do not know where to start and could anyone give some advices to help me out?

please

Hello
is there any possible way to put multip bitrates in a single url

i have one m3u8 url contain multip biterates how can i add it ?

Smooth switching

After source switch player is resetting (controls disappearing and player window becomes black).

Is it possible to make source switching without player black screen? Make it smooth like youtube make.

Default resolution in using <source> tags method

Hi, how can I setup default resolution by using this method?
----For example I want to choose 720p as default resolution----

<video id="video" class="video-js vjs-default-skin" width="1000" controls data-setup='{}'>
   <source src="http://example.com/360p.mp4" type='video/mp4' label='360p' res='360' />
   <source src="http://example.com/480p.mp4" type='video/mp4' label='480p' res='480' />
   <source src="http://example.com/720p.mp4" type='video/mp4' label='720P' res='720' />
</video>
<script>
  videojs('video').videoJsResolutionSwitcher()
</script>

I find this plugin very useful and great. Thanks for making this.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.