问题描述
我正在使用散景和面板来创建动画图以及一些导航控件,例如滑块、播放/暂停、跳过 5 秒等。
同时,我希望与该动画同步显示视频片段。我开始使用 dash.js 并设法相应地准备视频并将其显示在独立页面中。 (耶!)
由于我对 javascript 了解不多,所以我想知道:是否有同步这两件事的解决方案?
(一个梦想场景:一个用于显示和控制来自 python 的 dash.js 视频播放器的面板小部件。嗯,可以希望,对吧?但我会接受任何提示、建议和想法。)
解决方法
回答我自己的问题,以防万一它可以为任何人节省一些试验和错误。
走进几个死胡同后,我编写了一个小型自定义散景小部件,可以满足我的需求。
代码如下:
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.")