File size: 3,355 Bytes
7fa9d90
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
cce7cd9
7fa9d90
 
 
 
 
 
cce7cd9
 
7fa9d90
 
 
 
 
 
 
 
 
 
 
 
 
 
 
cce7cd9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7fa9d90
 
 
 
 
 
 
 
 
 
 
cce7cd9
7fa9d90
 
 
 
 
 
cce7cd9
7fa9d90
 
 
 
 
 
 
 
 
 
 
 
 
cce7cd9
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
"""
Video Creator Module for NCAkit
Creates short-form videos with TTS, captions, background videos, and music.
"""
from fastapi import FastAPI
import logging

# Module Metadata
MODULE_NAME = "video_creator"
MODULE_PREFIX = "/api/video"
MODULE_DESCRIPTION = "Create short-form videos with TTS, captions, and background music"

logger = logging.getLogger(__name__)


def register(app: FastAPI, config):
    """
    Register the video creator module with FastAPI.
    Initializes all services and adds routes.
    """
    from .router import router, set_short_creator
    from .services.libraries.tts_client import TTSClient
    from .services.libraries.whisper_client import WhisperClient
    from .services.libraries.pexels_client import PexelsClient
    from .services.libraries.pixabay_client import PixabayClient
    from .services.music_manager import MusicManager
    from .services.short_creator import ShortCreator
    
    logger.info("Registering video_creator module...")
    
    # Validate environment variables
    if not config.pexels_api_key and not config.pixabay_api_key:
        logger.warning("Neither PEXELS_API_KEY nor PIXABAY_API_KEY is set! Video generation may fail.")
    
    if not config.hf_tts:
        logger.warning("HF_TTS is missing! TTS will fail.")
    
    # Initialize TTS client
    logger.info("Initializing TTS client...")
    tts_client = TTSClient(config.hf_tts)
    
    # Initialize Whisper client
    logger.info("Initializing Whisper client...")
    whisper_client = WhisperClient(
        model_name=config.whisper_model,
        model_dir=config.whisper_model_dir
    )
    
    # Initialize Pexels client (optional)
    pexels_client = None
    if config.pexels_api_key:
        logger.info("Initializing Pexels client...")
        pexels_client = PexelsClient(config.pexels_api_key)
    else:
        logger.info("Pexels API key not set, skipping Pexels client")
    
    # Initialize Pixabay client (optional)
    pixabay_client = None
    if config.pixabay_api_key:
        logger.info("Initializing Pixabay client...")
        pixabay_client = PixabayClient(config.pixabay_api_key)
    else:
        logger.info("Pixabay API key not set, skipping Pixabay client")
    
    # Initialize music manager
    logger.info("Initializing music manager...")
    music_manager = MusicManager(config.music_dir_path)
    try:
        music_manager.ensure_music_files_exist()
    except FileNotFoundError as e:
        logger.error(f"Music setup error: {e}")
        logger.warning("Creating empty music directory")
        config.music_dir_path.mkdir(parents=True, exist_ok=True)
    
    # Initialize short creator with both video clients
    logger.info("Initializing short creator...")
    short_creator = ShortCreator(
        config=config,
        tts_client=tts_client,
        whisper_client=whisper_client,
        pexels_client=pexels_client,
        pixabay_client=pixabay_client,
        music_manager=music_manager
    )
    
    # Set the global short creator in the router
    set_short_creator(short_creator)
    
    # Store in app state for access from other modules if needed
    app.state.video_creator = short_creator
    
    # Register routes
    app.include_router(router, prefix=MODULE_PREFIX, tags=["Video Creator"])
    
    logger.info("video_creator module registered successfully!")