У меня есть большой файл SRT (субтитры), который я пытаюсь преобразовать в JSON, но мое регулярное выражение работает неправильно.
Мое выражение:
^(\d+)\r?\n(\d{1,2}:\d{1,2}:\d{1,2}([.,]\d{1,3})?)\s*\-\-\>\s*(\d{1,2}:\d{1,2}:\d{1,2}([.,]\d{1,3})?)\r?\n([\s\S]*)(\r?\n)*$
Вот пример моего файла srt, каждый субтитр следует той же схеме.
1
00:00:11,636 --> 00:00:13,221
Josh communicated but
2
00:00:13,221 --> 00:00:16,850
it's also the belief that
we never knew the severity
мой файл javascript
const fs = require('fs');
function parse(content, options) {
var captions = [];
var parts = content.split(/\r?\n\s+\r?\n/g);
for (var i = 0; i < parts.length; i++) {
var regex = /^(\d+)\r?\n(\d{1,2}:\d{1,2}:\d{1,2}([.,]\d{1,3})?)\s*\-\-\>\s*(\d{1,2}:\d{1,2}:\d{1,2}([.,]\d{1,3})?)\r?\n([\s\S]*)(\r?\n)*$/gi;
var match = regex.exec(parts[i]);
if (match) {
var caption = {};
var eol = "\n";
caption.id = parseInt(match[1]);
caption.start = match[2];
caption.end = match[4];
var lines = match[6].split('/\r?\n/');
caption.content = lines.join(eol);
captions.push(caption);
continue;
}
}
return captions;
};
var content = fs.readFileSync('./English-SRT-CC.srt', 'utf8');
var captions = parse(content);
var json = JSON.stringify(captions, " ", 2);
console.log(json);
fs.writeFile("output.json", json, 'utf8', function (err) {
if (err) {
return console.log(err);
}
console.log("JSON file has been saved.");
});
И наконец, вот мой вывод:
{
"id": 1,
"start": "00:00:11,636",
"end": "00:00:13,221",
"content": "Josh communicated but\n\n2\n00:00:13,221 --> 00:00:16,850\n
// cut for shortness, it just continues the rest of the file inside "content"
Мой желаемый результат?
{
"id": 1,
"start": "00:00:11,636",
"end": "00:00:13,221",
"content": "Josh communicated but"
},
{
"id": 2,
"start": "00:00:13,221",
"end": "00:00:16,850",
"content": "it's also the belief that\n we never knew the severity"
}
Спасибо!
Редактировать: regex101