pietrop / slate-transcript-editor

A React component to make correcting automated transcriptions of audio and video easier and faster. Using the SlateJs editor.
https://pietrop.github.io/slate-transcript-editor
Other
75 stars 33 forks source link

better way to format seconds #34

Open pietrop opened 3 years ago

pietrop commented 3 years ago

As noted in this fork clowdr-app/slate-transcript-editor - src/util/export-adapters/subtitles-generator/compose-subtitles/util/format-seconds.js there might be a better way to format seconds using d3-format

- const formatSeconds = seconds => new Date(seconds.toFixed(3) * 1000).toISOString().substr(11, 12);
+
+ import * as d3 from "d3-format"
+
+ function formatSeconds(totalSeconds) {
+     const hours = Math.floor(totalSeconds / 3600);
+     const minuteSeconds = totalSeconds - hours * 3600;
+     const minutes = Math.floor(minuteSeconds / 60);
+    const secondSeconds = minuteSeconds - minutes * 60;
+    const seconds = Math.floor(secondSeconds);
+    const millis = (secondSeconds - seconds) * 1000;
+
+    const hoursPart = d3.format("02d")(hours);
+    const minutesPart = d3.format("02d")(minutes);
+    const secondsPart = d3.format("02d")(seconds);
+    const millisPart = d3.format("03d")(millis);
+
+    return `${hoursPart}:${minutesPart}:${secondsPart},${millisPart}`;
+}

or could use this function pietrop/slate-transcript-editor/src/util/timecode-converter/src/secondsToTimecode.js#L25 ?

However keep an eye out for the purpose of the timecode, eg if it's for srt captions files, milliseconds might need to be 3 digits separated by a comma(?)