Skip to content

Commit

Permalink
convert subtitle formatting into webvtt (#323)
Browse files Browse the repository at this point in the history
  • Loading branch information
frenicohansen authored Jan 20, 2025
1 parent 81dfee0 commit 4dc1904
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 0 deletions.
5 changes: 5 additions & 0 deletions chrome/player/SubtitleTrack.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@ export class SubtitleTrack {
} else if (text.trim().split('\n')[0].trim().substr(0, 6) !== 'WEBVTT') {
text = SubtitleUtils.srt2webvtt(text);
}

// sometimes formatting in subtitles are not properly
// converted into webvtt, so we need to convert them manually
text = SubtitleUtils.convertSubtitleFormatting(text)

// eslint-disable-next-line new-cap
const parser = new WebVTT.Parser(window, WebVTT.StringDecoder());
parser.onRegion = (region) => {
Expand Down
9 changes: 9 additions & 0 deletions chrome/player/utils/SubtitleUtils.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -148,4 +148,13 @@ export class SubtitleUtils {
}
return cue;
}

static convertSubtitleFormatting(text) {
return text
.replace(/\{\\([ibu])1\}/g, '<$1>') // convert {\b1}, {\i1}, {\u1} to <b>, <i>, <u>
.replace(/\{\\([ibu])\}/g, '</$1>') // convert {\b}, {\i}, {\u} to </b>, </i>, </u>
.replace(/\{([ibu])\}/g, '<$1>') // convert {b}, {i}, {u} to <b>, <i>, <u>
.replace(/\{\/([ibu])\}/g, '</$1>') // convert {/b}, {/i}, {/u} to </b>, </i>, </u>
.replace(/(\r\n|\n)\{\\an8\}/g, ' line:5%\n'); // handle top positioning
}
}

0 comments on commit 4dc1904

Please sign in to comment.