ass.ts 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. 'use strict';
  2. import xml2js = require('xml2js');
  3. /**
  4. * Converts an input buffer to a SubStation Alpha subtitle.
  5. */
  6. export default function(input: string|Buffer, done: (err: Error, subtitle?: string) => void)
  7. {
  8. xml2js.parseString(input.toString(), {
  9. explicitArray: false,
  10. explicitRoot: false
  11. }, (err: Error, xml: ISubtitle) =>
  12. {
  13. if (err)
  14. {
  15. return done(err);
  16. }
  17. try
  18. {
  19. done(null, script(xml) + '\n' +
  20. style(xml.styles) + '\n' +
  21. event(xml.events));
  22. } catch (err)
  23. {
  24. done(err);
  25. }
  26. });
  27. }
  28. /**
  29. * Converts the event block.
  30. */
  31. function event(block: ISubtitleEvent): string
  32. {
  33. var format = 'Layer,Start,End,Style,Name,MarginL,MarginR,MarginV,Effect,Text';
  34. return '[Events]\n' +
  35. 'Format: ' + format + '\n' + [].concat(block.event).map(style => ('Dialogue: 0,' +
  36. style.$.start + ',' +
  37. style.$.end + ',' +
  38. style.$.style + ',' +
  39. style.$.name + ',' +
  40. style.$.margin_l + ',' +
  41. style.$.margin_r + ',' +
  42. style.$.margin_v + ',' +
  43. style.$.effect + ',' +
  44. style.$.text)).join('\n') + '\n';
  45. }
  46. /**
  47. * Converts the script block.
  48. */
  49. function script(block: ISubtitle): string
  50. {
  51. return '[Script Info]\n' +
  52. 'Title: ' + block.$.title + '\n' +
  53. 'ScriptType: v4.00+\n' +
  54. 'WrapStyle: ' + block.$.wrap_style + '\n' +
  55. 'PlayResX: ' + block.$.play_res_x + '\n' +
  56. 'PlayResY: ' + block.$.play_res_y + '\n' +
  57. 'Subtitle ID: ' + block.$.id + '\n' +
  58. 'Language: ' + block.$.lang_string + '\n' +
  59. 'Created: ' + block.$.created + '\n';
  60. }
  61. /**
  62. * Converts the style block.
  63. */
  64. function style(block: ISubtitleStyle): string
  65. {
  66. var format = 'Name,Fontname,Fontsize,PrimaryColour,SecondaryColour,' +
  67. 'OutlineColour,BackColour,Bold,Italic,Underline,StrikeOut,ScaleX,' +
  68. 'ScaleY,Spacing,Angle,BorderStyle,Outline,Shadow,Alignment,' +
  69. 'MarginL,MarginR,MarginV,Encoding';
  70. return '[V4+ Styles]\n' +
  71. 'Format: ' + format + '\n' + [].concat(block.style).map(style => 'Style: ' +
  72. style.$.name + ',' +
  73. style.$.font_name + ',' +
  74. style.$.font_size + ',' +
  75. style.$.primary_colour + ',' +
  76. style.$.secondary_colour + ',' +
  77. style.$.outline_colour + ',' +
  78. style.$.back_colour + ',' +
  79. style.$.bold + ',' +
  80. style.$.italic + ',' +
  81. style.$.underline + ',' +
  82. style.$.strikeout + ',' +
  83. style.$.scale_x + ',' +
  84. style.$.scale_y + ',' +
  85. style.$.spacing + ',' +
  86. style.$.angle + ',' +
  87. style.$.border_style + ',' +
  88. style.$.outline + ',' +
  89. style.$.shadow + ',' +
  90. style.$.alignment + ',' +
  91. style.$.margin_l + ',' +
  92. style.$.margin_r + ',' +
  93. style.$.margin_v + ',' +
  94. style.$.encoding).join('\n') + '\n';
  95. }