Clip Web videos.
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

58 rindas
1.6 KiB

  1. import * as childProcess from 'child_process';
  2. import { unlinkSync, readFileSync } from 'fs';
  3. export enum VideoType {
  4. YOUTUBE = 'youtube',
  5. }
  6. interface ClipVideoBaseParams {
  7. url: string,
  8. start: number | string,
  9. end: number | string,
  10. ffmpegExecutablePath?: string;
  11. }
  12. interface ClipYouTubeVideoParams extends ClipVideoBaseParams {
  13. downloaderExecutablePath?: string;
  14. }
  15. interface ClipVideoParams extends ClipYouTubeVideoParams {
  16. type: VideoType,
  17. }
  18. const clipYouTubeVideo = (params: ClipYouTubeVideoParams) => {
  19. if (!params.downloaderExecutablePath) {
  20. throw new Error('Downloader not found.');
  21. }
  22. if (!params.ffmpegExecutablePath) {
  23. throw new Error('ffmpeg not found.');
  24. }
  25. childProcess.spawnSync(params.downloaderExecutablePath, [params.url, '-o', 'input.webm']);
  26. childProcess.spawnSync(params.ffmpegExecutablePath, ['-ss', params.start.toString(), '-t', params.end.toString(), '-i', 'input.webm', 'output.webm']);
  27. unlinkSync('input.webm');
  28. const output = readFileSync('output.webm');
  29. unlinkSync('output.webm');
  30. return output;
  31. };
  32. export const clipVideo = (params: ClipVideoParams) => {
  33. const {
  34. type: videoType, url, start, end,
  35. } = params;
  36. switch (videoType as string) {
  37. case VideoType.YOUTUBE:
  38. return clipYouTubeVideo({
  39. ffmpegExecutablePath: process.env.FFMPEG_EXECUTABLE_PATH,
  40. downloaderExecutablePath: process.env.YOUTUBE_DOWNLOADER_EXECUTABLE_PATH,
  41. url,
  42. start,
  43. end,
  44. });
  45. default:
  46. break;
  47. }
  48. throw new TypeError(`Invalid video type: "${videoType}". Valid values are: ${JSON.stringify(Object.values(VideoType))}`);
  49. };