gpt4 book ai didi

javascript - VTTCue 未检测到 Javascript 中视频字幕的“\n”

转载 作者:行者123 更新时间:2023-12-03 01:39:05 26 4
gpt4 key购买 nike

我正在尝试播放带字幕的视频。我正在从 subtitles.txt 文件中提取持续时间和字幕字符串。字幕已正确提取并播放,但“code”未检测到下一行 = '\n'。所以请帮助我。

我想要的输出:

Golden dreams
and great heartache

我得到的输出:

Golden dreams\nand great heartache
<小时/>

我的subtitles.txt文件:

211 --> 223
"Golden dreams\nand great heartache"

我的代码:

<html>
<head>
<meta charset="utf-8"/>
<title></title>
</head>
<body>
<video video-player src="video.mp4"></video>
<script>
var video_player = document.querySelectorAll('[video-player]')[0];

track_english = video_player.addTextTrack("captions", undefined, "en");
track_english.mode = "showing";
subtitles_xhr(function(buffer)
{
var file = buffer;
file = file.split('\n');
for (var x = 0; x < file.length; x+=2)
{
track_english.addCue(new VTTCue(file[x].split(" --> ")[0], file[x].split(" --> ")[1], file[x+1]));
}
});

function subtitles_xhr (cb)
{
var xhr = new XMLHttpRequest;
xhr.open('GET', "subtitles.txt");
xhr.onload = function ()
{
cb(xhr.response);
};
xhr.send();
}
</script>
</body>
</html>

最佳答案

您的问题是,当您读取文本文件时显示的 \n 字符实际上是 \\n 字符。

VTTCue 解析器不会将这些字符识别为新行,因此您需要将 VTTCue 构造函数的第三个参数中的这些字符替换为实际的新行,\n.

// make the file available in StackSnippet
const txt_uri = URL.createObjectURL(new Blob([String.raw`1 --> 13
"Golden dreams\nand great heartache"`], {type: 'text/plain'}));

var video_player = document.querySelectorAll('[video-player]')[0];

track_english = video_player.addTextTrack("captions", undefined, "en");
track_english.mode = "showing";
subtitles_xhr(function(buffer) {
var file = buffer;
file = file.split('\n');
for (var x = 0; x < file.length; x += 2) {
let startTime = file[x].split(" --> ")[0],
endTime = file[x].split(" --> ")[1],
content = file[x + 1]
.replace(/\\n/g, '\n'); // here replace all occurrences of '\\n' with '\n'
track_english.addCue(
new VTTCue(startTime, endTime, content)
);
}
});

function subtitles_xhr(cb) {
var xhr = new XMLHttpRequest;
xhr.open('GET', txt_uri);
xhr.onload = function() {
cb(xhr.response);
};
xhr.send();
}
video{max-height:100vh}
<video video-player src="https://upload.wikimedia.org/wikipedia/commons/transcoded/a/a4/BBH_gravitational_lensing_of_gw150914.webm/BBH_gravitational_lensing_of_gw150914.webm.480p.webm" autoplay controls></video>

关于javascript - VTTCue 未检测到 Javascript 中视频字幕的“\n”,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50939541/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com