const fs = require('fs');
const cp = require('child_process');
const im = require('imagemagick');
const readline = require('readline');
const commandLineArgs = require('command-line-args');

const fullWidth = 1920;
const fullHeight = 1080;
const outDir = './clips';

const optionDefinitions = [
  { name: 'delimiter', alias: 'd', type: String },
  { name: 'font', alias: 'f', type: String },
  { name: 'justify', alias: 'j', type: String },
  { name: 'music', alias: 'm', type: String },
  { name: 'size', alias: 's', type: Number },
  { name: 'text', alias: 't', type: String },
];
const options = commandLineArgs(optionDefinitions);

var delimiter = options.hasOwnProperty('delimiter') ? options.delimiter : '===';
var font = options.font;
var justify = options.hasOwnProperty('justify') ? options.justify : 'c';
var music = options.music;
var fontsize = options.hasOwnProperty('size') ? options.size : '48';
var text = options.text;

if (!options.hasOwnProperty('font')
 || !options.hasOwnProperty('music')
 || !options.hasOwnProperty('text')) {
  // Can't do anything useful without input
  return;
}

var proc = cp.spawnSync('/usr/bin/soxi', [ '-D', music ]);
var time = parseFloat(proc.stdout.toString().trim(), 10);

var lineReader = readline.createInterface({
  terminal: false,
  input: fs.createReadStream(text)
});
var screens = [];
var screenful = [];
lineReader.on('line', function (line) {
  if (line == delimiter) {
    screens.push(screenful);
    screenful = [];
  } else {
    screenful.push(line);
  }
});
lineReader.on('close', function() {
  var pages = screens.length;
  var timePerPage = time / pages;
  var images = [];

  console.log('Runs for ' + time + 's, or ' + timePerPage + 's per page.');
  for (var page = 0; page < pages; page++) {
    var contents = screens[page].join('\n');
    var digits = screens.toString().length;
    var imageName = 'credit-' + ('0000' + (page + 1)).slice(-digits) + '.png';
    
    images.push(imageName);
    im.convert([
        imageFile,
        '-resize', fullWidth + 'x' + fullHeight,
        '-gravity', 'Center',
        '-background', '#000020',
        '-opaque', '#F0F0B0',
        '-extent', fullWidth + 'x' + fullHeight,
        imageName
      ], function (err, output) {
      if (err) {
        console.log(err);
      }
      im.convert([
        imageName,
        '-fill', foreground,
        '-font', fontFile,
        '-pointsize', 48,
        '-annotate', geo,
        contents,
        'text_' + imageName
      ], function (err, output) {
      }
    });
  });
}

@@ -1869,16 +1869,18 @@
 Name = '
+./
 credit-'
@@ -1970,121 +1970,50 @@
     
-im.convert(%5B%0A        imageFile,%0A        '-resize', fullWidth + 'x' + fullHeight,%0A        '-gravity', 'Center',%0A  
+console.log(imageName);%0A%0A    im.convert(%5B%0A
     
@@ -2050,18 +2050,14 @@
     
-  '-opaque
+'-fill
 ', '
@@ -2076,18 +2076,35 @@
     
-  '-extent
+'-font', font,%0A      '-size
 ', f
@@ -2142,258 +2142,71 @@
     
-  imageName%0A      %5D, function (err, output) %7B%0A      if (err) %7B%0A        console.log(err);%0A      %7D%0A      im.convert(%5B%0A        imageName,%0A        '-fill', foreground,%0A        '-font', fontFile,%0A        '-pointsize', 48,%0A        '-annotate', geo,%0A       
+'-pointsize', 192,%0A      '-gravity', 'Center',%0A      'label:' +
  con
@@ -2221,20 +2221,8 @@
     
-   'text_' +
  ima
@@ -2228,18 +2228,16 @@
 ageName%0A
-  
     %5D, f
@@ -2256,24 +2256,67 @@
 , output) %7B%0A
+      if (err) %7B%0A        console.log(err);%0A
       %7D%0A    
@@ -2326,9 +2326,9 @@
 %0A  %7D
-);
 %0A%7D
+);
 %0A

Fix image output