最后,我的策略是在布局中创建一个div(‘字幕-容器’)作为DashPlayer的sibling 项:
dash_player.DashPlayer(
id='vid',
controls = True,
url = None,
style={'margin-top':'20px','margin-bottom':'20px', 'margin-left':'10px'},
playing= False,
muted= False
),
html.Div(
id='subtitle-container',
children=[html.P("subtitle text")],
style={}
),
我使用DashPlayer的‘Current-Time’回调在字幕容器中实现了自己的逻辑来显示字幕.
@app.callback([Output('subtitle-container','children'),Output('subtitle-container', 'style')],
[Input('vid', 'currentTime')])
def update_subtitles(current_time):
subtitle_text = get_subtitle_for_time(current_time)
# Determine whether subtitles are being displayed
subtitles_displayed = bool(subtitle_text)
# Set the alpha value based on whether subtitles are being displayed
alpha_value = 0 if not subtitles_displayed else 0.5
# Update subtitle container style
subtitle_style={
'position':'absolute',
'bottom':'6%',
'left': '25%',
'width':'50%',
'background-color':f'rgba(30,30,30, {alpha_value})',
'padding':'5px',
'whiteSpace':'pre-wrap',
'color':'white',
'text-align':'center'
}
return subtitle_text, subtitle_style
该回调根据播放器的当前时间返回相应的字幕,显示在‘subtitle-tainer’中.我还实现了确定字幕样式的逻辑,当字幕存在时是半透明的黑色背景,而当没有字幕时没有背景(Alpha=0).
为了最初收集所有字幕,我解析了一个SRT文件,以生成一个字幕项列表.每个标题项都是3个项目的列表,即开始时间、结束时间和标题文本.例如
[1185.65, 1193.74, "You're the one who was frowning.\nFrisco. San Francisco. Exactly.'].
计时已从原始SRT时间戳转换而来.
获得球员当前时间的适当提示很简单:
def get_subtitle_for_time(current_time):
for caption in captions:
if current_time <= caption[1]:
if current_time >= caption[0]:
return caption[2]
return ''