gifski.ts 1.2 KB

123456789101112131415161718192021222324252627282930313233343536
  1. import { spawnSync } from 'child_process';
  2. import { closeSync, readFileSync, writeSync } from 'fs';
  3. import * as temp from 'temp';
  4. import { getLogger } from './loggers';
  5. const logger = getLogger('gifski');
  6. export default function (data: ArrayBuffer) {
  7. const outputFilePath = temp.path({suffix: '.gif'});
  8. // temp.track();
  9. try {
  10. const inputFile = temp.openSync();
  11. writeSync(inputFile.fd, Buffer.from(data));
  12. closeSync(inputFile.fd);
  13. logger.info(`saved video file to ${inputFile.path}, starting gif conversion...`);
  14. const args = [
  15. '--fps',
  16. '12.5',
  17. '--quiet',
  18. '--quality',
  19. '80',
  20. '-o',
  21. outputFilePath,
  22. inputFile.path,
  23. ];
  24. logger.info(` gifski ${args.join(' ')}`);
  25. const gifskiInvocation = spawnSync('gifski', args, {encoding: 'utf8', timeout: 90000});
  26. if (gifskiInvocation.stderr) throw Error(gifskiInvocation.stderr);
  27. logger.info(`gif conversion succeeded, file path: ${outputFilePath}`);
  28. return readFileSync(outputFilePath).buffer;
  29. } catch (error) {
  30. logger.error('error converting video to gif' + error ? `message: ${error}` : '');
  31. throw Error('error converting video to gif');
  32. }
  33. }