--- title: Short Video Maker emoji: ๐ŸŽฌ colorFrom: purple colorTo: pink sdk: docker app_port: 8880 --- # Short Video Maker - Python FastAPI ๐ŸŽฌ Complete Python/FastAPI migration of the short-video-maker for Hugging Face Spaces Docker deployment. ## โœจ Features - ๐ŸŽ™๏ธ Text-to-speech narration via Kokoro (cloud API) - ๐Ÿ“ Automatic caption generation using Whisper - ๐ŸŽฅ Background videos from Pexels API - ๐ŸŽต Background music with mood selection - ๐Ÿ“ฑ Portrait (9:16) and landscape (16:9) support - ๐ŸŒ Web UI and REST API - ๐Ÿš€ Optimized for Hugging Face Spaces deployment ## ๐Ÿ—๏ธ Project Structure ``` video_bot_python/ โ”œโ”€โ”€ app.py # Main FastAPI application โ”œโ”€โ”€ config.py # Configuration management โ”œโ”€โ”€ requirements.txt # Python dependencies โ”œโ”€โ”€ Dockerfile # HF Spaces Docker config โ”œโ”€โ”€ models/ โ”‚ โ””โ”€โ”€ schemas.py # Pydantic models โ”œโ”€โ”€ routers/ โ”‚ โ””โ”€โ”€ api.py # REST API endpoints โ”œโ”€โ”€ video_creator/ โ”‚ โ”œโ”€โ”€ short_creator.py # Main orchestrator โ”‚ โ”œโ”€โ”€ music_manager.py # Music file management โ”‚ โ””โ”€โ”€ libraries/ โ”‚ โ”œโ”€โ”€ tts_client.py # Kokoro TTS client โ”‚ โ”œโ”€โ”€ whisper_client.py # Faster-whisper integration โ”‚ โ”œโ”€โ”€ pexels_client.py # Pexels API client โ”‚ โ”œโ”€โ”€ ffmpeg_utils.py # Audio processing โ”‚ โ””โ”€โ”€ video_composer.py # MoviePy video composition โ”œโ”€โ”€ static/ โ”‚ โ”œโ”€โ”€ index.html # Web UI โ”‚ โ”œโ”€โ”€ style.css # UI styles โ”‚ โ”œโ”€โ”€ app.js # UI logic โ”‚ โ””โ”€โ”€ music/ # Background music files โ””โ”€โ”€ tests/ โ”œโ”€โ”€ test_api.py # Unit tests โ””โ”€โ”€ integration_test.py # E2E test ``` ## ๐Ÿš€ Quick Start ### Local Development 1. **Clone and install:** ```bash cd video_bot_python pip install -r requirements.txt ``` 2. **Configure environment:** ```bash cp .env.example .env # Edit .env with your API keys: # - PEXELS_API_KEY (from https://www.pexels.com/api/) # - HF_TTS (your TTS endpoint URL) ``` 3. **Run the server:** ``` bash python app.py # Or with uvicorn: uvicorn app:app --host 0.0.0.0 --port 8880 --reload ``` 4. **Access:** - Web UI: http://localhost:8880 - API docs: http://localhost:8880/docs - Health: http://localhost:8880/health ### Docker ```bash # Build docker build -t short-video-maker-python . # Run docker run -p 8880:8880 \ -e PEXELS_API_KEY="your_key" \ -e HF_TTS="https://your-tts.hf.space" \ -v $(pwd)/data:/data \ short-video-maker-python ``` ### Hugging Face Spaces (Deploy) 1. **Create Space:** - Go to https://huggingface.co/new-space - Select "Docker" SDK - Clone your space repository 2. **Push code:** ```bash git clone https://huggingface.co/spaces/YOUR_USERNAME/short-video-maker cp -r video_bot_python/* short-video-maker/ cd short-video-maker git add . git commit -m "Initial Python FastAPI version" git push ``` 3. **Configure secrets:** - Go to Space Settings โ†’ Repository Secrets - Add: `PEXELS_API_KEY` - Add: `HF_TTS` 4. **Wait for build and access:** - `https://YOUR_USERNAME-short-video-maker.hf.space` ## ๐Ÿ“ก API Endpoints | Endpoint | Method | Description | |----------|--------|-------------| | `/health` | GET | Health check | | `/api/short-video` | POST | Create new video | | `/api/short-video/{id}/status` | GET | Check video status | | `/api/short-video/{id}` | GET | Download video | | `/api/short-videos` | GET | List all videos | | `/api/short-video/{id}` | DELETE | Delete video | | `/api/voices` | GET | List TTS voices | | `/api/music-tags` | GET | List music moods | ### Example: Create Video ```bash curl -X POST http://localhost:8880/api/short-video \ -H "Content-Type: application/json" \ -d '{ "scenes": [ { "text": "Hello world!", "searchTerms": ["nature", "forest"] } ], "config": { "voice": "af_heart", "music": "chill", "orientation": "portrait" } }' ``` ## โš™๏ธ Environment Variables | Variable | Required | Default | Description | |----------|----------|---------|-------------| | `PEXELS_API_KEY` | โœ… Yes | - | Pexels API key | | `HF_TTS` | โœ… Yes | - | TTS service URL | | `PORT` | No | 8880 | Server port | | `LOG_LEVEL` | No | info | Logging level | | `WHISPER_MODEL` | No | tiny.en | Whisper model | | `DATA_DIR_PATH` | No | auto | Data directory | | `DOCKER` | No | false | Docker mode | ## ๐Ÿงช Testing **Unit tests:** ```bash pytest tests/test_api.py -v ``` **Integration test:** ```bash # Set environment variables first export PEXELS_API_KEY="your_key" export HF_TTS="https://your-tts.hf.space" python tests/integration_test.py ``` ## ๐Ÿ”„ Migration from Node.js This Python version maintains API compatibility with the original Node.js version: | Component | Node.js | Python | |-----------|---------|--------| | Framework | Express.js | FastAPI | | Video composition | Remotion | MoviePy | | Captions | Whisper.cpp | faster-whisper | | TTS | Kokoro-js | HTTP client (cloud) | | Media search | Pexels API | Pexels API | | Audio | FFmpeg | FFmpeg | **API endpoints remain identical** - existing clients will work without changes! ## ๐Ÿ“ Configuration Options ```python { "voice": "af_heart", # TTS voice "music": "chill", # Music mood (sad/happy/chill/dark/etc) "musicVolume": "high", # low/medium/high/muted "orientation": "portrait", # portrait/landscape "captionPosition": "bottom", # top/center/bottom "captionBackgroundColor": "blue", "paddingBack": 1000 # End padding in milliseconds } ``` ## ๐ŸŽต Adding Music 1. Copy MP3 files to `static/music/` 2. (Optional) Create `static/music/music_metadata.json`: ```json { "song1.mp3": {"mood": "chill"}, "song2.mp3": {"mood": "happy"} } ``` ## ๐Ÿ› Troubleshooting **"PEXELS_API_KEY is required"** - Set the environment variable or add to `.env` file **"HF_TTS is required"** - Set your TTS endpoint URL (must be accessible) **"No music files found"** - Copy MP3 files to `static/music/` directory **Video processing takes too long** - Use `tiny.en` Whisper model (faster) - Reduce video length - Use fewer scenes **Out of memory during rendering** - Increase container memory - Reduce `VIDEO_CACHE_SIZE_IN_BYTES` - Use shorter videos ## ๐Ÿ—๏ธ Development Install dev dependencies: ```bash pip install pytest pytest-asyncio httpx black ``` Format code: ```bash black . ``` ## ๐Ÿ“„ License MIT ## ๐Ÿ™ Credits - Original Node.js version by [David Gyori](https://github.com/gyoridavid/short-video-maker) - [Remotion](https://remotion.dev/) โ†’ [MoviePy](https://zulko.github.io/moviepy/) - [Whisper](https://github.com/ggml-org/whisper.cpp) โ†’ [faster-whisper](https://github.com/SYSTRAN/faster-whisper) - [Pexels](https://www.pexels.com/) for video content - [FFmpeg](https://ffmpeg.org/) for media processing