Skip to content

JS 时间转换为字幕时间,生成字幕

Published: at 04:10 PMSuggest Changes

记一下格式化时间算法

function srtTimestamp(params) {
  let milliseconds = params;
  let seconds = Math.floor(milliseconds / 1000);
  let minutes = Math.floor(seconds / 60);
  let hours = Math.floor(minutes / 60);
  milliseconds = milliseconds % 1000;
  seconds = seconds % 60;
  minutes = minutes % 60;
  return (
    (hours < 10 ? '0' : '') +
    hours +
    ':' +
    (minutes < 10 ? '0' : '') +
    minutes +
    ':' +
    (seconds < 10 ? '0' : '') +
    seconds +
    ',' +
    (milliseconds < 100 ? '0' : '') +
    (milliseconds < 10 ? '0' : '') +
    milliseconds
  );
}

function inputToSRT(params, index) {
  return (
    index +
    1 +
    '\r\n' +
    srtTimestamp(params.startTime) +
    ' --> ' +
    srtTimestamp(params.endTime) +
    '\r\n' +
    params.text +
    '\r\n\r\n'
  );
}

Previous Post
Vue 设置 iframe 内容样式
Next Post
JavaScript 异常数据提取算法