gifski.js 4.4 KB

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