|
| 1 | +/** |
| 2 | + *Subtitles File Parser Unit |
| 3 | + * |
| 4 | + * Licensed Under MIT License |
| 5 | + * Transforms an ASS file into a readable array for the CommentCoreLibrary |
| 6 | + * |
| 7 | + * With reference from (https://github.com/spiegeleixxl/html5-ass-subtitles/) |
| 8 | +***/ |
| 9 | + |
| 10 | +function parseASS(input, config) { |
| 11 | + var state = 0; |
| 12 | + var linecounter = 0; |
| 13 | + var resizeFactor = 1; |
| 14 | + var captions = {}; |
| 15 | + // initializing captions array |
| 16 | + var info = {}; |
| 17 | + var styles = {}; |
| 18 | + var timeline = []; |
| 19 | + // split the assfile by newlines. |
| 20 | + var assfile = input.split('\n'); |
| 21 | + var comments=""; |
| 22 | + |
| 23 | + for (var linecount=0; linecount < assfile.length; linecount++){ |
| 24 | + if (assfile[linecount].indexOf('[Script Info]') === 0){ |
| 25 | + state = 1; |
| 26 | + } else if (assfile[linecount].indexOf('[V4+ Styles]')=== 0){ |
| 27 | + state = 2; |
| 28 | + console.log(config); |
| 29 | + if ((info['PlayResX'] || info['PlayResY']) && (config.width || config.height)){ |
| 30 | + resizeFactor = parseInt(info['PlayResY']) / config.height; |
| 31 | + console.log(resizeFactor); |
| 32 | + } |
| 33 | + } else if (assfile[linecount].indexOf('[Events]')=== 0){ |
| 34 | + state = 3; |
| 35 | + } else if (state == 1){ |
| 36 | + if (assfile[linecount].indexOf(';') !== 0){ |
| 37 | + if (assfile[linecount].indexOf(':') > -1){ |
| 38 | + var infoLine = assfile[linecount].split(':'); |
| 39 | + info[infoLine[0].trim()] = infoLine[1].trim(); |
| 40 | + } |
| 41 | + } else { |
| 42 | + comments = comments + assfile[linecount] + "\n"; |
| 43 | + } |
| 44 | + } else if (state == 2){ |
| 45 | + if (assfile[linecount].indexOf('Style:')=== 0){ |
| 46 | + var styleparts = assfile[linecount].split(':')[1].split(','); |
| 47 | + var stylename = styleparts[0].trim(); |
| 48 | + |
| 49 | + var style = {}; |
| 50 | + style['stylename'] = styleparts[0].trim(); |
| 51 | + style['fontname'] = styleparts[1]; |
| 52 | + style['fontsize'] = parseFloat(styleparts[2]) * resizeFactor; |
| 53 | + style['color1'] = styleparts[3].replace(/^&H00/, "#"); |
| 54 | + style['color2'] = styleparts[4].replace(/^&H00/, "#"); |
| 55 | + style['border-color'] = styleparts[5]; |
| 56 | + style['shadow-color'] = styleparts[6]; |
| 57 | + style['bold'] = styleparts[7]; |
| 58 | + style['italic'] = styleparts[8]; |
| 59 | + style['underline'] = styleparts[9]; |
| 60 | + style['strikeout'] = styleparts[10]; |
| 61 | + style['fontscalex'] = styleparts[11]; |
| 62 | + style['fontscaley'] = styleparts[12]; |
| 63 | + style['spacing'] = styleparts[13]; |
| 64 | + style['angle'] = styleparts[14]; |
| 65 | + |
| 66 | + style['borderstyle'] = styleparts[15]; |
| 67 | + style['outline'] = parseFloat(styleparts[16])*resizeFactor; |
| 68 | + style['shadow'] = parseFloat(styleparts[17])*resizeFactor; |
| 69 | + style['alignment'] = styleparts[18]; |
| 70 | + style['marginleft'] = parseFloat(styleparts[19])*resizeFactor; |
| 71 | + style['marginright'] = parseFloat(styleparts[20])*resizeFactor; |
| 72 | + style['marginvertical'] = parseFloat(styleparts[21])*resizeFactor; |
| 73 | + style['encoding'] = styleparts[22]; |
| 74 | + |
| 75 | + styles[style.stylename] = style; |
| 76 | + } |
| 77 | + } else if (state == 3){ |
| 78 | + if (assfile[linecount].indexOf('Dialogue:')=== 0){ |
| 79 | + var lineparts = assfile[linecount].split(','); |
| 80 | + var st = lineparts[1].trim().split(':'); |
| 81 | + var et = lineparts[2].trim().split(':'); |
| 82 | + var stime = st[0]*60*60 + st[1]*60 + parseFloat(st[2]); |
| 83 | + var etime = et[0]*60*60 + et[1]*60 + parseFloat(et[2]); |
| 84 | + var comment = { |
| 85 | + 'stime' : Math.round(stime * 1000), |
| 86 | + 'dur': Math.round((etime - stime) * 1000), |
| 87 | + 'ttl': Math.round((etime - stime) * 1000), |
| 88 | + 'mode': 4, |
| 89 | + 'size': styles[lineparts[3]]['fontsize'] * resizeFactor, |
| 90 | + 'color': styles[lineparts[3]]['color1'], |
| 91 | + 'font': styles[lineparts[3]]['fontname'], |
| 92 | + 'margin': styles[lineparts[3]]['marginleft'] + "px " + styles[lineparts[3]]['marginvertical'] + "px " + styles[lineparts[3]]['marginright'] + "px " + styles[lineparts[3]]['marginvertical'] + "px", |
| 93 | + 'style' : lineparts[3], |
| 94 | + 'pool': 15, |
| 95 | + 'actor' : lineparts[4], |
| 96 | + 'marginleft' : lineparts[5], |
| 97 | + 'marginright' : lineparts[6], |
| 98 | + 'marginvertical' : lineparts[7], |
| 99 | + 'effect' : lineparts[8] |
| 100 | + } |
| 101 | + for (var z = 0; z < 9; z++) { lineparts.shift(); } |
| 102 | + comment.text = lineparts.join(','); |
| 103 | + comment.text = comment.text.replace(/{[^}]+}/gi,""); |
| 104 | + /* |
| 105 | + captions['lines'][linecounter]['asstags'] = ""; |
| 106 | + var matches = captions['lines'][linecounter]['text'].match(/{[^}]+}/g); |
| 107 | + for (var z in matches){ |
| 108 | + if (matches[z].startsWith("{\\")){ |
| 109 | + captions['lines'][linecounter]['asstags'] = captions['lines'][linecounter]['asstags'] + matches[z] + " "; |
| 110 | + } |
| 111 | + } |
| 112 | + captions['lines'][linecounter]['text'] = captions['lines'][linecounter]['text'].replace(/{[^}]+}/gi,""); |
| 113 | + */ |
| 114 | + timeline.push(comment); |
| 115 | + linecounter = linecounter+1; |
| 116 | + } |
| 117 | + } |
| 118 | + } |
| 119 | + console.log(styles); |
| 120 | + return timeline; |
| 121 | +} |
0 commit comments