项目作者: contently

项目描述 :
A plugin for video.js to add support for timeline moment/range comments and annotations
高级语言: JavaScript
项目地址: git://github.com/contently/videojs-annotation-comments.git
创建时间: 2017-03-20T18:46:55Z
项目社区:https://github.com/contently/videojs-annotation-comments

开源协议:Other

下载


CircleCI

AnnotationComments : Collaborate in your VideoJS player

AnnotationComments Screenshot1

Upgrading v1 -> v2

Please note that the event based API has changed. In version 1, you can subscribe to plugin events with pluginInstance.on(). In version 2, the same functionality is available with pluginInstance.registerListener(). The following docs are for the latest version.

About

Background

Collaboration between videographers and clients can be tedious, with emails and phone calls that waste time trying to reference specific frames and areas of the screen. This plugin enables more efficient collaboration from the browser.

This plugin was conceived and developed as a Hack Week project at Contently by Evan Carothers and Jack Pope. Continuing our focus and commitment to multimedia support at Contently, the entire team productized and bulletproofed the plugin as a flexible solution to be used in our product and other open-source use cases.

Goals

  • Efficient for videographers and clients alike - Provides useful collaboration features including annotations, comments/replies, ranged time markers, and more, with intuitive controls.
  • SIMPLE & LIGHTWEIGHT - Everything is contained within the plugin and player element. There is no need to build additional UI components. Just install VideoJS, register the plugin, setup whatever backend storage you wish, and start collaborating.
  • EXTENSIBLE - The plugin can be integrated with existing commenting systems (as we did within Contently), and makes very few assumptions about how to store annotations. Custom events are available for communicating with external APIs, providing support for on-page interactions and data persistence. Simple CSS overrides can also allow for branding customizations with minimal effort, or completely custom UI/UX.

VideoJS Plugins

VideoJS is a popular open-source HTML5 video player library used by 400k+ sites. As of v6, there is an extendable plugin architecture which was used to create this plugin. This plugin is built and tested against VideoJS v7

Use it!

Install

  1. yarn add @contently/videojs-annotation-comments

OR

  1. npm install @contently/videojs-annotation-comments

Add it to your VideoJS player

As a script from build

  1. // ...videojs & videojs-annotation-comments have been loaded in script tags...
  2. var player = videojs('video-id');
  3. var plugin = player.annotationComments(pluginOptions)

As a module

  1. import videojs from 'video.js'
  2. import AnnotationComments from '@contently/videojs-annotation-comments'
  3. videojs.registerPlugin('annotationComments', AnnotationComments(videojs))
  4. var player = videojs('video-id')
  5. var plugin = player.annotationComments(pluginOptions)

Plugin options / configuration

When initializing the plugin, you can pass in an options array to override default options. Any excluded options are set to their default values, listed below:

  1. const pluginOptions = {
  2. // Collection of annotation data to initialize
  3. annotationsObjects: [],
  4. // Flexible meta data object (currently used for user data, but addl data can be provided to wrap each comment with metadata - provide the id of the current user and fullname of the current user at minimum, which are required for the UI)
  5. meta: { user_id: null, user_name: null },
  6. // Use arrow keys to move through annotations when Annotation mode is active
  7. bindArrowKeys: true,
  8. // Show or hide the control panel and annotation toggle button (NOTE - if controls are hidden you must provide custom UI and events to drive the annotations - more on that in "Programmatic Control" below)
  9. showControls: true,
  10. // Show or hide the comment list when an annotation is active. If false, the text 'Click and drag to select', will follow the cursor during annotation mode
  11. showCommentList: true,
  12. // If false, annotations mode will be disabled in fullscreen
  13. showFullScreen: true,
  14. // Show or hide the tooltips with comment preview, and annotation shape, on marker hover or timeline activate
  15. showMarkerShapeAndTooltips: true,
  16. // If false, step two of adding annotations (writing and saving the comment) will be disabled
  17. internalCommenting: true,
  18. // If true, toggle the player to annotation mode immediately after init. (NOTE - "annotationModeEnabled" event is not fired for this initial state)
  19. startInAnnotationMode: false
  20. };

