Skip to content

Qiming-Liu/srtparse.js

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

41 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

A javascript srt text parser and player.

Install

$ npm install srtparsejs

or

$ yarn add srtparsejs

parse Example

import * as srtparsejs from "srtparsejs"; // cjs
const srtparsejs = require('srtparsejs'); // esm

let srt = `
1
00:00:11,544 --> 00:00:12,682
Hello
`
let parsed = srtparsejs.parse(srt)
console.log(parsed)
/*
[{
    id: '1',
    startTime: '00:00:11,544',
    endTime: '00:00:12,682',
    text: 'Hello' 
}]
*/

let srtString = srtparsejs.toSrt(parsed)
console.log(srtString)
/*
1
00:00:11,544 --> 00:00:12,682
Hello
`
*/

srtPlayer Usage

// Display the text of each subtitle by time
let srtPlayer = srtparsejs.setPlayer(parsed, text=>{
    console.log(text)
})

// Move player to this time
srtPlayer.update("00:00:11,544") 

// Get subtitle end time
console.log(srtPlayer.getEndTime())
//control the player
let pause = false

//srt player check interval
let interval = 100

//parse srt
let srtArray = srtparsejs.parse(srt)

//start millisecond
let ms = 0

//create player
let player = srtparsejs.setPlayer(srtArray, text => {
    //here to display the text
    console.log(text)
})

//update srt player time
setInterval(() => {
    if (pause) return;
    ms += interval
    //update to specific time
    player.update(srtparsejs.toTime(ms))
}, interval)