FFmpegDownloader.ts 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. import { dirname, basename, 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 { DownloaderBase } from './DownloaderBase';
  8. import { Event } from './Event';
  9. import { extractGeneric } from './archive';
  10. import { mergeOptions } from './util';
  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 extends DownloaderBase {
  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 options: IFFmpegDownloaderOptions;
  41. constructor(options: IFFmpegDownloaderOptions) {
  42. super();
  43. this.options = mergeOptions(FFmpegDownloader.DEFAULT_OPTIONS, options);
  44. debug('in constructor', 'options', options);
  45. }
  46. public async fetch() {
  47. const { mirror, version, platform, arch, showProgress } = this.options;
  48. const partVersion = await this.handleVersion(version);
  49. const partPlatform = this.handlePlatform(platform);
  50. const partArch = this.handleArch(arch);
  51. const url = `${ mirror }/${ partVersion }/${ partVersion }-${ partPlatform }-${ partArch }.zip`;
  52. const filename = `ffmpeg-${ basename(url) }`;
  53. const path = resolve(this.destination, filename);
  54. debug('in fetch', 'url', url);
  55. debug('in fetch', 'filename', filename);
  56. debug('in fetch', 'path', path);
  57. if(await this.isFileExists(path) && await this.isFileSynced(url, path)) {
  58. return path;
  59. }
  60. await this.download(url, filename, path, showProgress);
  61. return path;
  62. }
  63. protected async handleVersion(version: string) {
  64. switch(version) {
  65. case 'lts':
  66. case 'stable':
  67. case 'latest':
  68. throw new Error('ERROR_VERSION_UNSUPPORTED');
  69. default:
  70. return version[0] == 'v' ? version.slice(1) : version;
  71. }
  72. }
  73. }