Annotation Data Structure

To initialize the plugin with the annotationsObjects collection, use the following structure:

  1. const annotationsObjects = [{
  2. id: 1,
  3. range: {
  4. start: 10,
  5. end: 15
  6. },
  7. shape: {
  8. x1: 23.47,
  9. y1: 9.88,
  10. x2: 60.83,
  11. y2: 44.2
  12. },
  13. comments: [{
  14. id: 1,
  15. meta: {
  16. datetime: '2017-03-28T19:17:32.238Z',
  17. user_id: 1,
  18. user_name: 'Jack Pope'
  19. },
  20. body: 'The first comment!'
  21. }]
  22. }];

Programmatic Control

If you’d like to drive the plugin or render plugin data through external UI elements, you can configure the plugin to hide the internal components and pass data through custom events. There are two kinds of AnnotationComments API events, externally fired and internally fired.

Waiting for Plugin Ready

Before triggering any events on the plugin, you must wait for it to be ready. You can use the onReady function on the plugin:

  1. plugin.onReady(() => {
  2. // do stuff with the plugin, such as fire events or setup listeners
  3. });

Supported Externally Fired Events:

These events are external actions that can be called from your scripts to trigger events within the plugin:

  1. // openAnnotation : Opens an annotation within the player given an ID
  2. plugin.fire('openAnnotation', { id: myAnnotationId });
  3. // closeActiveAnnotation : Closes any active annotation
  4. plugin.fire('closeActiveAnnotation');
  5. // newAnnotation : Adds a new annotation within the player and opens it given comment data
  6. plugin.fire('newAnnotation', {
  7. id: 1,
  8. range: { start: 20, end: null },
  9. shape: { // NOTE - x/y vals are % based (Floats) in video, not pixel values
  10. x1: null,
  11. x2: null,
  12. y1: null,
  13. y2: null
  14. },
  15. commentStr: "This is my comment."
  16. });
  17. // destroyAnnotation : Removes an annotation and it's marker within the player given comment data
  18. plugin.fire('destroyAnnotation', { id: 1 });
  19. // newComment : Adds a new comment to an Annotation given an Annotation ID and a body
  20. plugin.fire('newComment', { annotationId: 1, body: "My comment string" });
  21. // destroyComment : Removes a comment from an Annotation given a Comment ID
  22. plugin.fire('destroyComment', { id: 1 });
  23. // addingAnnotation : Plugin enters the adding annotation state (adding an annotation at the current player timestamp)
  24. plugin.fire('addingAnnotation');
  25. // cancelAddingAnnotation : Plugin exits the adding annotation state
  26. plugin.fire('cancelAddingAnnotation');
  27. // toggleAnnotationMode : toggle annotation mode to alternative on/off value
  28. plugin.fire('toggleAnnotationMode');

Supported Internally Fired Events:

