gifski.js 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. "use strict";
  2. var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
  3. function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
  4. return new (P || (P = Promise))(function (resolve, reject) {
  5. function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
  6. function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
  7. function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
  8. step((generator = generator.apply(thisArg, _arguments || [])).next());
  9. });
  10. };
  11. Object.defineProperty(exports, "__esModule", { value: true });
  12. const child_process_1 = require("child_process");
  13. const fs_1 = require("fs");
  14. const temp = require("temp");
  15. const loggers_1 = require("./loggers");
  16. const logger = loggers_1.getLogger('gifski');
  17. const sizeLimit = 10 * Math.pow(2, 20);
  18. const roundToEven = (n) => Math.ceil(n / 2) * 2;
  19. exports.default = (data, targetWidth) => __awaiter(void 0, void 0, void 0, function* () {
  20. const outputFilePath = temp.path({ suffix: '.gif' });
  21. temp.track();
  22. try {
  23. const inputFile = temp.openSync();
  24. fs_1.writeSync(inputFile.fd, Buffer.from(data));
  25. fs_1.closeSync(inputFile.fd);
  26. logger.info(`saved video file to ${inputFile.path}, starting gif conversion...`);
  27. const args = [
  28. inputFile.path,
  29. '-o',
  30. outputFilePath,
  31. '--fps',
  32. '12.5',
  33. '--quiet',
  34. '--quality',
  35. '90',
  36. ];
  37. if (typeof (targetWidth) === 'number') {
  38. args.push('--width', roundToEven(targetWidth).toString());
  39. }
  40. logger.info(` gifski ${args.join(' ')}`);
  41. const gifskiSpawn = child_process_1.spawn('gifski', args);
  42. const gifskiResult = new Promise((resolve, reject) => {
  43. const sizeChecker = setInterval(() => {
  44. if (fs_1.existsSync(outputFilePath) && fs_1.statSync(outputFilePath).size > sizeLimit)
  45. gifskiSpawn.kill();
  46. }, 5000);
  47. gifskiSpawn.on('exit', () => {
  48. clearInterval(sizeChecker);
  49. if (!fs_1.existsSync(outputFilePath) || fs_1.statSync(outputFilePath).size === 0)
  50. return reject('no file was created on exit');
  51. logger.info(`gif conversion succeeded, file path: ${outputFilePath}`);
  52. resolve(fs_1.readFileSync(outputFilePath).buffer);
  53. });
  54. });
  55. const stderr = [];
  56. gifskiSpawn.stderr.on('data', errdata => stderr.push(errdata));
  57. gifskiSpawn.stderr.on('end', () => {
  58. if (stderr.length !== 0) {
  59. if (!gifskiSpawn.killed)
  60. gifskiSpawn.kill();
  61. throw Error(Buffer.concat(stderr).toString());
  62. }
  63. });
  64. return yield gifskiResult;
  65. }
  66. catch (error) {
  67. logger.error(`error converting video to gif ${error ? `message: ${error}` : ''}`);
  68. throw Error('error converting video to gif');
  69. }
  70. finally {
  71. temp.cleanup();
  72. }
  73. });