|
- import {
- createVideoClipper,
- VideoType,
- CreateVideoClipperParams,
- } from '@modal/webvideo-clip-core';
- import { constants } from 'http2';
- import SERVER from './server';
-
- SERVER.route({
- method: 'POST',
- url: '/clip',
- handler: async (request, reply) => {
- const {
- url,
- start,
- end,
- } = request.body as Record<string, unknown>;
-
- const { postprocess = false } = request.query as Record<string, unknown>;
-
- let videoType: string = '';
-
- if (url.startsWith('https://www.youtube.com')) {
- videoType = VideoType.YOUTUBE;
- }
-
- const videoClipperArgs = {
- type: videoType,
- url,
- start,
- end,
- downloaderExecutablePath: process.env.YOUTUBE_DOWNLOADER_EXECUTABLE_PATH,
- } as CreateVideoClipperParams;
- if (postprocess) {
- videoClipperArgs.postprocessorExecutablePath = process.env.POSTPROCESSOR_EXECUTABLE_PATH;
- }
-
- const clipper = createVideoClipper(videoClipperArgs);
-
- let clipResult: Record<string, unknown>;
- clipper.on('success', (result: Record<string, unknown>) => {
- clipResult = result;
- });
-
- let theError: Error;
- clipper.on('error', (error: Error) => {
- theError = error;
- });
-
- clipper.on('end', () => {
- if (theError) {
- reply
- .status(constants.HTTP_STATUS_INTERNAL_SERVER_ERROR)
- .send({
- message: theError.message,
- });
- return;
- }
-
- reply
- .header('Content-Type', clipResult.type as string)
- .send(clipResult.output as Buffer);
- });
-
- clipper.process();
- },
- });
|