parseVideoTime.ts 589 B

1234567891011121314151617
  1. /** Parses a video time string in the format `[hh:m]m:ss` to the equivalent number of seconds - returns 0 if input couldn't be parsed */
  2. function parseVideoTime(videoTime: string) {
  3. const matches = /^((\d{1,2}):)?(\d{1,2}):(\d{2})$/.exec(videoTime);
  4. if(!matches)
  5. return 0;
  6. const [, , hrs, min, sec] = matches as unknown as [string, string | undefined, string | undefined, string, string];
  7. let finalTime = 0;
  8. if(hrs)
  9. finalTime += Number(hrs) * 60 * 60;
  10. finalTime += Number(min) * 60 + Number(sec);
  11. return isNaN(finalTime) ? 0 : finalTime;
  12. }
  13. void [parseVideoTime];