gifski.js 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. const child_process_1 = require("child_process");
  4. const fs_1 = require("fs");
  5. const temp = require("temp");
  6. const loggers_1 = require("./loggers");
  7. const logger = loggers_1.getLogger('gifski');
  8. function default_1(data, targetWidth) {
  9. const outputFilePath = temp.path({ suffix: '.gif' });
  10. // temp.track();
  11. try {
  12. const inputFile = temp.openSync();
  13. fs_1.writeSync(inputFile.fd, Buffer.from(data));
  14. fs_1.closeSync(inputFile.fd);
  15. logger.info(`saved video file to ${inputFile.path}, starting gif conversion...`);
  16. const args = [
  17. inputFile.path,
  18. '-o',
  19. outputFilePath,
  20. '--fps',
  21. '12.5',
  22. '--quiet',
  23. '--quality',
  24. '90',
  25. ];
  26. if (typeof (targetWidth) === 'number') {
  27. args.push('--width', (Math.ceil(targetWidth / 2) * 2).toString());
  28. }
  29. logger.info(` gifski ${args.join(' ')}`);
  30. const gifskiInvocation = child_process_1.spawnSync('gifski', args, { encoding: 'utf8', timeout: 90000 });
  31. if (gifskiInvocation.stderr)
  32. throw Error(gifskiInvocation.stderr);
  33. logger.info(`gif conversion succeeded, file path: ${outputFilePath}`);
  34. return fs_1.readFileSync(outputFilePath).buffer;
  35. }
  36. catch (error) {
  37. logger.error('error converting video to gif' + error ? `message: ${error}` : '');
  38. throw Error('error converting video to gif');
  39. }
  40. }
  41. exports.default = default_1;