Skip to content

Commit

Permalink
🐛 Fix exporting webvtt with zero length tokens
Browse files Browse the repository at this point in the history
  • Loading branch information
phlmn committed Dec 29, 2023
1 parent 33b5cbc commit a08f701
Showing 1 changed file with 49 additions and 16 deletions.
65 changes: 49 additions & 16 deletions frontend/src/utils/export/webvtt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,39 @@ function atomToString(item: Text, includeWordTimings: boolean): string {
}
}

function createVttCue({
cueStart,
cueEnd,
cuePayload,
includeSpeakerNames,
paragraph,
speakerNames,
}: {
cueStart: number;
cueEnd: number;
cuePayload: string;
paragraph: Paragraph;
includeSpeakerNames: boolean;
speakerNames: Record<string, string>;
}) {
if (cueStart >= cueEnd) {
console.log(
`found cueStart=${cueStart} that is not before cueEnd=${cueEnd}, fixing the end to be behind cueStart`,
);
cueEnd = cueStart + 0.02;
}

return new VttCue({
startTime: cueStart,
endTime: cueEnd,
payload:
(includeSpeakerNames && paragraph.speaker
? `<v ${escapeVttString(getSpeakerName(paragraph.speaker, speakerNames))}>`
: '') + cuePayload,
payloadEscaped: true,
});
}

export function canGenerateVtt(paras: Paragraph[] | undefined): {
canGenerate: boolean;
reason: string;
Expand Down Expand Up @@ -62,16 +95,16 @@ function paragraphToCues(
cueLength + atom.text.length > maxLineLength
) {
cues.push(
new VttCue({
startTime: cueStart,
endTime: cueEnd,
payload:
(includeSpeakerNames && paragraph.speaker
? `<v ${escapeVttString(getSpeakerName(paragraph.speaker, speaker_names))}>`
: '') + cuePayload,
payloadEscaped: true,
createVttCue({
cueStart,
cueEnd,
cuePayload,
includeSpeakerNames,
paragraph,
speakerNames: speaker_names,
}),
);

cuePayload = '';
cueLength = 0;
cueStart = null;
Expand All @@ -96,15 +129,15 @@ function paragraphToCues(
'Paragraph contains no timings, cannot generate cue(s). Make sure to only call this function if `canGenerateVtt` returns true',
);
}

cues.push(
new VttCue({
startTime: cueStart,
endTime: cueEnd,
payload:
(includeSpeakerNames && paragraph.speaker
? `<v ${escapeVttString(getSpeakerName(paragraph.speaker, speaker_names))}>`
: '') + cuePayload,
payloadEscaped: true,
createVttCue({
cueStart,
cueEnd,
cuePayload,
includeSpeakerNames,
paragraph,
speakerNames: speaker_names,
}),
);
}
Expand Down

0 comments on commit a08f701

Please sign in to comment.