gifski.js 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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. function default_1(data, targetWidth) {
  20. return __awaiter(this, void 0, void 0, function* () {
  21. const outputFilePath = temp.path({ suffix: '.gif' });
  22. temp.track();
  23. try {
  24. const inputFile = temp.openSync();
  25. fs_1.writeSync(inputFile.fd, Buffer.from(data));
  26. fs_1.closeSync(inputFile.fd);
  27. logger.info(`saved video file to ${inputFile.path}, starting gif conversion...`);
  28. const args = [
  29. inputFile.path,
  30. '-o',
  31. outputFilePath,
  32. '--fps',
  33. '12.5',
  34. '--quiet',
  35. '--quality',
  36. '90',
  37. ];
  38. if (typeof (targetWidth) === 'number') {
  39. args.push('--width', roundToEven(targetWidth).toString());
  40. }
  41. logger.info(` gifski ${args.join(' ')}`);
  42. const gifskiSpawn = child_process_1.spawn('gifski', args);
  43. const gifskiResult = new Promise((resolve, reject) => {
  44. const sizeChecker = setInterval(() => {
  45. if (fs_1.existsSync(outputFilePath) && fs_1.statSync(outputFilePath).size > sizeLimit)
  46. gifskiSpawn.kill();
  47. }, 5000);
  48. gifskiSpawn.on('exit', () => {
  49. clearInterval(sizeChecker);
  50. if (!fs_1.existsSync(outputFilePath))
  51. reject('no file was created on exit');
  52. logger.info(`gif conversion succeeded, file path: ${outputFilePath}`);
  53. resolve(fs_1.readFileSync(outputFilePath).buffer);
  54. });
  55. });
  56. const stderr = [];
  57. gifskiSpawn.stderr.on('data', errdata => {
  58. if (!gifskiSpawn.killed)
  59. gifskiSpawn.kill();
  60. stderr.concat(errdata);
  61. });
  62. gifskiSpawn.stderr.on('end', () => {
  63. if (stderr.length !== 0)
  64. throw Error(Buffer.concat(stderr).toString());
  65. });
  66. return yield gifskiResult;
  67. }
  68. catch (error) {
  69. logger.error('error converting video to gif' + error ? `message: ${error}` : '');
  70. throw Error('error converting video to gif');
  71. }
  72. finally {
  73. temp.cleanup();
  74. }
  75. });
  76. }
  77. exports.default = default_1;