微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Javascript 将 WEBTT 文件转换为 json

如何解决Javascript 将 WEBTT 文件转换为 json

文件.vtt:

WEBVTT

00:00:00.039 --> 00:00:25.968
VINCENZO Cassano!

00:00:26.044 --> 00:00:26.961
damn it.

00:01:23.434 --> 00:01:24.894
Mr. Vincenzo Cassano.

00:01:24.978 --> 00:01:27.814
You're under arrest
for the murder of Mr. Oh Jeong-bae.

00:01:43.913 --> 00:01:44.956
Hands up,00:01:45.540 --> 00:01:46.708
or I'll fire.

00:01:51.504 --> 00:01:52.964
I didn't do it.

转成json:

[
   {
      "from":"00:00:00.039","to":"00:00:25.968","timeString":"00:00:00.039 --> 00:00:25.968","text":"VINCENZO Cassano!"
   },....,{
      "from":"00:01:24.978","to":"00:01:27.814","timeString":"00:01:24.978 --> 00:01:27.814","text":"You're under arrest\nfor the murder of Mr. Oh Jeong-bae."<- Multi line,i assume it's a \n??
   }
]

结果:

enter image description here

我有一个 .vtt 文件,对于字幕,我必须确保创建一个如上所示的 json 数组,还要考虑多行。

我写了这个,它应该删除前导 WEBVTT 和双空格,但我无法删除前导空格,如下图所示,索引 0(也许这个问题是我解决的通过添加 .replace('\n',''))

来修复它
const v = enc.decode(text).replace('WEBVTT','').replace(/[\r\n]{2,}/g,'\n').replace('\n','');
const lines = v.split('\n');
lines.map((el,i) => console.log(`${i} - ${el}`));

const test = new RegExp('\\b(\\d{2}:\\d{2}:\\d{2})\\.(\\d{3})\\b').test(el);
<-- expression to check if the string is of the type 00:00:00.039

enter image description here

你能帮我一把吗?

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。