简体   繁体   English

如何在 HTML5 视频播放器中获取特定时间戳的字幕文本?

[英]How to fetch the subtitle text at a specific timestamp in HTML5 video player?

So Ive this html5 video player code which plays video and loads subtitle.所以我有这个 html5 视频播放器代码,它播放视频并加载字幕。 It also attempts to show the subtitle at an exact timestamp, so that the text at that timestamp could be read into a variable and submitted to a database.它还尝试以确切的时间戳显示字幕,以便可以将处于该时间戳的文本读入变量并提交到数据库。 THE JSFIDDLE JSFIDDLE

But I want to display the subtitle text dynamically so that as soon as I press submit, the subtitle at that particular timestamp gets available to be saved in a database, in a frame somewhat like this:-但是我想动态显示字幕文本,以便一旦我按下提交,该特定时间戳的字幕就可以保存在数据库中,在一个有点像这样的框架中:-

<p>Subtitle_tstamp: <span id="s_tstamp"></span></p>
<input name="s_tstamp" id="hidden-tstamp" type="hidden">
<p class="submit">
<button type="submit" class="btn btn-primary" name="submit" value="Submit" id="submit_form">Submit</button>
<p>

I can see "hidden-tstamp: ", but subtitle isnt displaying in front of it.我可以看到“hidden-tstamp:”,但字幕没有显示在它前面。 I want it to be displayed under the video in front of "hidden-tstamp: " like in this tutorial .我希望它像本教程一样显示在“hidden-tstamp:”前面的视频下。 And when I click submit, the subtitle text at that time should get passed/saved into a variable for further processing.当我点击提交时,当时的字幕文本应该被传递/保存到一个变量中以供进一步处理。

The <track> element has thecuechange event. <track>元素具有cuechange事件。 This event fires every time whenever a subtitle has been changed.每次更改字幕时都会触发此事件。 That means when a text shows and when a text disappears.这意味着何时显示文本以及何时消失文本。

Select your <track> and <input> element, listen for the cuechange event and based on the current text that is showing update the value of your input . Select 您的<track><input>元素,侦听cuechange事件并根据当前显示的text更新您的input value

In the event handler check if there are any activeCues .在事件处理程序中检查是否有任何activeCues These are stored in an array.这些存储在一个数组中。 So if the array is empty no text is showing, if it is not select the first key and select the text property of it.因此,如果数组为空,则不显示任何文本,如果不是 select 第一个键和 select 它的text属性。 That is the current subtitle.那是当前的字幕。 Set the <input> ' value to the text property.<input> ' 值设置为text属性。

All of the data of about the track is stored in the event.target.track property of the cuechange event.所有关于轨道的数据都存储在cuechange事件的event.target.track属性中。

// Select the track and the input element.
const video = document.getElementById('my_video_1');
const track = video.querySelector('track');
const stamp = document.getElementById('hidden-tstamp');

// Listen for the cuechange event.
// Check if there are active cues and use them as value.
track.addEventListener('cuechange', event => {
  const currentCues = event.target.track.activeCues;
  if (currentCues.length) {
    stamp.value = currentCues[0].text;
  } else {
    stamp.value = '';
  }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM