Skip to content

Formerly KrillinAI, VideoTranslation and Dubbing Tool for 120+ Languages. Open-source AI workspace for creators, powered by Codex. Video downloader, video translation, create videos, images, audios, and edits with Agents in one place. For your Social Media like YouTube, TikTok, Instagram, etc.

1015 B
Raw Blame
import type { FastifyInstance } from 'fastify';
import {
  VideoMetadataError,
  type VideoMetadataService
} from '../video-metadata/service.js';
import { apiError } from './errors.js';

export async function registerVideoMetadataRoutes(
  server: FastifyInstance,
  service: VideoMetadataService
): Promise<void> {
  server.get('/video-metadata', async (request, reply) => {
    const { url } = request.query as { url?: string };
    if (typeof url !== 'string' || url.trim().length === 0) {
      return reply.code(400).send(apiError('VALIDATION_FAILED', 'url is required'));
    }

    try {
      return await service.get(url);
    } catch (error) {
      if (error instanceof VideoMetadataError) {
        if (error.code === 'UNSUPPORTED_SOURCE' || error.code === 'INVALID_PART') {
          return reply.code(400).send(apiError('VALIDATION_FAILED', error.message));
        }
        return reply.code(502).send(apiError('VIDEO_METADATA_UNAVAILABLE', error.message));
      }
      throw error;
    }
  });
}