FFmpegDownloader.ts 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. import { dirname, basename, join, resolve } from 'path';
  2. import * as request from 'request';
  3. import * as ProgressBar from 'progress';
  4. import { ensureDirSync, exists, writeFile } from 'fs-extra-promise';
  5. const debug = require('debug')('build:ffmpegDownloader');
  6. const progress = require('request-progress');
  7. import { Event } from './Event';
  8. import { mergeOptions } from './util';
  9. const DIR_CACHES = resolve(dirname(module.filename), '..', '..', 'caches');
  10. ensureDirSync(DIR_CACHES);
  11. interface IRequestProgress {
  12. percent: number;
  13. speed: number;
  14. size: {
  15. total: number,
  16. transferred: number,
  17. };
  18. time: {
  19. elapsed: number,
  20. remaining: number,
  21. };
  22. }
  23. interface IFFmpegDownloaderOptions {
  24. platform?: string;
  25. arch?: string;
  26. version?: string;
  27. mirror?: string;
  28. useCaches?: boolean;
  29. showProgress?: boolean;
  30. }
  31. export class FFmpegDownloader {
  32. public static DEFAULT_OPTIONS: IFFmpegDownloaderOptions = {
  33. platform: process.platform,
  34. arch: process.arch,
  35. version: '0.14.7',
  36. mirror: 'https://github.com/iteufel/nwjs-ffmpeg-prebuilt/releases/download/',
  37. useCaches: true,
  38. showProgress: true,
  39. };
  40. public onProgress: Event<IRequestProgress> = new Event('progress');
  41. public options: IFFmpegDownloaderOptions;
  42. private destination: string = DIR_CACHES;
  43. constructor(options: IFFmpegDownloaderOptions) {
  44. this.options = mergeOptions(FFmpegDownloader.DEFAULT_OPTIONS, options);
  45. debug('in constructor', 'options', options);
  46. }
  47. public async fetch() {
  48. const { mirror, version, platform, arch, showProgress } = this.options;
  49. const partVersion = this.handleVersion(version);
  50. const partPlatform = this.handlePlatform(platform);
  51. const partArch = this.handleArch(arch);
  52. const url = `${ mirror }/${ partVersion }/${ partVersion }-${ partPlatform }-${ partArch }.zip`;
  53. const filename = `ffmpeg-${ basename(url) }`;
  54. const path = join(this.destination, filename);
  55. debug('in fetch', 'url', url);
  56. debug('in fetch', 'filename', filename);
  57. debug('in fetch', 'path', path);
  58. if(!(await this.isFileExists(path))) {
  59. await this.download(url, filename, path, showProgress);
  60. }
  61. return path;
  62. }
  63. protected handlePlatform(platform: string) {
  64. switch(platform) {
  65. case 'win32':
  66. case 'win':
  67. return 'win';
  68. case 'darwin':
  69. case 'osx':
  70. case 'mac':
  71. return 'osx';
  72. case 'linux':
  73. return 'linux';
  74. default:
  75. throw new Error('ERROR_UNKNOWN_PLATFORM');
  76. }
  77. }
  78. protected handleArch(arch: string) {
  79. switch(arch) {
  80. case 'x86':
  81. case 'ia32':
  82. return 'ia32';
  83. case 'x64':
  84. return 'x64';
  85. default:
  86. throw new Error('ERROR_UNKNOWN_PLATFORM');
  87. }
  88. }
  89. protected handleVersion(version: string) {
  90. switch(version) {
  91. case 'lts':
  92. case 'stable':
  93. case 'latest':
  94. throw new Error('ERROR_VERSION_UNSUPPORTED');
  95. default:
  96. return version[0] == 'v' ? version.slice(1) : version;
  97. }
  98. }
  99. protected setDestination(destination: string) {
  100. this.destination = destination;
  101. }
  102. protected isFileExists(path: string) {
  103. return new Promise((resolve, reject) => {
  104. exists(path, resolve);
  105. });
  106. }
  107. protected async download(url: string, filename: string, path: string, showProgress: boolean) {
  108. let bar: ProgressBar = null;
  109. const onProgress = (state: IRequestProgress) => {
  110. if(!state.size.total) {
  111. return;
  112. }
  113. if(!bar) {
  114. bar = new ProgressBar('[:bar] :speedKB/s :etas', {
  115. width: 50,
  116. total: state.size.total,
  117. });
  118. }
  119. bar.update(state.size.transferred / state.size.total, {
  120. speed: (state.speed / 1000).toFixed(2),
  121. });
  122. };
  123. if(showProgress) {
  124. this.onProgress.subscribe(onProgress);
  125. }
  126. debug('in download', 'start downloading', filename);
  127. await new Promise((resolve, reject) => {
  128. progress(request(url, {
  129. encoding: null,
  130. }, (err, res, data) => {
  131. if(err) {
  132. return reject(err);
  133. }
  134. if(res.statusCode != 200) {
  135. const e = new Error(`ERROR_STATUS_CODE statusCode = ${ res.statusCode }`);
  136. return reject(e);
  137. }
  138. writeFile(path, data, err => err ? reject(err) : resolve());
  139. }))
  140. .on('progress', (state: IRequestProgress) => {
  141. this.onProgress.trigger(state);
  142. });
  143. });
  144. debug('in fetch', 'end downloading', filename);
  145. if(showProgress) {
  146. this.onProgress.unsubscribe(onProgress);
  147. if(bar) {
  148. bar.terminate();
  149. }
  150. }
  151. return path;
  152. }
  153. }