These are events that are triggered from within the running plugin and can be listened for by binding to plugin.registerListener within your scripts:

  1. // annotationOpened : Fired whenever an annotation is opened
  2. plugin.registerListener('annotationOpened', (event) => {
  3. // event.detail =
  4. // {
  5. // annotation: (object) annotation data in format {id:.., comments:..., range:..., shape:...},
  6. // triggered_by_timeline: (boolean) TRUE = the event was triggered via a timeline action (like scrubbing or playing), FALSE = the annotation was opened via marker click, UI button interactions, or API/event input
  7. // }
  8. });
  9. // annotationClosed : Fired whenever an annotation is closed
  10. plugin.registerListener('annotationClosed', (event) => {
  11. // event.detail = annotation (object) in format {id:.., comments:..., range:..., shape:...}
  12. });
  13. // addingAnnotationDataChanged : Fired from adding annotation state if:
  14. // 1. the marker is dragged
  15. // 2. the start of the marker is moved via control buttons
  16. // 3. the shape is dragged
  17. plugin.registerListener('addingAnnotationDataChanged', (event) => {
  18. var newRange = event.detail.range; // returns range data if range was changed
  19. var newShape = event.detail.shape; // returns shape data if shape was changed
  20. // do something with the data
  21. });
  22. // annotationDeleted : Fired when an annotation has been deleted via the UI
  23. plugin.registerListener('annotationDeleted', (event) => {
  24. // annotationId = event.detail
  25. });
  26. // enteredAnnotationMode : Fired when the plugin enters adding annotation mode
  27. // includes initial range data
  28. plugin.registerListener('enteredAddingAnnotation', (event) => {
  29. var startTime = event.detail.range.start;
  30. // do something when adding annotation state begins
  31. });
  32. // onStateChanged: Fired when plugin state has changed (annotation added, removed, etc)
  33. // This is a way to watch global plugin state, as an alternative to watching various annotation events
  34. plugin.registerListener('onStateChanged', (event) => {
  35. // event.detail = annotation state data
  36. });
  37. // playerBoundsChanged : Fired when the player boundaries change due to window resize or fullscreen mode
  38. plugin.registerListener('playerBoundsChanged', (event) => {
  39. var bounds = event.detail;
  40. // do something with the new boundaries
  41. });
  42. // Entering annotation mode (annotation icon was clicked when previously 'off')
  43. plugin.registerListener('annotationModeEnabled', (event) => {
  44. // do something
  45. });
  46. // Exiting annotation mode (annotation icon was clicked when previously 'on')
  47. plugin.registerListener('annotationModeDisabled', (event) => {
  48. // do something
  49. });

Develop and Build

We’re using yarn for package management and gulp as our build system.

The fastest way to get started:

  • Clone the repo
  • Run yarn install
  • Run yarn build
  • Run yarn watch
  • Visit http://localhost:3004/test.html to see the magic happen.

Templates

We’re using the Handlebars templating library to render various components within the plugin. For performance, the templates are pre-compiled into a JS file within the development environment. That way we only need to require the Handlebars runtime, saving nearly 100kb from the minified build! ⚡️

The gulp templates task is used to precompile every template to /src/js/compiled/templates.js. This file should not be modified directly, but rather the templates themselves in /src/templates should be modified if changes are needed. The templates task will run automatically within gulp watch.

UI / CSS Customization

The plugin uses SASS and all styles are defined in annotaitons.scss. There is extenssive commenting on classes and styles in the file. The plugin uses a deep level of specificity to prevent styles from polluting elements on the page, and all classes are prefixed with vac- to prevent classname collisions in the global namespace.

You can extend/modify colors and elements quite easily by writing an overrides stylesheet to address the specific elements that you wish to modify. You can also change the variable colors in the stylesheet and compile yourself for more customization.

NOTE - our gulp build tasks use an auto-prefixer to make the styles work cross-browser, so be sure to run that yourself if you compile the SASS files with changes.

Testing

Feature tests

Feature tests are currently browser-based and run by visiting http://localhost:3004/mocha/features/index.html. Feature tests can be added as files in the /test/mocha/features/ directory and then included within the index.html file as a external scripts.

Unit tests

Unit tests are run through the gulp test task. If the tdd task is included in gulp watch, the tests will run with every change to the test files. Each module should have a corresponding unit test file within the /test/mocha/modules directory.

Gulp commands

gulp watch: Fires up webserver @ http://localhost:3004/test.html, watches for any file changes in /src, including js, css (scss), and templates (.hbs), repackages, and transpiles to an unminified file in /build on change.

gulp transpile: Transpiles modules/files to build file in /build with JS maps

gulp build: Runs transpilation, browserify, sass, then minifies to distribution filename in /build with attribution

gulp templates: Uses Handlebars to pre-compile templates into a javascript file. See Templates section above.

gulp test: Runs the mocha unit tests within the /test/mocha/modules/ directory.

gulp lint: Runs jshint linter on javascript files in /src

License

This plugin is licensed under the Apache License, Version 2.0, which is the same license used by Video.js