Cod*_*Man 2 javascript jquery srt
我有这样的输入:
start: 0.21 | duration: 0.30 | text: Subtitle Text 1 
start: 0.32 | duration: 0.52 | text: Subtitle Text 2 
这个输入需要转换成SRT格式,所以就变成了这样:
1
00:00:00,210 --> 00:00:00,300
Subtitle Text 1
2
00:00:00,320 --> 00:00:00,520
Subtitle Text 2
JS:
function formatMilliseconds($milliseconds) {
    $seconds = Math.floor($milliseconds / 1000);
    $minutes = Math.floor($seconds / 60);
    $hours = Math.floor($minutes / 60);
    $milliseconds = $milliseconds % 1000;
    $seconds = $seconds % 60;
    $minutes = $minutes % 60;
console.log( $hours, $minutes, $seconds, $milliseconds); // 0 0 0 0.21
}
格式毫秒(0.21)
首先,您需要将输入格式转换为可处理的格式,类似于
var subtitles = [
{
  start: 0.21,
  end: 0.3,
  text: "Subtitle Text 1"
},
{
  start: 0.32,
  end: 0.52,
  text: "Subtitle Text 2"
}
];
请注意,我在这里选择了“结束”而不是“持续时间”,因为在我看来,“持续时间”意味着字幕在开始+持续时间(0.21 + 0.3 = 0.51)秒处结束。
格式化函数几乎是正确的,但需要以毫秒形式提供输入值,而不是十进制秒值。对于输出,您还需要用零填充小时/分钟/秒。您最好为此使用辅助函数,您可以在其中指定目标字符串长度,但对于本示例,我只是内联完成的:
var subtitles = [
{
  start: 0.21,
  end: 0.3,
  text: "Subtitle Text 1"
},
{
  start: 0.32,
  end: 0.52,
  text: "Subtitle Text 2"
}
];
| 归档时间: | 
 | 
| 查看次数: | 2167 次 | 
| 最近记录: |