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 (将#修改为@)

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...