UNPKG

jianying

Version:

A CLI Tools for parse JianyingPro Projects.

33 lines (25 loc) 1.03 kB
const fs = require('fs') const path = require('path') const pad = num => (num < 10 ? '0' : '') + num const parseTime = time => { time = time / 1000000 const h = Math.floor(time / 3600) const m = Math.floor((time - h * 3600) / 60) const s = Math.floor(time - h * 3600 - m * 60) const ms = time - Math.floor(time) return `${pad(h)}:${pad(m)}:${pad(s)},${ms.toFixed(3).substr(2)}` } module.exports = name => { if (typeof name !== 'string') { throw new TypeError(`Expected a string, got ${typeof name}`) } const filename = path.resolve(name) const content = fs.readFileSync(filename, 'utf8') const { tracks, materials } = JSON.parse(content) const { segments } = tracks.find(t => t.type === 'text' && t.segments.length > 10) const srt = segments.map((item, index) => `${index} ${parseTime(item.target_timerange.start)} --> ${parseTime(item.target_timerange.start + item.target_timerange.duration)} ${materials.texts.find(t => t.id === item.material_id).content} `).join('\n') console.log(srt) }