Clip Web videos.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

68 lines
1.5 KiB

  1. import {
  2. createVideoClipper,
  3. VideoType,
  4. CreateVideoClipperParams,
  5. } from '@modal/webvideo-clip-core';
  6. import { constants } from 'http2';
  7. import SERVER from './server';
  8. SERVER.route({
  9. method: 'POST',
  10. url: '/clip',
  11. handler: async (request, reply) => {
  12. const {
  13. url,
  14. start,
  15. end,
  16. } = request.body as Record<string, unknown>;
  17. const { postprocess = false } = request.query as Record<string, unknown>;
  18. let videoType: string = '';
  19. if (url.startsWith('https://www.youtube.com')) {
  20. videoType = VideoType.YOUTUBE;
  21. }
  22. const videoClipperArgs = {
  23. type: videoType,
  24. url,
  25. start,
  26. end,
  27. downloaderExecutablePath: process.env.YOUTUBE_DOWNLOADER_EXECUTABLE_PATH,
  28. } as CreateVideoClipperParams;
  29. if (postprocess) {
  30. videoClipperArgs.postprocessorExecutablePath = process.env.POSTPROCESSOR_EXECUTABLE_PATH;
  31. }
  32. const clipper = createVideoClipper(videoClipperArgs);
  33. let clipResult: Record<string, unknown>;
  34. clipper.on('success', (result: Record<string, unknown>) => {
  35. clipResult = result;
  36. });
  37. let theError: Error;
  38. clipper.on('error', (error: Error) => {
  39. theError = error;
  40. });
  41. clipper.on('end', () => {
  42. if (theError) {
  43. reply
  44. .status(constants.HTTP_STATUS_INTERNAL_SERVER_ERROR)
  45. .send({
  46. message: theError.message,
  47. });
  48. return;
  49. }
  50. reply
  51. .header('Content-Type', clipResult.type as string)
  52. .send(clipResult.output as Buffer);
  53. });
  54. clipper.process();
  55. },
  56. });