FFmpegDownloader.js 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. "use strict";
  2. var __extends = (this && this.__extends) || (function () {
  3. var extendStatics = Object.setPrototypeOf ||
  4. ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
  5. function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
  6. return function (d, b) {
  7. extendStatics(d, b);
  8. function __() { this.constructor = d; }
  9. d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
  10. };
  11. })();
  12. var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
  13. return new (P || (P = Promise))(function (resolve, reject) {
  14. function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
  15. function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
  16. function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
  17. step((generator = generator.apply(thisArg, _arguments || [])).next());
  18. });
  19. };
  20. var __generator = (this && this.__generator) || function (thisArg, body) {
  21. var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
  22. return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
  23. function verb(n) { return function (v) { return step([n, v]); }; }
  24. function step(op) {
  25. if (f) throw new TypeError("Generator is already executing.");
  26. while (_) try {
  27. if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
  28. if (y = 0, t) op = [op[0] & 2, t.value];
  29. switch (op[0]) {
  30. case 0: case 1: t = op; break;
  31. case 4: _.label++; return { value: op[1], done: false };
  32. case 5: _.label++; y = op[1]; op = [0]; continue;
  33. case 7: op = _.ops.pop(); _.trys.pop(); continue;
  34. default:
  35. if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
  36. if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
  37. if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
  38. if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
  39. if (t[2]) _.ops.pop();
  40. _.trys.pop(); continue;
  41. }
  42. op = body.call(thisArg, _);
  43. } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
  44. if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
  45. }
  46. };
  47. Object.defineProperty(exports, "__esModule", { value: true });
  48. var path_1 = require("path");
  49. var debug = require('debug')('build:ffmpegDownloader');
  50. var progress = require('request-progress');
  51. var common_1 = require("./common");
  52. var util_1 = require("./util");
  53. var FFmpegDownloader = /** @class */ (function (_super) {
  54. __extends(FFmpegDownloader, _super);
  55. function FFmpegDownloader(options) {
  56. var _this = _super.call(this) || this;
  57. _this.options = util_1.mergeOptions(FFmpegDownloader.DEFAULT_OPTIONS, options);
  58. if (_this.options.destination !== _this.destination) {
  59. _this.setDestination(_this.options.destination);
  60. }
  61. debug('in constructor', 'options', options);
  62. return _this;
  63. }
  64. FFmpegDownloader.prototype.fetch = function () {
  65. return __awaiter(this, void 0, void 0, function () {
  66. var _a, mirror, version, platform, arch, showProgress, partVersion, partPlatform, partArch, url, filename, path, _b, _c, err_1;
  67. return __generator(this, function (_d) {
  68. switch (_d.label) {
  69. case 0:
  70. _a = this.options, mirror = _a.mirror, version = _a.version, platform = _a.platform, arch = _a.arch, showProgress = _a.showProgress;
  71. return [4 /*yield*/, this.handleVersion(version)];
  72. case 1:
  73. partVersion = _d.sent();
  74. partPlatform = this.handlePlatform(platform);
  75. partArch = this.handleArch(arch);
  76. url = mirror + "/" + partVersion + "/" + partVersion + "-" + partPlatform + "-" + partArch + ".zip";
  77. filename = "ffmpeg-" + path_1.basename(url);
  78. path = path_1.resolve(this.destination, filename);
  79. debug('in fetch', 'url', url);
  80. debug('in fetch', 'filename', filename);
  81. debug('in fetch', 'path', path);
  82. _b = this.options.forceCaches;
  83. if (!_b) return [3 /*break*/, 3];
  84. return [4 /*yield*/, this.isFileExists(path)];
  85. case 2:
  86. _b = (_d.sent());
  87. _d.label = 3;
  88. case 3:
  89. if (_b) {
  90. return [2 /*return*/, path];
  91. }
  92. _d.label = 4;
  93. case 4:
  94. _d.trys.push([4, 8, , 9]);
  95. return [4 /*yield*/, this.isFileExists(path)];
  96. case 5:
  97. _c = (_d.sent());
  98. if (!_c) return [3 /*break*/, 7];
  99. return [4 /*yield*/, this.isFileSynced(url, path)];
  100. case 6:
  101. _c = (_d.sent());
  102. _d.label = 7;
  103. case 7:
  104. if (_c) {
  105. return [2 /*return*/, path];
  106. }
  107. return [3 /*break*/, 9];
  108. case 8:
  109. err_1 = _d.sent();
  110. debug('in fetch', 'err', err_1);
  111. if (err_1.code === 'ENOTFOUND' && this.options.useCaches) {
  112. console.info('DNS lookup fails, use local caches at this time.');
  113. return [2 /*return*/, path];
  114. }
  115. else if (err_1.code === 'EAI_AGAIN' && this.options.useCaches) {
  116. console.info('DNS lookup timeout, use local caches at this time.');
  117. return [2 /*return*/, path];
  118. }
  119. else {
  120. throw err_1;
  121. }
  122. return [3 /*break*/, 9];
  123. case 9: return [4 /*yield*/, this.download(url, filename, path, showProgress)];
  124. case 10:
  125. _d.sent();
  126. return [2 /*return*/, path];
  127. }
  128. });
  129. });
  130. };
  131. FFmpegDownloader.prototype.handleVersion = function (version) {
  132. return __awaiter(this, void 0, void 0, function () {
  133. return __generator(this, function (_a) {
  134. switch (version) {
  135. case 'lts':
  136. case 'stable':
  137. case 'latest':
  138. throw new Error('ERROR_VERSION_UNSUPPORTED');
  139. default:
  140. return [2 /*return*/, version[0] == 'v' ? version.slice(1) : version];
  141. }
  142. return [2 /*return*/];
  143. });
  144. });
  145. };
  146. FFmpegDownloader.DEFAULT_OPTIONS = {
  147. platform: process.platform,
  148. arch: process.arch,
  149. version: '0.14.7',
  150. mirror: 'https://github.com/iteufel/nwjs-ffmpeg-prebuilt/releases/download/',
  151. useCaches: true,
  152. showProgress: true,
  153. forceCaches: false,
  154. destination: common_1.DownloaderBase.DEFAULT_DESTINATION,
  155. };
  156. return FFmpegDownloader;
  157. }(common_1.DownloaderBase));
  158. exports.FFmpegDownloader = FFmpegDownloader;
  159. //# sourceMappingURL=FFmpegDownloader.js.map