How to sync a bokeh/panel animation with a video player like dash.js?

460 views Asked by At

I am using bokeh and panel to create an animated plot together with some navigation controls like a slider, play/pause, skip 5s, etc.

At the same time, there is video footage which I would like to display in sync with that animation. I started playing around with dash.js and managed to prepare the video accordingly and display it in a standalone page. (yay!)

Since I don't know much javascript, I was wondering: Is there a solution out there for synchronizing these two things?

(A dream scenario: A panel widget for displaying and controlling a dash.js video player from python. Well, one can hope, right? But I'll take any hints, advice, ideas.)

1

There are 1 answers

0
Ansgar T. On

Answering my own question, just in case it may save anyone a bit of trial&error.

After walking into a few dead ends, I wrote a small custom bokeh widget that does what I need.

Here's the code:

from bokeh.core.properties import Bool, String, Float
from bokeh.models import Div

DASH_URL = <url to the dash.js lib>

JS_CODE = """
import * as p from "core/properties"
import {Div, DivView} from "models/widgets/div"

declare var dashjs: any

export type DashViewerData = {from: number, to: number}

export class DashViewerView extends DivView {
  model: DashViewer
  private video_el: HTMLElement
  private player: any  

  render(): void {
    super.render()
    this.video_el = document.createElement('video')
    this.video_el.id = this.model.video_element_id
    this.video_el.setAttribute('width', "1120px") 
    this.video_el.setAttribute('height', "630px")
    this.video_el.setAttribute('controls', '')
    this.el.appendChild(this.video_el)
    this.el.appendChild(document.createTextNode(this.model.url))

    document.createElement('script')
    this.player = dashjs.MediaPlayer().create();
    this.player.initialize(this.video_el, this.model.url, this.model.is_playing);
  }

  play_listener(){
    if (this.model.is_playing){
      this.player.play()
    }
    else {
      this.player.pause()
    }
  }

  connect_signals(): void {
    this.connect(this.model.properties.is_playing.change, this.play_listener);
    this.connect(this.model.properties.time.change, ()=>this.player.seek(this.model.time));
  }
}

export namespace DashViewer {
  export type Attrs = p.AttrsOf<Props>

  export type Props = Div.Props & {
    video_element_id: p.Property<string>
    url: p.Property<string>
    is_playing: p.Property<boolean>
    time: p.Property<number>
  }
}

export interface DashViewer extends DashViewer.Attrs {}

export class DashViewer extends Div {
  properties: DashViewer.Props
  __view_type__: DashViewerView

  constructor(attrs?: Partial<DashViewer.Attrs>) {
    super(attrs)
  }

  static init_DashViewer(): void {
    this.prototype.default_view = DashViewerView

    this.define<DashViewer.Props>({
      video_element_id: [p.String, 'dashviewer_video'],
      url:              [p.String, ''],
      is_playing:       [p.Boolean, false],
      time:             [p.Number, 0.0]
    })
  }
}
"""


class DashViewer(Div):
    __implementation__ = JS_CODE
    __javascript__ = [DASH_URL]
    __css__ = []

    video_element_id = String(default='dash_player', help="id of the video element in the DOM")
    url = String(help="The URL from which to load the video. (This should point to an mpd file.)")
    is_playing = Bool(default=False)
    time = Float(default=0.0, help="Time in seconds. Change this to make the player jump to that time.")