gifski.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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. const safeStatSync = (path) => !fs_1.existsSync(path) ? null : fs_1.statSync(path);
  20. const isEmptyNullable = (path) => !fs_1.existsSync(path) ? null : fs_1.statSync(path).size === 0;
  21. function default_1(data, targetWidth) {
  22. return __awaiter(this, void 0, void 0, function* () {
  23. const outputFilePath = temp.path({ suffix: '.gif' });
  24. temp.track();
  25. try {
  26. const inputFile = temp.openSync();
  27. fs_1.writeSync(inputFile.fd, Buffer.from(data));
  28. fs_1.closeSync(inputFile.fd);
  29. child_process_1.spawnSync('ffmpeg', [
  30. '-i',
  31. inputFile.path,
  32. '-c:a', 'copy',
  33. '-vn',
  34. inputFile.path + '.mka',
  35. ]);
  36. switch (isEmptyNullable(inputFile.path + '.mka')) {
  37. case true:
  38. fs_1.unlinkSync(inputFile.path + '.mka');
  39. break;
  40. case false: logger.info(`extracted audio to ${inputFile.path + '.mka'}`);
  41. }
  42. logger.info(`saved video file to ${inputFile.path}, starting gif conversion...`);
  43. const args = [
  44. inputFile.path,
  45. '-o',
  46. outputFilePath,
  47. '--fps',
  48. '12.5',
  49. '--quiet',
  50. '--quality',
  51. '90',
  52. ];
  53. if (typeof (targetWidth) === 'number') {
  54. args.push('--width', roundToEven(targetWidth).toString());
  55. }
  56. logger.info(` gifski ${args.join(' ')}`);
  57. const gifskiSpawn = child_process_1.spawn('gifski', args);
  58. const gifskiResult = new Promise((resolve, reject) => {
  59. const sizeChecker = setInterval(() => {
  60. var _a;
  61. if (((_a = safeStatSync(outputFilePath)) === null || _a === void 0 ? void 0 : _a.size) > sizeLimit)
  62. gifskiSpawn.kill();
  63. }, 5000);
  64. gifskiSpawn.on('exit', () => {
  65. clearInterval(sizeChecker);
  66. if (!fs_1.existsSync(outputFilePath))
  67. reject('no file was created on exit');
  68. logger.info('gif conversion succeeded, remuxing to mkv...');
  69. child_process_1.spawnSync('ffmpeg', [
  70. '-i',
  71. outputFilePath,
  72. ...fs_1.existsSync(inputFile.path + '.mka') ? ['-i', inputFile.path + '.mka'] : [],
  73. '-c', 'copy',
  74. outputFilePath + '.mkv',
  75. ]);
  76. if (isEmptyNullable(outputFilePath + '.mkv'))
  77. reject('remux to mkv failed');
  78. logger.info(`mkv remuxing succeeded, file path: ${outputFilePath}.mkv`);
  79. resolve(fs_1.readFileSync(outputFilePath + '.mkv').buffer);
  80. });
  81. });
  82. const stderr = [];
  83. gifskiSpawn.stderr.on('data', errdata => {
  84. if (!gifskiSpawn.killed)
  85. gifskiSpawn.kill();
  86. stderr.concat(errdata);
  87. });
  88. gifskiSpawn.stderr.on('end', () => {
  89. if (stderr.length !== 0)
  90. throw Error(Buffer.concat(stderr).toString());
  91. });
  92. return yield gifskiResult;
  93. }
  94. catch (error) {
  95. logger.error('error converting video to gif' + error ? `message: ${error}` : '');
  96. throw Error('error converting video to gif');
  97. }
  98. finally {
  99. temp.cleanup();
  100. }
  101. });
  102. }
  103. exports.default = default_1;