gifski.js 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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 loggers_1 = require("./loggers");
  15. const logger = (0, loggers_1.getLogger)('gifski');
  16. const sizeLimit = 8000000;
  17. const roundToEven = (n) => Math.ceil(n / 2) * 2;
  18. exports.default = (inputFilePath, targetWidth) => __awaiter(void 0, void 0, void 0, function* () {
  19. const outputFilePath = inputFilePath.replace(/(?:\.[^.]*)?$/, '.gif');
  20. if ((0, fs_1.existsSync)(outputFilePath) && (0, fs_1.statSync)(outputFilePath).size > 0) {
  21. logger.info(`converted gif exists, skipping conversion...`);
  22. return outputFilePath;
  23. }
  24. try {
  25. const args = [
  26. inputFilePath,
  27. '-o',
  28. outputFilePath,
  29. '--fps',
  30. '11.988',
  31. '--quiet',
  32. '--quality',
  33. '95',
  34. ];
  35. if (typeof (targetWidth) === 'number') {
  36. args.push('--width', roundToEven(targetWidth).toString());
  37. }
  38. logger.info(` gifski ${args.join(' ')}`);
  39. const gifskiSpawn = (0, child_process_1.spawn)('gifski', args);
  40. const gifskiResult = new Promise((resolve, reject) => {
  41. const sizeChecker = setInterval(() => {
  42. if ((0, fs_1.existsSync)(outputFilePath) && (0, fs_1.statSync)(outputFilePath).size > sizeLimit)
  43. gifskiSpawn.kill();
  44. }, 5000);
  45. gifskiSpawn.on('exit', () => {
  46. clearInterval(sizeChecker);
  47. if (!(0, fs_1.existsSync)(outputFilePath) || (0, fs_1.statSync)(outputFilePath).size === 0)
  48. return reject('no file was created on exit');
  49. logger.info(`gif conversion succeeded, file path: ${outputFilePath}`);
  50. resolve(outputFilePath);
  51. });
  52. });
  53. const stderr = [];
  54. gifskiSpawn.stderr.on('data', errdata => stderr.push(errdata));
  55. gifskiSpawn.stderr.on('end', () => {
  56. if (stderr.length !== 0) {
  57. if (!gifskiSpawn.killed)
  58. gifskiSpawn.kill();
  59. throw Error(Buffer.concat(stderr).toString());
  60. }
  61. });
  62. return yield gifskiResult;
  63. }
  64. catch (error) {
  65. logger.error(`error converting video to gif ${error ? `message: ${error}` : ''}`);
  66. throw Error('error converting video to gif');
  67. }
  68. });