我正在使用散景和面板创建动画情节以及一些导航控件,如滑块、播放/暂停、跳过 5 秒等。
同时,有一些视频片段我想与该动画同步显示。我开始使用 dash.js 并设法相应地准备视频并将其显示在独立页面中。(耶!)
由于我不太了解javascript,所以我想知道:是否有同步这两件事的解决方案?
(一个梦想的场景:一个面板小部件,用于从 python 显示和控制 dash.js 视频播放器。嗯,希望对吧?但我会接受任何提示、建议和想法。)
我正在使用散景和面板创建动画情节以及一些导航控件,如滑块、播放/暂停、跳过 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.")