├── packages.txt
├── whisper_tiktok
├── __init__.py
├── config
│ ├── __init__.py
│ └── logger_config.py
├── utils
│ ├── __init__.py
│ └── color_utils.py
├── execution
│ ├── __init__.py
│ └── command_executor.py
├── factories
│ ├── __init__.py
│ └── video_factory.py
├── interfaces
│ ├── __init__.py
│ ├── tts_service.py
│ ├── video_downloader.py
│ └── transcription_service.py
├── processors
│ ├── __init__.py
│ └── video_processor.py
├── services
│ ├── __init__.py
│ ├── tts_service.py
│ ├── video_downloader.py
│ ├── transcription_service.py
│ └── ffmpeg_service.py
├── strategies
│ ├── __init__.py
│ └── processing_strategy.py
├── repositories
│ ├── __init__.py
│ └── video_repository.py
├── voice_manager.py
├── container.py
└── main.py
├── .dockerignore
├── .github
├── FUNDING.yml
├── ISSUE_TEMPLATE
│ ├── feature_request.md
│ ├── bug_report.md
│ └── other.md
├── workflows
│ ├── ci_mkdocs.yml
│ ├── ci-style-checks.yml
│ └── release.yml
└── pull_request_template.md
├── video.json
├── docker-compose.yml
├── Dockerfile
├── .pre-commit-config.yaml
├── mkdocs.yml
├── SECURITY.md
├── CODE_OF_CONDUCT.md
├── docs
├── 3-docker.md
├── 2-cuda.md
├── 1-ffmpeg.md
└── index.md
├── pyproject.toml
├── CONTRIBUTING.md
├── .gitignore
├── README.md
├── app.py
└── LICENSE
/packages.txt:
--------------------------------------------------------------------------------
1 | ffmpeg
2 |
--------------------------------------------------------------------------------
/whisper_tiktok/__init__.py:
--------------------------------------------------------------------------------
1 |
--------------------------------------------------------------------------------
/whisper_tiktok/config/__init__.py:
--------------------------------------------------------------------------------
1 |
--------------------------------------------------------------------------------
/whisper_tiktok/utils/__init__.py:
--------------------------------------------------------------------------------
1 |
--------------------------------------------------------------------------------
/whisper_tiktok/execution/__init__.py:
--------------------------------------------------------------------------------
1 |
--------------------------------------------------------------------------------
/whisper_tiktok/factories/__init__.py:
--------------------------------------------------------------------------------
1 |
--------------------------------------------------------------------------------
/whisper_tiktok/interfaces/__init__.py:
--------------------------------------------------------------------------------
1 |
--------------------------------------------------------------------------------
/whisper_tiktok/processors/__init__.py:
--------------------------------------------------------------------------------
1 |
--------------------------------------------------------------------------------
/whisper_tiktok/services/__init__.py:
--------------------------------------------------------------------------------
1 |
--------------------------------------------------------------------------------
/whisper_tiktok/strategies/__init__.py:
--------------------------------------------------------------------------------
1 |
--------------------------------------------------------------------------------
/whisper_tiktok/repositories/__init__.py:
--------------------------------------------------------------------------------
1 |
--------------------------------------------------------------------------------
/.dockerignore:
--------------------------------------------------------------------------------
1 | .git
2 | *.log
3 | *.txt
4 | *.mp4
5 | .venv
6 |
--------------------------------------------------------------------------------
/.github/FUNDING.yml:
--------------------------------------------------------------------------------
1 | github: MatteoFasulo
2 | custom: "https://www.paypal.me/MatteoFasulo"
3 |
--------------------------------------------------------------------------------
/video.json:
--------------------------------------------------------------------------------
1 | [
2 | {
3 | "series":"Crazy facts that you did not know",
4 | "part":"Part 1",
5 | "text":"The King is invaluable",
6 | "outro":"Follow us for more",
7 | "tags":[
8 | "chess",
9 | "facts",
10 | "crazy"
11 | ]
12 | }
13 | ]
14 |
--------------------------------------------------------------------------------
/whisper_tiktok/interfaces/tts_service.py:
--------------------------------------------------------------------------------
1 | from abc import ABC, abstractmethod
2 | from pathlib import Path
3 |
4 |
5 | class ITTSService(ABC):
6 | """Interface for text-to-speech services."""
7 |
8 | @abstractmethod
9 | async def synthesize(self, text: str, output_file: Path, voice: str) -> None:
10 | """Synthesize speech from text."""
11 |
--------------------------------------------------------------------------------
/whisper_tiktok/interfaces/video_downloader.py:
--------------------------------------------------------------------------------
1 | from abc import ABC, abstractmethod
2 | from pathlib import Path
3 |
4 |
5 | class IVideoDownloader(ABC):
6 | """Interface for video downloading services."""
7 |
8 | @abstractmethod
9 | def download(self, url: str, output_dir: Path) -> Path:
10 | """Download video from URL to output directory."""
11 |
--------------------------------------------------------------------------------
/docker-compose.yml:
--------------------------------------------------------------------------------
1 | version: '3.8'
2 |
3 | services:
4 | whisper-tiktok:
5 | build:
6 | context: .
7 | dockerfile: Dockerfile
8 | ports:
9 | - "8501:8501"
10 | healthcheck:
11 | test: ["CMD", "curl", "--fail", "http://localhost:8501/_stcore/health"]
12 | interval: 30s
13 | timeout: 10s
14 | retries: 5
15 | volumes:
16 | - .:/app
17 |
--------------------------------------------------------------------------------
/whisper_tiktok/interfaces/transcription_service.py:
--------------------------------------------------------------------------------
1 | from abc import ABC, abstractmethod
2 | from pathlib import Path
3 |
4 |
5 | class ITranscriptionService(ABC):
6 | """Interface for transcription services."""
7 |
8 | @abstractmethod
9 | def transcribe(
10 | self,
11 | audio_file: Path,
12 | srt_file: Path,
13 | ass_file: Path,
14 | model: str,
15 | options: dict,
16 | ) -> tuple[Path, Path]:
17 | """Transcribe audio and generate SRT/ASS files."""
18 |
--------------------------------------------------------------------------------
/.github/ISSUE_TEMPLATE/feature_request.md:
--------------------------------------------------------------------------------
1 | ---
2 | name: Feature request
3 | about: Suggest an idea for this project
4 | title: "[Feature]"
5 | labels: enhancement
6 | assignees: MatteoFasulo
7 |
8 | ---
9 |
10 | **Is your feature request related to a problem? Please describe.**
11 | A clear and concise description of what the problem is. Ex. I'm always frustrated when [...]
12 |
13 | **Describe the solution you'd like**
14 | A clear and concise description of what you want to happen.
15 |
16 | **Describe alternatives you've considered**
17 | A clear and concise description of any alternative solutions or features you've considered.
18 |
19 | **Additional context**
20 | Add any other context or screenshots about the feature request here.
21 |
--------------------------------------------------------------------------------
/Dockerfile:
--------------------------------------------------------------------------------
1 | FROM ghcr.io/astral-sh/uv:python3.12-bookworm-slim
2 |
3 | ENV UV_LINK_MODE copy
4 |
5 | RUN apt-get update && \
6 | apt-get install -y --no-install-recommends \
7 | build-essential \
8 | ffmpeg \
9 | curl \
10 | ca-certificates \
11 | && rm -rf /var/lib/apt/lists/*
12 |
13 | WORKDIR /app
14 |
15 | ENV UV_COMPILE_BYTECODE=1
16 | ENV UV_NO_DEV=1
17 | ENV UV_TOOL_BIN_DIR=/usr/local/bin
18 | ENV PATH="/app/.venv/bin:$PATH"
19 |
20 | COPY pyproject.toml uv.lock /app/
21 |
22 | RUN --mount=type=cache,target=/root/.cache/uv \
23 | uv sync --locked --no-install-project
24 |
25 | COPY . /app
26 |
27 | RUN --mount=type=cache,target=/root/.cache/uv \
28 | uv sync --locked
29 |
30 | EXPOSE 8501
31 |
32 | HEALTHCHECK CMD curl --fail http://localhost:8501/_stcore/health
33 |
34 | CMD ["uv", "run", "streamlit", "run", "app.py", "--server.port=8501", "--server.address=0.0.0.0"]
35 |
--------------------------------------------------------------------------------
/whisper_tiktok/utils/color_utils.py:
--------------------------------------------------------------------------------
1 | import re
2 |
3 |
4 | def validate_hex_color(color: str) -> bool:
5 | """Validate if the input string is a valid hex color."""
6 | pattern = r"^#?([0-9A-Fa-f]{3}|[0-9A-Fa-f]{6})$"
7 | return bool(re.match(pattern, color))
8 |
9 |
10 | def rgb_to_bgr(rgb: str) -> str:
11 | """Convert RGB hex to BGR hex."""
12 | # Validate input length
13 | if len(rgb) != 6 and len(rgb) != 7:
14 | raise ValueError("RGB hex must be 6 or 7 characters long (including #).")
15 |
16 | # Validate hex characters
17 | match = validate_hex_color(rgb)
18 |
19 | if not match:
20 | raise ValueError("Invalid RGB hex format.")
21 |
22 | if rgb.startswith("#"):
23 | rgb = rgb[1:]
24 | r, g, b = rgb[0:2], rgb[2:4], rgb[4:6]
25 | return b + g + r
26 |
27 |
28 | if __name__ == "__main__":
29 | # Example usage
30 | rgb_color = "#1A2B3C"
31 | bgr_color = rgb_to_bgr(rgb_color)
32 | print(f"RGB: {rgb_color} -> BGR: #{bgr_color}")
33 |
--------------------------------------------------------------------------------
/.github/ISSUE_TEMPLATE/bug_report.md:
--------------------------------------------------------------------------------
1 | ---
2 | name: Bug report
3 | about: Create a report to help us improve
4 | title: "[BUG]"
5 | labels: bug, help wanted
6 | assignees: MatteoFasulo
7 |
8 | ---
9 |
10 | **Describe the bug**
11 | A clear and concise description of what the bug is.
12 |
13 | **To Reproduce**
14 | Steps to reproduce the behavior:
15 |
16 | 1. Go to '...'
17 | 2. Click on '....'
18 | 3. Scroll down to '....'
19 | 4. See error
20 |
21 | **Expected behavior**
22 | A clear and concise description of what you expected to happen.
23 |
24 | **Screenshots**
25 | If applicable, add screenshots to help explain your problem.
26 |
27 | **Desktop (please complete the following information):**
28 |
29 | - OS: [e.g. iOS]
30 | - Browser [e.g. chrome, safari]
31 | - Version [e.g. 22]
32 |
33 | **Smartphone (please complete the following information):**
34 |
35 | - Device: [e.g. iPhone6]
36 | - OS: [e.g. iOS8.1]
37 | - Browser [e.g. stock browser, safari]
38 | - Version [e.g. 22]
39 |
40 | **Additional context**
41 | Add any other context about the problem here.
42 |
--------------------------------------------------------------------------------
/.github/workflows/ci_mkdocs.yml:
--------------------------------------------------------------------------------
1 | name: ci_mkdocs
2 | on:
3 | push:
4 | branches:
5 | - main
6 | - dev*
7 | permissions:
8 | contents: write
9 | jobs:
10 | deploy:
11 | runs-on: ubuntu-latest
12 | steps:
13 | - uses: actions/checkout@v6
14 | - name: Configure Git Credentials
15 | run: |
16 | git config user.name github-actions[bot]
17 | git config user.email 41898282+github-actions[bot]@users.noreply.github.com
18 |
19 | - uses: actions/setup-python@v6
20 | with:
21 | python-version: 3.12
22 | - run: echo "cache_id=$(date --utc '+%V')" >> $GITHUB_ENV
23 |
24 | - uses: actions/cache@v5
25 | with:
26 | key: mkdocs-material-${{ env.cache_id }}
27 | path: ~/.cache
28 | restore-keys: |
29 | mkdocs-material-
30 | - run: pip install mkdocs-material
31 | - run: mkdocs gh-deploy --force
32 |
--------------------------------------------------------------------------------
/whisper_tiktok/services/tts_service.py:
--------------------------------------------------------------------------------
1 | from logging import Logger
2 | from pathlib import Path
3 |
4 | import edge_tts
5 |
6 | from whisper_tiktok.interfaces.tts_service import ITTSService
7 |
8 |
9 | class TTSService(ITTSService):
10 | """Text-to-Speech service using a hypothetical TTS engine."""
11 |
12 | def __init__(self, logger: Logger):
13 | self.logger = logger
14 |
15 | async def synthesize(
16 | self,
17 | text: str,
18 | output_file: Path,
19 | voice: str = "en-US-ChristopherNeural",
20 | ) -> None:
21 | """
22 | Synthesize speech from text and save to output file.
23 |
24 | Args:
25 | text (str): The text to be converted to speech.
26 | output_file (Path): The path to save the synthesized audio file.
27 | voice (str): The voice to be used for synthesis.
28 | """
29 | self.logger.debug(f"Synthesizing speech to {output_file} using voice {voice}")
30 | communicate = edge_tts.Communicate(text, voice)
31 | await communicate.save(output_file.as_posix())
32 |
--------------------------------------------------------------------------------
/whisper_tiktok/voice_manager.py:
--------------------------------------------------------------------------------
1 | from typing import Any
2 |
3 | import edge_tts
4 |
5 |
6 | class VoicesManager:
7 | """Wrapper for edge_tts VoicesManager."""
8 |
9 | @staticmethod
10 | async def create():
11 | """Create and return voices manager object."""
12 | return await edge_tts.VoicesManager.create()
13 |
14 | @staticmethod
15 | def find(voices, gender: str, locale: str) -> Any:
16 | """Find a voice by gender and locale.
17 |
18 | Args:
19 | voices: Voices manager object from create()
20 | gender: Gender filter (Male/Female)
21 | locale: Language locale filter (e.g., en-US)
22 |
23 | Returns:
24 | Dictionary with voice information
25 |
26 | Raises:
27 | ValueError: If no voice found
28 | """
29 | result = voices.find(Gender=gender, Locale=locale)
30 | if not result or len(result) == 0:
31 | raise ValueError(f"No voice found for {gender} - {locale}")
32 | # Return the first result as a dict-like object
33 | return result[0]
34 |
--------------------------------------------------------------------------------
/whisper_tiktok/repositories/video_repository.py:
--------------------------------------------------------------------------------
1 | import random
2 | from logging import Logger
3 | from pathlib import Path
4 |
5 |
6 | class VideoRepository:
7 | """Repository for video-related file operations."""
8 |
9 | def __init__(self, base_path: Path, logger: Logger):
10 | self.base_path = base_path
11 | self.logger = logger
12 |
13 | def save_audio(self, uuid: str, data: bytes) -> Path:
14 | """Save audio file."""
15 | path = self.base_path / uuid / f"{uuid}.mp3"
16 | path.parent.mkdir(parents=True, exist_ok=True)
17 | path.write_bytes(data)
18 | return path
19 |
20 | def get_background_videos(self) -> list[Path]:
21 | """Get list of available background videos."""
22 | bg_path = self.base_path / "background"
23 | return list(bg_path.glob("*.mp4"))
24 |
25 | def random_background(self) -> Path:
26 | """Get random background video."""
27 | videos = self.get_background_videos()
28 | if not videos:
29 | raise ValueError("No background videos available")
30 | return random.choice(videos)
31 |
--------------------------------------------------------------------------------
/.github/workflows/ci-style-checks.yml:
--------------------------------------------------------------------------------
1 | name: CI Style Checks
2 |
3 | on:
4 | push:
5 | branches:
6 | - main
7 | - dev*
8 | pull_request:
9 | paths-ignore:
10 | - "*.md"
11 |
12 | jobs:
13 | style:
14 | name: Style Checks
15 | runs-on: ubuntu-latest
16 | strategy:
17 | matrix:
18 | python-version: ["3.11", "3.12"]
19 | steps:
20 | - name: Checkout Repo
21 | uses: actions/checkout@v6
22 |
23 | - name: Install uv and set the python version
24 | uses: astral-sh/setup-uv@v7
25 | with:
26 | python-version: ${{ matrix.python-version }}
27 | enable-cache: true
28 |
29 | - name: Pre-install
30 | run: |
31 | sudo apt-get update
32 | sudo apt-get -y -q install ffmpeg libavcodec-extra
33 |
34 | - name: Install the project
35 | run: uv sync --extra dev
36 |
37 | - name: ruff
38 | run: uv run ruff check whisper_tiktok
39 |
40 | - name: black
41 | run: uv run black --line-length 88 --check whisper_tiktok
42 |
43 | - name: mypy
44 | run: uv run mypy whisper_tiktok
45 |
46 | - name: pylint
47 | run: uv run pylint --fail-under=9.6 whisper_tiktok
48 |
--------------------------------------------------------------------------------
/.github/workflows/release.yml:
--------------------------------------------------------------------------------
1 | name: Create and publish a Docker image
2 |
3 | on:
4 | push:
5 | branches:
6 | - main
7 | pull_request:
8 | branches:
9 | - main
10 | env:
11 | REGISTRY: ghcr.io
12 | IMAGE_NAME: ${{ github.repository }}
13 |
14 | jobs:
15 | build-and-push-image:
16 | runs-on: ubuntu-latest
17 | permissions:
18 | contents: read
19 | packages: write
20 | #
21 | steps:
22 | - name: Checkout repository
23 | uses: actions/checkout@v6
24 | - name: Log in to the Container registry
25 | uses: docker/login-action@v3
26 | with:
27 | registry: ${{ env.REGISTRY }}
28 | username: ${{ github.actor }}
29 | password: ${{ secrets.GITHUB_TOKEN }}
30 | - name: Extract metadata (tags, labels) for Docker
31 | id: meta
32 | uses: docker/metadata-action@v5
33 | with:
34 | images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}
35 | - name: Build and push Docker image
36 | uses: docker/build-push-action@v6
37 | with:
38 | context: .
39 | push: ${{ github.event_name != 'pull_request' }}
40 | tags: ${{ steps.meta.outputs.tags }}
41 | labels: ${{ steps.meta.outputs.labels }}
42 |
--------------------------------------------------------------------------------
/whisper_tiktok/services/video_downloader.py:
--------------------------------------------------------------------------------
1 | from logging import Logger
2 | from pathlib import Path
3 |
4 | from whisper_tiktok.execution.command_executor import CommandExecutor
5 | from whisper_tiktok.interfaces.video_downloader import IVideoDownloader
6 |
7 |
8 | class VideoDownloadError(Exception):
9 | """Custom exception for video download errors."""
10 |
11 |
12 | class VideoDownloaderService(IVideoDownloader):
13 | """YouTube video downloader using yt-dlp."""
14 |
15 | def __init__(self, executor: CommandExecutor, logger: Logger):
16 | self.executor = executor
17 | self.logger = logger
18 |
19 | def download(self, url: str, output_dir: Path) -> Path:
20 | """Download video from URL."""
21 | output_dir.mkdir(parents=True, exist_ok=True)
22 |
23 | command = rf"yt-dlp -f bestvideo[ext=mp4] --restrict-filenames -o %(id)s.%(ext)s {url}"
24 | result = self.executor.execute(command, cwd=output_dir)
25 |
26 | if result.returncode != 0:
27 | raise VideoDownloadError(f"Failed to download: {result.stderr}")
28 |
29 | # Find downloaded file
30 | videos = list(output_dir.glob("*.mp4"))
31 | if not videos:
32 | raise VideoDownloadError("No video file found after download")
33 |
34 | return videos[-1] # Most recent
35 |
--------------------------------------------------------------------------------
/whisper_tiktok/services/transcription_service.py:
--------------------------------------------------------------------------------
1 | from pathlib import Path
2 |
3 | import stable_whisper
4 | import torch
5 |
6 | from whisper_tiktok.interfaces.transcription_service import ITranscriptionService
7 |
8 |
9 | class TranscriptionService(ITranscriptionService):
10 | """Service for transcribing audio using Whisper."""
11 |
12 | def __init__(self, logger):
13 | self.logger = logger
14 |
15 | def transcribe(
16 | self,
17 | audio_file: Path,
18 | srt_file: Path,
19 | ass_file: Path,
20 | model: str,
21 | options: dict,
22 | ) -> tuple[Path, Path]:
23 | self.logger.debug(
24 | f"Transcribing {audio_file} with model {model} and options {options}"
25 | )
26 |
27 | whisper_model = stable_whisper.load_model(
28 | model, device=torch.device("cuda" if torch.cuda.is_available() else "cpu")
29 | )
30 | self.logger.debug(f"Loaded Whisper model: {model}")
31 |
32 | transcription = whisper_model.transcribe(
33 | audio_file.as_posix(),
34 | regroup=True,
35 | fp16=False,
36 | word_timestamps=True,
37 | )
38 | transcription.to_srt_vtt(srt_file.as_posix(), word_level=True)
39 | transcription.to_ass(ass_file.as_posix(), word_level=True, **options)
40 | return (srt_file, ass_file)
41 |
--------------------------------------------------------------------------------
/.github/ISSUE_TEMPLATE/other.md:
--------------------------------------------------------------------------------
1 | ---
2 | name: Other
3 | about: Whatever does not apply to a bug nor to a feature request
4 | title: "[Other]"
5 | labels: help wanted, question
6 | assignees: MatteoFasulo
7 |
8 | ---
9 |
10 | ## Prerequisites
11 |
12 | Please answer the following questions for yourself before submitting an issue.
13 |
14 | - [ ] I am running the latest version
15 | - [ ] I checked the documentation and found no answer
16 | - [ ] I checked to make sure that this issue has not already been filed
17 | - [ ] I'm reporting the issue to the correct repository (for multi-repository projects)
18 |
19 | ## Expected Behavior
20 |
21 | Please describe the behavior you are expecting
22 |
23 | ## Current Behavior
24 |
25 | What is the current behavior?
26 |
27 | ## Failure Information (for bugs)
28 |
29 | Please help provide information about the failure if this is a bug. If it is not a bug, please remove the rest of this template.
30 |
31 | ## Steps to Reproduce
32 |
33 | Please provide detailed steps for reproducing the issue.
34 |
35 | 1. step 1
36 | 2. step 2
37 | 3. you get it...
38 |
39 | ## Context
40 |
41 | Please provide any relevant information about your setup. This is important in case the issue is not reproducible except for under certain conditions.
42 |
43 | - Operating System:
44 |
45 | ## Failure Logs
46 |
47 | Please include any relevant log snippets or files here.
48 |
--------------------------------------------------------------------------------
/.github/pull_request_template.md:
--------------------------------------------------------------------------------
1 | # Pull Request
2 |
3 | ## Summary
4 |
5 | Please include a brief description of the purpose of this pull request.
6 |
7 | ## Motivation & Context
8 |
9 | - Why is this change necessary?
10 | - What problem does it solve?
11 | - Reference any relevant GitHub issues (e.g., fixes #123).
12 |
13 | ## Changes Introduced
14 |
15 | Provide a clear description of the changes made, including:
16 |
17 | - Major features added
18 | - Bugs fixed
19 | - Refactoring and improvements
20 | - Other relevant updates
21 |
22 | ## Checklist
23 |
24 | Please review and check the following items before submitting your pull request:
25 |
26 | - [ ] I have tested my changes thoroughly.
27 | - [ ] My code follows the project's coding style guidelines.
28 | - [ ] I have updated documentation, if necessary.
29 | - [ ] All existing tests pass successfully.
30 | - [ ] I have added new tests, if applicable.
31 | - [ ] I have reviewed my code to ensure it is clean and efficient.
32 | - [ ] I have added any required comments or explanations for complex code changes.
33 |
34 | ---
35 |
36 | ## Screenshots (if applicable)
37 |
38 | If your pull request includes changes to the user interface, please provide screenshots to visually demonstrate the changes.
39 |
40 | ## Additional Notes
41 |
42 | Include any additional information or notes about your pull request that may be helpful for reviewers.
43 |
--------------------------------------------------------------------------------
/whisper_tiktok/config/logger_config.py:
--------------------------------------------------------------------------------
1 | """Logger configuration module."""
2 |
3 | import logging
4 | import sys
5 | from pathlib import Path
6 |
7 |
8 | def setup_logger(log_dir: Path, log_level: str = "INFO") -> logging.Logger:
9 | """
10 | Configure and return a logger instance.
11 |
12 | Args:
13 | log_dir: Directory to store log files
14 | log_level: Logging level (DEBUG, INFO, WARNING, ERROR, CRITICAL)
15 |
16 | Returns:
17 | Configured logger instance
18 | """
19 | log_dir.mkdir(parents=True, exist_ok=True)
20 |
21 | logger = logging.getLogger("whisper_tiktok")
22 | logger.setLevel(getattr(logging, log_level.upper()))
23 |
24 | # Clear existing handlers
25 | logger.handlers.clear()
26 |
27 | # Create formatters
28 | detailed_formatter = logging.Formatter(
29 | "%(asctime)s - %(name)s - %(levelname)s - %(message)s"
30 | )
31 | simple_formatter = logging.Formatter("%(levelname)s: %(message)s")
32 |
33 | # File handler
34 | file_handler = logging.FileHandler(log_dir / "app.log", encoding="utf-8")
35 | file_handler.setLevel(logging.DEBUG)
36 | file_handler.setFormatter(detailed_formatter)
37 | logger.addHandler(file_handler)
38 |
39 | # Console handler
40 | console_handler = logging.StreamHandler(sys.stdout)
41 | console_handler.setLevel(getattr(logging, log_level.upper()))
42 | console_handler.setFormatter(simple_formatter)
43 | logger.addHandler(console_handler)
44 |
45 | return logger
46 |
--------------------------------------------------------------------------------
/.pre-commit-config.yaml:
--------------------------------------------------------------------------------
1 | repos:
2 | - repo: https://github.com/pre-commit/pre-commit-hooks
3 | rev: v6.0.0
4 | hooks:
5 | - id: check-ast
6 | - id: check-docstring-first
7 | - id: check-merge-conflict
8 | - id: check-toml
9 | - id: debug-statements
10 | - id: end-of-file-fixer
11 | - id: trailing-whitespace
12 | - id: check-added-large-files
13 | - id: check-json
14 | - id: detect-private-key
15 |
16 | - repo: https://github.com/psf/black
17 | rev: 25.12.0
18 | hooks:
19 | - id: black
20 | name: black (format)
21 | args: ["--line-length=88"]
22 |
23 | - repo: https://github.com/PyCQA/isort
24 | rev: 7.0.0
25 | hooks:
26 | - id: isort
27 | name: isort (sort imports)
28 |
29 | - repo: https://github.com/astral-sh/ruff-pre-commit
30 | rev: v0.14.9
31 | hooks:
32 | - id: ruff
33 | name: ruff (lint)
34 | args: [--fix]
35 | - id: ruff-format
36 | name: ruff (format)
37 |
38 | - repo: https://github.com/pre-commit/mirrors-mypy
39 | rev: v1.19.0
40 | hooks:
41 | - id: mypy
42 | name: mypy (type check)
43 | args: [--ignore-missing-imports]
44 |
45 | - repo: https://github.com/PyCQA/bandit
46 | rev: 1.9.2
47 | hooks:
48 | - id: bandit
49 | name: bandit (security check)
50 | args: [-ll]
51 |
52 | - repo: https://github.com/astral-sh/uv-pre-commit
53 | rev: 0.9.17
54 | hooks:
55 | - id: uv-lock
56 | - id: uv-export
57 |
--------------------------------------------------------------------------------
/mkdocs.yml:
--------------------------------------------------------------------------------
1 | site_name: Whisper-TikTok
2 | theme:
3 | name: material
4 | features:
5 | - navigation.tabs
6 | - navigation.sections
7 | - toc.integrate
8 | - navigation.top
9 | - search.suggest
10 | - search.highlight
11 | - content.tabs.link
12 | - content.code.annotate
13 | - content.code.copy
14 | language: en
15 | palette:
16 | - scheme: default
17 | toggle:
18 | icon: material/toggle-switch-off-outline
19 | name: Switch to dark mode
20 | primary: teal
21 | accent: purple
22 | - scheme: slate
23 | toggle:
24 | icon: material/toggle-switch
25 | name: Switch to light mode
26 | primary: teal
27 | accent: lime
28 |
29 | extra:
30 | social:
31 | - icon: fontawesome/brands/github-alt
32 | link: https://github.com/MatteoFasulo
33 | - icon: fontawesome/brands/linkedin
34 | link: https://www.linkedin.com/in/matteofasulo/
35 |
36 | markdown_extensions:
37 | - pymdownx.highlight:
38 | anchor_linenums: true
39 | - pymdownx.inlinehilite
40 | - pymdownx.snippets
41 | - admonition
42 | - pymdownx.arithmatex:
43 | generic: true
44 | - footnotes
45 | - pymdownx.superfences
46 | - pymdownx.mark
47 | - attr_list
48 | - pymdownx.emoji:
49 | emoji_index: !!python/name:material.extensions.emoji.twemoji
50 | emoji_generator: !!python/name:material.extensions.emoji.to_svg
51 |
52 | copyright: |
53 | © 2023 Matteo Fasulo
54 |
--------------------------------------------------------------------------------
/SECURITY.md:
--------------------------------------------------------------------------------
1 | # Security Policy
2 |
3 | ## Supported Versions
4 |
5 | | Version | Supported |
6 | | ------- | ------------------ |
7 | | 2.0.x | :white_check_mark: |
8 | | < 1.0 | :x: |
9 |
10 | ## Reporting a Vulnerability
11 |
12 | If you discover a security vulnerability in this project, please follow these steps to report it:
13 |
14 | 1. **Ensure Your Findings Are Valid**: Before reporting a vulnerability, please ensure it is a genuine security issue. Avoid making it public until it has been resolved.
15 |
16 | 2. **Contact Us Privately**: Send an email to [info@matteofasulo.com](mailto:info@matteofasulo.com) with details about the vulnerability. Include the following information:
17 | - A brief description of the vulnerability.
18 | - Steps to reproduce or a proof-of-concept.
19 | - Affected versions (if known).
20 | - Any additional information that may be relevant.
21 |
22 | 3. **Expect a Response**: You should receive an acknowledgment of your report within 24 hours. We will work with you to verify and understand the issue.
23 |
24 | 4. **Resolution Timeline**: The time to resolve the issue may vary depending on its complexity and severity. We will keep you informed of our progress and let you know when we expect to release a fix.
25 |
26 | 5. **Public Disclosure**: Once the issue is resolved, we will coordinate with you on when and how the vulnerability will be publicly disclosed. We typically aim to do this responsibly and after providing a fix to affected versions.
27 |
28 | 6. **Credit**: If you report a valid security vulnerability that leads to a fix, you may be eligible for public acknowledgment and credit in our release notes or on our website. Please let us know if you wish to be credited.
29 |
30 | Thank you for helping us keep our project secure.
31 |
--------------------------------------------------------------------------------
/whisper_tiktok/container.py:
--------------------------------------------------------------------------------
1 | import logging
2 | from pathlib import Path
3 |
4 | from dependency_injector import containers, providers
5 |
6 | from whisper_tiktok.execution.command_executor import CommandExecutor
7 | from whisper_tiktok.services.ffmpeg_service import FFmpegService
8 | from whisper_tiktok.services.transcription_service import TranscriptionService
9 | from whisper_tiktok.services.tts_service import TTSService
10 | from whisper_tiktok.services.video_downloader import VideoDownloaderService
11 |
12 |
13 | class Container(containers.DeclarativeContainer):
14 | """IoC container for dependency injection."""
15 |
16 | config = providers.Configuration()
17 |
18 | # Path providers
19 | workspace_path = providers.Singleton(lambda: Path.cwd())
20 |
21 | media_path = providers.Factory(
22 | lambda workspace, uuid: workspace / "media" / uuid,
23 | workspace=workspace_path,
24 | uuid=providers.Dependency(),
25 | )
26 |
27 | output_path = providers.Factory(
28 | lambda workspace, uuid: workspace / "output" / uuid,
29 | workspace=workspace_path,
30 | uuid=providers.Dependency(),
31 | )
32 |
33 | # Service providers
34 | logger = providers.Singleton(lambda: logging.getLogger("whisper_tiktok"))
35 |
36 | command_executor = providers.Factory(CommandExecutor, logger=logger)
37 |
38 | ffmpeg_service = providers.Factory(
39 | FFmpegService, executor=command_executor, logger=logger
40 | ) # Changed
41 |
42 | video_downloader = providers.Factory(
43 | VideoDownloaderService,
44 | executor=command_executor, # Changed from ffmpeg_service
45 | logger=logger,
46 | )
47 |
48 | tts_service = providers.Factory(TTSService, logger=logger)
49 |
50 | transcription_service = providers.Factory(TranscriptionService, logger=logger)
51 |
52 | # Additional service providers can be added here
53 |
--------------------------------------------------------------------------------
/whisper_tiktok/execution/command_executor.py:
--------------------------------------------------------------------------------
1 | import subprocess
2 | from dataclasses import dataclass
3 | from logging import Logger
4 | from pathlib import Path
5 |
6 |
7 | class CommandExecutionError(Exception):
8 | """Custom exception for command execution errors."""
9 |
10 |
11 | class CommandTimeoutError(Exception):
12 | """Custom exception for command timeouts."""
13 |
14 |
15 | @dataclass
16 | class ExecutionResult:
17 | """Result of command execution."""
18 |
19 | returncode: int
20 | stdout: str
21 | stderr: str
22 |
23 | @property
24 | def success(self) -> bool:
25 | """Indicates if the command executed successfully."""
26 | return self.returncode == 0
27 |
28 |
29 | class CommandExecutor:
30 | """Executes external commands with error handling."""
31 |
32 | def __init__(self, logger: Logger):
33 | self.logger = logger
34 |
35 | def execute(
36 | self, command: str, cwd: Path | None = None, timeout: int | None = None
37 | ) -> ExecutionResult:
38 | """Execute command and return result."""
39 |
40 | self.logger.debug(f"Executing: {command}")
41 | try:
42 | with subprocess.Popen(
43 | command,
44 | cwd=cwd,
45 | stdout=subprocess.PIPE,
46 | stderr=subprocess.PIPE,
47 | text=True,
48 | ) as process:
49 | stdout, stderr = process.communicate(timeout=timeout)
50 | result = ExecutionResult(
51 | returncode=process.returncode, stdout=stdout, stderr=stderr
52 | )
53 | return result
54 | except subprocess.TimeoutExpired as exc:
55 | self.logger.error(f"Command timed out: {command}")
56 | raise CommandTimeoutError(f"Command timed out after {timeout}s") from exc
57 | except Exception as exc:
58 | self.logger.exception(f"Command execution failed: {command}")
59 | raise CommandExecutionError(str(exc)) from exc
60 |
--------------------------------------------------------------------------------
/whisper_tiktok/factories/video_factory.py:
--------------------------------------------------------------------------------
1 | import uuid
2 |
3 | from whisper_tiktok.container import Container
4 | from whisper_tiktok.processors.video_processor import VideoProcessor
5 | from whisper_tiktok.strategies.processing_strategy import (
6 | DownloadBackgroundStrategy,
7 | ProcessingStrategy,
8 | TikTokUploadStrategy,
9 | TranscriptionStrategy,
10 | TTSGenerationStrategy,
11 | VideoCompositionStrategy,
12 | )
13 |
14 |
15 | class VideoCreatorFactory:
16 | """Factory for creating video processor instances."""
17 |
18 | def __init__(self, container: Container):
19 | self.container = container
20 |
21 | def create_processor(self, video_data: dict, config: dict) -> VideoProcessor:
22 | """Create a configured video processor."""
23 | uuid_str = str(uuid.uuid4())
24 |
25 | return VideoProcessor(
26 | uuid=uuid_str,
27 | video_data=video_data,
28 | config=config,
29 | strategies=self._build_strategies(config),
30 | logger=self.container.logger(),
31 | )
32 |
33 | def _build_strategies(self, config: dict) -> list[ProcessingStrategy]:
34 | """Build processing pipeline based on config."""
35 | strategies = [
36 | DownloadBackgroundStrategy(
37 | self.container.video_downloader(), self.container.logger()
38 | ),
39 | TTSGenerationStrategy(
40 | self.container.tts_service(), self.container.logger()
41 | ),
42 | TranscriptionStrategy(
43 | self.container.transcription_service(), self.container.logger()
44 | ),
45 | VideoCompositionStrategy(
46 | self.container.ffmpeg_service(), self.container.logger()
47 | ),
48 | ]
49 |
50 | if config.get("upload_tiktok"):
51 | strategies.append(
52 | TikTokUploadStrategy(self.container.uploader(), self.container.logger())
53 | )
54 |
55 | return strategies
56 |
--------------------------------------------------------------------------------
/CODE_OF_CONDUCT.md:
--------------------------------------------------------------------------------
1 | # Code of Conduct
2 |
3 | We expect all contributors and participants in the Reddit Video Maker Bot project to adhere to the following code of conduct. We want to create a welcoming and inclusive community where everyone feels respected and valued, regardless of their background, experience, or personal beliefs.
4 |
5 | ## Our Standards
6 |
7 | Examples of behavior that contribute to creating a positive environment include:
8 |
9 | - Being respectful and considerate towards others, their opinions, and their work.
10 | - Using welcoming and inclusive language.
11 | - Showing empathy towards other contributors and participants.
12 | - Being open to constructive feedback and criticism.
13 | - Focusing on what is best for the community and the project.
14 |
15 | Examples of unacceptable behavior include:
16 |
17 | - Using derogatory, offensive, or discriminatory language or behavior.
18 | - Trolling, insulting, or derogatory comments or personal attacks.
19 | - Engaging in any form of harassment or intimidation, including but not limited to sexual harassment, racism, and hate speech.
20 | - Public or private harassment, insults, or threats against anyone.
21 | - Publishing or communicating private information about others without their consent.
22 | - Other conduct that would be considered inappropriate in a professional setting.
23 |
24 | ## Enforcement
25 |
26 | We take this code of conduct seriously and will enforce it to the best of our ability. We expect all participants to follow these guidelines at all times, and any behavior that violates these standards may result in a warning or, in extreme cases, expulsion from the project.
27 |
28 | If you witness or experience behavior that violates this code of conduct, please contact the project maintainers or send an email to the project email address with the subject line "Code of Conduct Violation." All complaints will be reviewed and investigated, and appropriate action will be taken.
29 |
30 | ## Attribution
31 |
32 | This code of conduct is adapted from the [Contributor Covenant](https://www.contributor-covenant.org), version 2.0, available at https://www.contributor-covenant.org/version/2/0/code_of_conduct.html.
33 |
--------------------------------------------------------------------------------
/whisper_tiktok/processors/video_processor.py:
--------------------------------------------------------------------------------
1 | from dataclasses import dataclass
2 | from logging import Logger
3 | from pathlib import Path
4 |
5 | from whisper_tiktok.strategies.processing_strategy import (
6 | ProcessingContext,
7 | ProcessingStrategy,
8 | )
9 |
10 |
11 | @dataclass
12 | class ProcessingResult:
13 | """Result of video processing."""
14 |
15 | uuid: str
16 | output_path: Path
17 | success: bool = True
18 |
19 |
20 | class VideoProcessor:
21 | """Main orchestrator for video processing pipeline."""
22 |
23 | def __init__(
24 | self,
25 | uuid: str,
26 | video_data: dict,
27 | config: dict,
28 | strategies: list[ProcessingStrategy],
29 | logger: Logger,
30 | ):
31 | self.uuid = uuid
32 | self.video_data = video_data
33 | self.config = config
34 | self.strategies = strategies
35 | self.logger = logger
36 |
37 | async def process(self) -> ProcessingResult:
38 | """Execute the processing pipeline."""
39 |
40 | # Initialize context
41 | media_path = Path(self.config.get("workspace_path", ".")) / "media" / self.uuid
42 | output_path = (
43 | Path(self.config.get("workspace_path", ".")) / "output" / self.uuid
44 | )
45 |
46 | media_path.mkdir(parents=True, exist_ok=True)
47 | output_path.mkdir(parents=True, exist_ok=True)
48 |
49 | context = ProcessingContext(
50 | video_data=self.video_data,
51 | uuid=self.uuid,
52 | media_path=media_path,
53 | output_path=output_path,
54 | config=self.config,
55 | )
56 |
57 | # Execute each strategy
58 | try:
59 | for strategy in self.strategies:
60 | self.logger.info(f"Executing strategy: {strategy.__class__.__name__}")
61 | context = await strategy.execute(context)
62 |
63 | output_file = context.output_path / f"{self.uuid}.mp4"
64 |
65 | return ProcessingResult(
66 | uuid=self.uuid,
67 | output_path=output_file,
68 | success=True,
69 | )
70 | except Exception:
71 | self.logger.exception(f"Processing failed for video {self.uuid}")
72 | raise
73 |
--------------------------------------------------------------------------------
/docs/3-docker.md:
--------------------------------------------------------------------------------
1 | # Docker Image for Whisper-TikTok
2 |
3 | In this guide, we'll walk you through the process of using the dockerized version of the Whisper-TikTok model. Docker is a platform that allows you to package applications and their dependencies into containers, making it easy to run them on any system without worrying about compatibility issues.
4 |
5 | ## Table of Contents
6 |
7 | 1. [Prerequisites](#prerequisites)
8 | 2. [Pull the image](#pulling-the-image)
9 | 3. [Run the container](#run-the-container)
10 |
11 | ---
12 |
13 | ## Prerequisites
14 |
15 | Before you begin, make sure you have Docker installed on your system. If you don't have Docker installed, you can follow the [official installation guide](https://docs.docker.com/get-docker/) to set it up.
16 |
17 | ## Pull the image
18 |
19 | To use the Whisper-TikTok model in a Docker container, you first need to pull the Docker image from the [ghcr repository](https://github.com/MatteoFasulo/Whisper-TikTok/pkgs/container/whisper-tiktok). You can do this by running the following command in your terminal:
20 |
21 | ```sh
22 | docker pull ghcr.io/matteofasulo/whisper-tiktok:main
23 | ```
24 |
25 | This command will download the latest version of the Whisper-TikTok Docker image to your system.
26 |
27 | ## Run the container
28 |
29 | Once you have pulled the Docker image, you can run the container using the following command:
30 |
31 | ```sh
32 | docker run -d --name whisper-tiktok --network host --mount source=whisper-tiktok-vol,target=/app ghcr.io/matteofasulo/whisper-tiktok:main
33 | ```
34 |
35 | This command will start the Whisper-TikTok container in detached mode, using the host network and mounting a volume to store the model checkpoints and logs. You can now access the Whisper-TikTok API at `http://localhost:8000`.
36 |
37 | To stop the container, you can run the following command:
38 |
39 | ```sh
40 | docker stop whisper-tiktok
41 | ```
42 |
43 | And to remove the container, you can use:
44 |
45 | ```sh
46 | docker rm whisper-tiktok
47 | ```
48 |
49 | If you want to inspect the container volume to retrieve the output files, you can use the following command:
50 |
51 | ```sh
52 | docker volume inspect whisper-tiktok-vol
53 | ```
54 |
55 | This will provide you with the path to the volume on your system. Navigate to that path to access the model outputs.
56 |
--------------------------------------------------------------------------------
/pyproject.toml:
--------------------------------------------------------------------------------
1 | [project]
2 | name = "whisper-tiktok"
3 | version = "2.0.3"
4 | description = "A TikTok video creation tool using Whisper ASR and EdgeTTS"
5 | authors = [
6 | { name="Matteo Fasulo", email="info@matteofasulo.com" },
7 | ]
8 | license = "Apache-2.0"
9 | readme = "README.md"
10 | requires-python = ">=3.11"
11 | dependencies = [
12 | "dependency-injector>=4.0,<5.0",
13 | "edge-tts==7.2.6",
14 | "numpy>=2.3.5",
15 | "openai-whisper>=20250625",
16 | "pre-commit>=4.5.0",
17 | "rich>=14.2.0",
18 | "stable-ts>=2.19.1",
19 | "streamlit>=1.52.1",
20 | "tiktok-uploader>=1.1.5",
21 | "torch>=2.6.0,<2.8.0",
22 | "tqdm>=4.67.1",
23 | "typer>=0.20.0",
24 | "yt-dlp>=2025.11.12",
25 | ]
26 |
27 | [project.optional-dependencies]
28 | dev = [
29 | "pytest",
30 | "pytest-cov",
31 | "pytest-asyncio",
32 | "anyio",
33 | "black",
34 | "isort",
35 | "ruff",
36 | "pylint",
37 | "mypy",
38 | "bandit",
39 | "mkdocs-material",
40 | ]
41 |
42 | [tool.setuptools.packages]
43 | find = {}
44 |
45 | [tool.isort]
46 | py_version = 311
47 | profile = "black"
48 | line_length = 88
49 |
50 | [tool.black]
51 | line-length = 88
52 | target-version = ['py311', 'py312']
53 |
54 | [tool.mypy]
55 | python_version = "3.11"
56 | warn_return_any = true
57 | warn_unused_configs = true
58 | ignore_missing_imports = true
59 | disallow_untyped_defs = false
60 |
61 | [tool.pylint.messages_control]
62 | disable = [
63 | "C0103", # invalid-name
64 | "C0114", # missing-module-docstring
65 | "R0903", # too-few-public-methods
66 | ]
67 |
68 | [tool.pytest.ini_options]
69 | testpaths = ["tests"]
70 | python_files = ["test_*.py"]
71 | addopts = "-v --strict-markers"
72 |
73 | [tool.ruff]
74 | line-length = 88
75 | target-version = "py311"
76 | lint.select = [
77 | "E", # pycodestyle errors
78 | "W", # pycodestyle warnings
79 | "F", # Pyflakes
80 | "I", # isort
81 | "C", # flake8-comprehensions
82 | "B", # flake8-bugbear
83 | ]
84 | lint.ignore = ["E501"] # line too long (handled by black)
85 |
86 | [tool.ruff.lint.mccabe]
87 | # Flag errors (`C901`) whenever the complexity level exceeds 5.
88 | max-complexity = 20
89 |
90 | [tool.pylint.main]
91 | extension-pkg-allow-list = ["dependency_injector.containers"]
92 |
93 | [tool.uv.sources]
94 | torch = [
95 | { index = "pytorch-cu126" },
96 | ]
97 |
98 | [[tool.uv.index]]
99 | name = "pytorch-cu126"
100 | url = "https://download.pytorch.org/whl/cu126"
101 | explicit = true
102 |
103 | [[tool.mypy.overrides]]
104 | module = ["tiktok_uploader.*", "stable_whisper.*"]
105 | follow_imports = "skip"
106 |
--------------------------------------------------------------------------------
/CONTRIBUTING.md:
--------------------------------------------------------------------------------
1 | # Contributing to Whisper-TikTok
2 |
3 | Thank you for your interest in contributing to our project! We appreciate your support and look forward to working with you.
4 |
5 | ## Getting Started
6 |
7 | Before contributing to our project, we recommend that you familiarize yourself with our project's [code of conduct](CODE_OF_CONDUCT.md). We also encourage you to review the [existing issues](https://github.com/MatteoFasulo/Whisper-TikTok/issues) and [pull requests](https://github.com/MatteoFasulo/Whisper-TikTok/pulls) to get an idea of what needs to be done and to avoid duplicating efforts.
8 |
9 | ## Ways to Contribute
10 |
11 | ### Reporting Issues
12 |
13 | If you encounter any bugs or issues, please let us know by creating an issue in our project's [issue tracker](https://github.com/MatteoFasulo/Whisper-TikTok/issues). When reporting an issue, please include as much detail as possible, such as a clear and descriptive title, a step-by-step description of the problem, and any relevant screenshots or error messages.
14 |
15 | ### Suggesting Enhancements
16 |
17 | We welcome suggestions for new features or enhancements to our project! Please create an issue in our project's [issue tracker](https://github.com/MatteoFasulo/Whisper-TikTok/issues) and describe the new feature or enhancement you'd like to see. Be sure to provide as much detail as possible, such as why you think the feature would be useful, any relevant use cases, and any potential challenges or limitations.
18 |
19 | ### Contributing Code
20 |
21 | We appreciate contributions of all kinds, including code contributions! Before contributing code, please make sure to do the following:
22 |
23 | 1. Review the [existing issues](https://github.com/MatteoFasulo/Whisper-TikTok/issues) and [pull requests](https://github.com/MatteoFasulo/Whisper-TikTok/pulls) to make sure your proposed changes haven't already been addressed.
24 | 2. Familiarize yourself with our project's code structure and development practices.
25 | 3. Create a fork of our project and make your changes in a new branch.
26 | 4. Submit a pull request with a clear and descriptive title, a detailed description of the changes you made, and any relevant screenshots or code snippets.
27 |
28 | Please note that all code contributions are subject to review and may require changes before they can be merged into the main project.
29 |
30 | ### Improving Documentation
31 |
32 | Improving project documentation is also a valuable contribution! If you notice any errors or areas where the documentation could be improved, please create an issue in our project's [issue tracker](https://github.com/MatteoFasulo/Whisper-TikTok/issues) or submit a pull request with your proposed changes.
33 |
34 | ## Code of Conduct
35 |
36 | Our project has a code of conduct to ensure that all contributors feel welcome and valued. Please review the [CODE_OF_CONDUCT.md](CODE_OF_CONDUCT.md) file before contributing to our project.
37 |
38 | ## Conclusion
39 |
40 | We appreciate your interest in contributing to our project and look forward to your contributions. If you have any questions or need any help, please don't hesitate to reach out to us through the issue tracker or by email.
41 |
--------------------------------------------------------------------------------
/docs/2-cuda.md:
--------------------------------------------------------------------------------
1 | # Installing CUDA Driver for GPU Acceleration
2 |
3 | To harness the power of GPU acceleration for the OpenAI Whisper model with PyTorch, you'll need to install the CUDA driver on your system. CUDA is a parallel computing platform and API developed by NVIDIA that allows GPUs to perform complex computations much faster than traditional CPUs. This guide will walk you through the process of installing the CUDA driver step by step.
4 |
5 | ## Table of Contents
6 |
7 | 1. [Prerequisites](#prerequisites)
8 | 2. [Installing CUDA Driver](#installing-cuda-driver)
9 | 3. [Verifying CUDA Installation](#verifying-cuda-installation)
10 |
11 | ---
12 |
13 | ## Prerequisites
14 |
15 | Before you begin, make sure you have the following prerequisites in place:
16 |
17 | - A compatible NVIDIA GPU: CUDA requires an NVIDIA GPU that supports CUDA. You can check the list of supported GPUs on the NVIDIA website.
18 |
19 | - Operating System: CUDA is available for various operating systems, including Windows, Linux, and macOS. Ensure you are using a supported OS.
20 |
21 | - NVIDIA Driver: Make sure you have the latest NVIDIA driver installed for your GPU. You can download it from the official NVIDIA website.
22 |
23 | ---
24 |
25 | ## Installing CUDA Driver
26 |
27 | Follow these steps to install the CUDA driver on your system:
28 |
29 | ### Step 1: Download CUDA Toolkit
30 |
31 | 1. Visit the official NVIDIA CUDA Toolkit download page: [https://developer.nvidia.com/cuda-downloads](https://developer.nvidia.com/cuda-downloads).
32 |
33 | 2. Select your operating system and architecture. Choose the version that matches your system.
34 |
35 | 3. Click the "Download" button to start the download.
36 |
37 | ### Step 2: Run the Installer
38 |
39 | 1. Once the download is complete, run the CUDA Toolkit installer.
40 |
41 | 2. Follow the on-screen instructions to install CUDA. You can customize the installation options, but it's recommended to install the default components unless you have specific requirements.
42 |
43 | 3. During the installation, you may be prompted to install the NVIDIA driver if it's not already installed. Follow the prompts to complete the driver installation.
44 |
45 | 4. CUDA will also prompt you to install the CUDA Toolkit and CUDA Samples. Install both components.
46 |
47 | ### Step 3: Environment Setup
48 |
49 | 1. After the installation is complete, you need to add CUDA to your system's PATH environment variable.
50 |
51 | - **Windows:** CUDA will automatically add itself to the system PATH during installation. You may need to restart your computer for the changes to take effect.
52 |
53 | - **Linux:** You can add CUDA to your PATH by appending the following line to your shell profile file (e.g., `~/.bashrc` or `~/.zshrc`):
54 |
55 | ```sh
56 | export PATH=/usr/local/cuda/bin:$PATH
57 | ```
58 |
59 | - **macOS:** CUDA should also be added to your PATH automatically on macOS. Restart your terminal for the changes to apply.
60 |
61 | ### Step 4: Reboot Your System
62 |
63 | 1. To ensure that the changes are applied correctly, it's recommended to reboot your system.
64 |
65 | ---
66 |
67 | ## Verifying CUDA Installation
68 |
69 | To verify that CUDA is installed correctly, follow these steps:
70 |
71 | 1. Open a terminal or command prompt.
72 |
73 | 2. Run the following command to check the CUDA version:
74 |
75 | ```sh
76 | nvcc --version
77 | ```
78 |
79 | This command should display the CUDA version, confirming that CUDA is installed.
80 |
81 | 3. Additionally, you can run a GPU-related command, such as:
82 |
83 | ```sh
84 | nvidia-smi
85 | ```
86 |
87 | This command will display information about your NVIDIA GPU, including the driver version and GPU utilization.
88 |
89 | Congratulations! You've successfully installed the CUDA driver for GPU acceleration. You can now utilize the power of your GPU to accelerate tasks, including running the OpenAI Whisper model with PyTorch for faster and more efficient computations.
90 |
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | # Byte-compiled / optimized / DLL files
2 | __pycache__/
3 | *.py[cod]
4 | *$py.class
5 |
6 | # C extensions
7 | *.so
8 |
9 | # Distribution / packaging
10 | .Python
11 | build/
12 | develop-eggs/
13 | dist/
14 | downloads/
15 | eggs/
16 | .eggs/
17 | lib/
18 | lib64/
19 | parts/
20 | sdist/
21 | var/
22 | wheels/
23 | share/python-wheels/
24 | *.egg-info/
25 | .installed.cfg
26 | *.egg
27 | MANIFEST
28 |
29 | # PyInstaller
30 | # Usually these files are written by a python script from a template
31 | # before PyInstaller builds the exe, so as to inject date/other infos into it.
32 | *.manifest
33 | *.spec
34 |
35 | # Installer logs
36 | pip-log.txt
37 | pip-delete-this-directory.txt
38 |
39 | # Unit test / coverage reports
40 | htmlcov/
41 | .tox/
42 | .nox/
43 | .coverage
44 | .coverage.*
45 | .cache
46 | nosetests.xml
47 | coverage.xml
48 | *.cover
49 | *.py,cover
50 | .hypothesis/
51 | .pytest_cache/
52 | cover/
53 |
54 | # Translations
55 | *.mo
56 | *.pot
57 |
58 | # Django stuff:
59 | *.log
60 | local_settings.py
61 | db.sqlite3
62 | db.sqlite3-journal
63 |
64 | # Flask stuff:
65 | instance/
66 | .webassets-cache
67 |
68 | # Scrapy stuff:
69 | .scrapy
70 |
71 | # Sphinx documentation
72 | docs/_build/
73 |
74 | # PyBuilder
75 | .pybuilder/
76 | target/
77 |
78 | # Jupyter Notebook
79 | .ipynb_checkpoints
80 |
81 | # IPython
82 | profile_default/
83 | ipython_config.py
84 |
85 | # pyenv
86 | # For a library or package, you might want to ignore these files since the code is
87 | # intended to run in multiple environments; otherwise, check them in:
88 | # .python-version
89 |
90 | # pipenv
91 | # According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
92 | # However, in case of collaboration, if having platform-specific dependencies or dependencies
93 | # having no cross-platform support, pipenv may install dependencies that don't work, or not
94 | # install all needed dependencies.
95 | #Pipfile.lock
96 |
97 | # poetry
98 | # Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control.
99 | # This is especially recommended for binary packages to ensure reproducibility, and is more
100 | # commonly ignored for libraries.
101 | # https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control
102 | #poetry.lock
103 |
104 | # pdm
105 | # Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control.
106 | #pdm.lock
107 | # pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it
108 | # in version control.
109 | # https://pdm.fming.dev/#use-with-ide
110 | .pdm.toml
111 |
112 | # PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm
113 | __pypackages__/
114 |
115 | # Celery stuff
116 | celerybeat-schedule
117 | celerybeat.pid
118 |
119 | # SageMath parsed files
120 | *.sage.py
121 |
122 | # Environments
123 | .env
124 | .venv
125 | env/
126 | venv/
127 | ENV/
128 | env.bak/
129 | venv.bak/
130 |
131 | # Spyder project settings
132 | .spyderproject
133 | .spyproject
134 |
135 | # Rope project settings
136 | .ropeproject
137 |
138 | # mkdocs documentation
139 | /site
140 |
141 | # mypy
142 | .mypy_cache/
143 | .dmypy.json
144 | dmypy.json
145 |
146 | # Pyre type checker
147 | .pyre/
148 |
149 | # pytype static type analyzer
150 | .pytype/
151 |
152 | # Cython debug symbols
153 | cython_debug/
154 |
155 | # PyCharm
156 | # JetBrains specific template is maintained in a separate JetBrains.gitignore that can
157 | # be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
158 | # and can be added to the global gitignore or merged into this file. For a more nuclear
159 | # option (not recommended) you can uncomment the following to ignore the entire idea folder.
160 | #.idea/
161 |
162 | # media folder
163 | media/*
164 |
165 | # background folder
166 | background/*
167 |
168 | # output folder
169 | output/*
170 |
171 | # Cookies
172 | cookies.txt
173 |
174 | # reddit2json
175 | .env
176 | Pipfile
177 | Pipfile.lock
178 |
--------------------------------------------------------------------------------
/docs/1-ffmpeg.md:
--------------------------------------------------------------------------------
1 | # Installing FFmpeg
2 |
3 | FFmpeg is a powerful multimedia framework that provides command-line tools to record, convert, and stream audio and video. It's an essential tool for anyone working with multimedia files. In this guide, we'll walk you through the installation process for FFmpeg on various operating systems.
4 |
5 | ## Table of Contents
6 |
7 | 1. [Windows](#windows-installation)
8 | 2. [macOS](#macos-installation)
9 | 3. [Linux](#linux-installation)
10 | 4. [Testing Your FFmpeg Installation](#testing-ffmpeg)
11 |
12 | ---
13 |
14 | ## Windows Installation
15 |
16 | Installing FFmpeg on Windows can be done using pre-built executables.
17 |
18 | 1. **Download FFmpeg:** Visit the official FFmpeg website's download page at [https://www.ffmpeg.org/download.html](https://www.ffmpeg.org/download.html). Scroll down to the "Windows" section and choose one of the following options:
19 |
20 | - **Static Builds:** These are recommended for most users. Download the latest "64-bit" or "32-bit" static build, depending on your system architecture.
21 |
22 | - **Other Builds:** Advanced users can explore other options like linking libraries or shared builds.
23 |
24 | 2. **Extract the Zip File:** Once the download is complete, extract the contents of the zip file to a location on your computer, e.g., `C:\ffmpeg`. You should now have a folder containing FFmpeg executable files.
25 |
26 | 3. **Add FFmpeg to System Path (Optional):** To use FFmpeg from any command prompt or terminal window, you can add its location to your system's PATH environment variable.
27 |
28 | 4. **Testing Installation:** Open a command prompt and run the following command to verify your FFmpeg installation:
29 |
30 | ```sh
31 | ffmpeg -version
32 | ```
33 |
34 | If installed correctly, this command will display FFmpeg's version information.
35 |
36 | ---
37 |
38 | ## macOS Installation
39 |
40 | You can install FFmpeg on macOS using package managers like Homebrew or MacPorts. Here's how to do it with Homebrew:
41 |
42 | 1. **Install Homebrew:** If you don't already have Homebrew installed, open a terminal and run the following command:
43 |
44 | ```sh
45 | /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
46 | ```
47 |
48 | 2. **Install FFmpeg:** Once Homebrew is installed, you can install FFmpeg by running the following command:
49 |
50 | ```sh
51 | brew install ffmpeg
52 | ```
53 |
54 | 3. **Testing Installation:** After installation, run the following command in your terminal to verify FFmpeg is correctly installed:
55 |
56 | ```sh
57 | ffmpeg -version
58 | ```
59 |
60 | You should see FFmpeg's version information.
61 |
62 | ---
63 |
64 | ## Linux Installation
65 |
66 | On Linux, you can install FFmpeg using your distribution's package manager. Here are instructions for some popular Linux distributions:
67 |
68 | ### Ubuntu/Debian
69 |
70 | 1. Open a terminal.
71 |
72 | 2. Run the following commands to update your package list and install FFmpeg:
73 |
74 | ```sh
75 | sudo apt update
76 | sudo apt install ffmpeg
77 | ```
78 |
79 | ### CentOS/Fedora
80 |
81 | 1. Open a terminal.
82 |
83 | 2. Run the following command to install FFmpeg:
84 |
85 | ```sh
86 | sudo dnf install ffmpeg
87 | ```
88 |
89 | ### Arch Linux
90 |
91 | 1. Open a terminal.
92 |
93 | 2. Run the following command to install FFmpeg:
94 |
95 | ```sh
96 | sudo pacman -S ffmpeg
97 | ```
98 |
99 | ### Testing Installation
100 |
101 | After installation, run the following command in your terminal to verify FFmpeg is correctly installed:
102 |
103 | ```sh
104 | ffmpeg -version
105 | ```
106 |
107 | You should see FFmpeg's version information.
108 |
109 | ---
110 |
111 | ## Testing Your FFmpeg Installation
112 |
113 | To ensure FFmpeg is working correctly, you can run a simple test command. Open your command prompt, terminal, or PowerShell and run:
114 |
115 | ```sh
116 | ffmpeg -version
117 | ```
118 |
119 | This command should display FFmpeg's version information, confirming that FFmpeg is successfully installed on your system.
120 |
121 | Congratulations! You've now installed FFmpeg and can use its powerful multimedia capabilities for various tasks like video conversion, editing, and more.
122 |
--------------------------------------------------------------------------------
/whisper_tiktok/services/ffmpeg_service.py:
--------------------------------------------------------------------------------
1 | import json
2 | import os
3 | from logging import Logger
4 | from pathlib import Path
5 | from typing import NamedTuple
6 |
7 | from whisper_tiktok.execution.command_executor import CommandExecutor, ExecutionResult
8 |
9 |
10 | class FFmpegError(Exception):
11 | """Custom exception for FFmpeg errors."""
12 |
13 |
14 | class MediaInfo(NamedTuple):
15 | """Represents the result of running FFprobe.
16 |
17 | Attributes:
18 | return_code (int): The return code of the FFprobe process.
19 | json (str): The JSON output from FFprobe.
20 | error (str): The error message from FFprobe, if any.
21 | """
22 |
23 | return_code: int
24 | json: str
25 | error: str
26 |
27 | @staticmethod
28 | def from_json(result: ExecutionResult) -> "MediaInfo":
29 | """Creates a MediaInfo instance from FFprobe execution result."""
30 | return MediaInfo(
31 | return_code=result.returncode, json=result.stdout, error=result.stderr
32 | )
33 |
34 | @staticmethod
35 | def convert_time(time_in_seconds: float) -> str:
36 | """
37 | Converts time in seconds to a string in the format "hh:mm:ss.mmm".
38 |
39 | Args:
40 | time_in_seconds (float): The time in seconds to be converted.
41 |
42 | Returns:
43 | str: The time in the format "hh:mm:ss.mmm".
44 | """
45 | hours = int(time_in_seconds // 3600)
46 | minutes = int((time_in_seconds % 3600) // 60)
47 | seconds = int(time_in_seconds % 60)
48 | milliseconds = int((time_in_seconds - int(time_in_seconds)) * 1000)
49 | return f"{hours:02d}:{minutes:02d}:{seconds:02d}.{milliseconds:03d}"
50 |
51 | @property
52 | def duration(self) -> float:
53 | """Extracts the duration of the audio stream from the FFprobe JSON output."""
54 | d = json.loads(self.json)
55 |
56 | streams = d.get("streams", [])
57 | audio_stream = None
58 | for stream in streams:
59 | if stream["codec_type"] == "audio":
60 | audio_stream = stream
61 | break
62 |
63 | if audio_stream is None:
64 | raise ValueError("No audio stream found")
65 |
66 | return float(audio_stream["duration"])
67 |
68 |
69 | class FFmpegService:
70 | """Service for FFmpeg operations."""
71 |
72 | def __init__(self, executor: CommandExecutor, logger: Logger):
73 | self.executor = executor
74 | self.logger = logger
75 |
76 | def _build_video_filters(self, subtitles: Path) -> str:
77 | return rf"crop=ih/16*9:ih,scale=w=1080:h=1920:flags=lanczos,gblur=sigma=2,ass={subtitles.as_posix()}"
78 |
79 | def _build_ffmpeg_command(
80 | self,
81 | background: Path,
82 | audio: Path,
83 | output: Path,
84 | start_time: int,
85 | duration: str,
86 | filters: str,
87 | ) -> str:
88 | return rf"ffmpeg -ss {start_time} -t {duration} -i {background.as_posix()} -i {audio.as_posix()} -map 0:v -map 1:a -filter:v {filters} -c:v libx264 -crf 23 -c:a aac -ac 2 -b:a 192K {output.as_posix()} -y -threads {os.cpu_count()}"
89 |
90 | def compose_video(
91 | self,
92 | background: Path,
93 | audio: Path,
94 | subtitles: Path,
95 | output: Path,
96 | start_time: int,
97 | duration: str,
98 | ) -> Path:
99 | """Compose final video with background, audio, and subtitles."""
100 |
101 | # Build filter complex
102 | filters = self._build_video_filters(subtitles)
103 |
104 | command = self._build_ffmpeg_command(
105 | background, audio, output, start_time, duration, filters
106 | )
107 | result = self.executor.execute(command)
108 |
109 | if result.returncode != 0:
110 | raise FFmpegError(f"Failed to compose video: {result.stderr}")
111 |
112 | return output
113 |
114 | def get_media_info(self, file_path: Path) -> MediaInfo:
115 | """Get media information using ffprobe."""
116 | command = f"ffprobe -v quiet -print_format json -show_format -show_streams {file_path.as_posix()}"
117 | result = self.executor.execute(command)
118 |
119 | if result.returncode != 0:
120 | raise FFmpegError(f"Failed to probe media: {result.stderr}")
121 |
122 | return MediaInfo.from_json(result)
123 |
--------------------------------------------------------------------------------
/whisper_tiktok/strategies/processing_strategy.py:
--------------------------------------------------------------------------------
1 | from abc import ABC, abstractmethod
2 | from dataclasses import dataclass, field
3 | from logging import Logger
4 | from pathlib import Path
5 |
6 | from whisper_tiktok.interfaces.transcription_service import ITranscriptionService
7 | from whisper_tiktok.interfaces.tts_service import ITTSService
8 | from whisper_tiktok.interfaces.video_downloader import IVideoDownloader
9 | from whisper_tiktok.services.ffmpeg_service import FFmpegService
10 |
11 |
12 | @dataclass
13 | class ProcessingContext:
14 | """Context object passed through processing pipeline."""
15 |
16 | video_data: dict
17 | uuid: str
18 | media_path: Path
19 | output_path: Path
20 | config: dict
21 | artifacts: dict = field(default_factory=dict)
22 |
23 |
24 | class ProcessingStrategy(ABC):
25 | """Base strategy for video processing steps."""
26 |
27 | @abstractmethod
28 | async def execute(self, context: ProcessingContext) -> ProcessingContext:
29 | """Execute processing step and update context."""
30 |
31 |
32 | class DownloadBackgroundStrategy(ProcessingStrategy):
33 | """Strategy for downloading background video."""
34 |
35 | def __init__(self, downloader: IVideoDownloader, logger: Logger):
36 | self.downloader = downloader
37 | self.logger = logger
38 |
39 | async def execute(self, context: ProcessingContext) -> ProcessingContext:
40 | url = context.config["background_url"]
41 | background_path = self.downloader.download(url, Path("background"))
42 | context.artifacts["background_video"] = background_path
43 | self.logger.info(f"Downloaded background: {background_path}")
44 | return context
45 |
46 |
47 | class TTSGenerationStrategy(ProcessingStrategy):
48 | """Strategy for generating TTS audio."""
49 |
50 | def __init__(self, tts_service: ITTSService, logger: Logger):
51 | self.tts_service = tts_service
52 | self.logger = logger
53 |
54 | async def execute(self, context: ProcessingContext) -> ProcessingContext:
55 | """Integrates TTS into the pipeline"""
56 | text = f"{context.video_data['series']} - {context.video_data['part']}.\n"
57 | text += f"{context.video_data['text']}\n"
58 | text += f"{context.video_data['outro']}"
59 |
60 | output_file = context.media_path / f"{context.uuid}.mp3"
61 | voice = context.config.get("tts_voice", "en-US-ChristopherNeural")
62 |
63 | await self.tts_service.synthesize(text, output_file, voice)
64 | context.artifacts["audio_file"] = output_file
65 |
66 | self.logger.info(f"Generated TTS audio: {output_file}")
67 | return context
68 |
69 |
70 | class TranscriptionStrategy(ProcessingStrategy):
71 | """Strategy for transcribing audio to generate subtitles."""
72 |
73 | def __init__(self, transcription_service: ITranscriptionService, logger: Logger):
74 | self.transcription_service = transcription_service
75 | self.logger = logger
76 |
77 | async def execute(self, context: ProcessingContext) -> ProcessingContext:
78 | audio_file = context.artifacts.get("audio_file")
79 | if not audio_file:
80 | raise ValueError("Audio file not found in context artifacts.")
81 |
82 | srt_file = context.media_path / f"{context.uuid}.srt"
83 | ass_file = context.media_path / f"{context.uuid}.ass"
84 | self.transcription_service.transcribe(
85 | audio_file,
86 | srt_file,
87 | ass_file,
88 | model=context.config["model"],
89 | options=context.config,
90 | )
91 | context.artifacts["srt_file"] = srt_file
92 | context.artifacts["ass_file"] = ass_file
93 | self.logger.info(f"Generated transcription SRT: {srt_file}")
94 | self.logger.info(f"Generated transcription ASS: {ass_file}")
95 | return context
96 |
97 |
98 | class VideoCompositionStrategy(ProcessingStrategy):
99 | """Strategy for composing the final video."""
100 |
101 | def __init__(self, ffmpeg_service: FFmpegService, logger: Logger):
102 | self.ffmpeg_service = ffmpeg_service
103 | self.logger = logger
104 |
105 | async def execute(self, context: ProcessingContext) -> ProcessingContext:
106 | background_video = context.artifacts["background_video"]
107 | audio_file = context.artifacts["audio_file"]
108 | ass_file = context.artifacts["ass_file"]
109 |
110 | # Get video duration and audio duration to calculate start time
111 | audio_info = self.ffmpeg_service.get_media_info(file_path=audio_file)
112 | duration = audio_info.duration
113 | str_duration = audio_info.convert_time(time_in_seconds=duration)
114 |
115 | # Then compose video
116 | output_file = context.output_path / f"{context.uuid}.mp4"
117 |
118 | # Implementation using FFmpegService
119 | self.ffmpeg_service.compose_video(
120 | background=background_video,
121 | audio=audio_file,
122 | subtitles=ass_file,
123 | output=output_file,
124 | start_time=0,
125 | duration=str_duration,
126 | )
127 |
128 | context.artifacts["final_video"] = output_file
129 | self.logger.info(f"Composed video: {output_file}")
130 | return context
131 |
132 |
133 | class TikTokUploadStrategy(ProcessingStrategy):
134 | """Strategy for uploading videos to TikTok."""
135 |
136 | def __init__(self, uploader, logger: Logger):
137 | self.uploader = uploader
138 | self.logger = logger
139 |
140 | async def execute(self, context: ProcessingContext) -> ProcessingContext:
141 | raise NotImplementedError("TikTok upload not implemented yet.")
142 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # Introducing Whisper-TikTok 🤖🎥
2 |
3 | ## Star History
4 |
5 | [](https://star-history.com/#MatteoFasulo/Whisper-TikTok&Date)
6 |
7 | ## Table of Contents
8 |
9 | - [Introduction](#introduction)
10 | - [Video (demo)](#demo-video)
11 | - [Command-Line](#command-line)
12 | - [Usage Examples](#usage-examples)
13 | - [Additional Resources](#additional-resources)
14 | - [Code of Conduct](#code-of-conduct)
15 | - [Contributing](#contributing)
16 | - [Acknowledgments](#acknowledgments)
17 | - [License](#license)
18 |
19 | ## Introduction
20 |
21 | Discover Whisper-TikTok, an innovative AI-powered tool that leverages the prowess of **Edge TTS**, **OpenAI-Whisper**, and **FFMPEG** to craft captivating TikTok videos. Harnessing the capabilities of OpenAI's Whisper model, Whisper-TikTok effortlessly generates an accurate **transcription** from provided audio files, laying the foundation for the creation of mesmerizing TikTok videos through the utilization of **FFMPEG**. Additionally, the program seamlessly integrates the **Microsoft Edge Cloud Text-to-Speech (TTS) API** to lend a vibrant **voiceover** to the video. Opting for Microsoft Edge Cloud TTS API's voiceover is a deliberate choice, as it delivers a remarkably **natural and authentic** auditory experience, setting it apart from the often monotonous and artificial voiceovers prevalent in numerous TikTok videos.
22 |
23 | ## Demo Video
24 |
25 |
26 |
27 | ## Installation 🛠️
28 |
29 | Whisper-TikTok has been tested in Windows 10, Windows 11 and Ubuntu 24.04 systems equipped with **Python versions 3.11, and 3.12**.
30 |
31 | If you want to run Whisper-TikTok locally, you can clone the repository using the following command:
32 |
33 | ```bash
34 | git clone https://github.com/MatteoFasulo/Whisper-TikTok.git
35 | ```
36 |
37 | Install the required dependencies using pip:
38 |
39 | ```python
40 | pip install -r requirements.txt
41 | ```
42 |
43 | However, we encourage the adoption of astral [`uv`](https://docs.astral.sh/uv/) to install the required dependencies. If you are using `uv`, you can install the dependencies with the following command:
44 |
45 | ```bash
46 | uv sync
47 | ```
48 |
49 | Then, install the repository as a package:
50 |
51 | ```bash
52 | pip install -e .
53 | ```
54 |
55 | or
56 |
57 | ```bash
58 | uv pip install -e .
59 | ```
60 |
61 | Binaries for [**FFMPEG**](https://ffmpeg.org/) are not included in the repository and must be installed separately. Make sure to have FFMPEG installed and accessible in your system's PATH. For convenience, here are the installation instructions for various package managers:
62 |
63 | ```bash
64 | # on Ubuntu or Debian
65 | sudo apt update && sudo apt install ffmpeg
66 |
67 | # on Arch Linux
68 | sudo pacman -S ffmpeg
69 |
70 | # on MacOS using Homebrew ()
71 | brew install ffmpeg
72 |
73 | # on Windows using Chocolatey ()
74 | choco install ffmpeg
75 |
76 | # on Windows using Scoop ()
77 | scoop install ffmpeg
78 | ```
79 |
80 | Please note that for optimal performance, it's advisable to have a GPU when using the OpenAI Whisper model for Automatic Speech Recognition (ASR). However, the program will work without a GPU, but it will run more slowly due to CPU limitations.
81 |
82 | ## Command-Line
83 |
84 | To run the program from the command-line, execute the following command within your terminal:
85 |
86 | ```bash
87 | python -m whisper_tiktok.main --help
88 | ```
89 |
90 | which will provide you with a list of available commands.
91 |
92 | ### CLI Options
93 |
94 | Whisper-TikTok supports many command-line options to customize the generated TikTok video. Just to name a few, you can choose the Whisper model to use, the TTS voice, subtitle format, subtitle position, font size, font color, and many more.
95 |
96 | To browse all available options, run the following command:
97 |
98 | ```bash
99 | python -m whisper_tiktok.main create --help
100 | ```
101 |
102 | > If you use the --random_voice option, please specify both --gender and --language arguments. Whisper model will auto-detect the language of the audio file and use the corresponding model.
103 |
104 | ## Usage Examples
105 |
106 | - Generate a TikTok video using a specific TTS voice:
107 |
108 | ```bash
109 | python -m whisper_tiktok.main create --tts en-US-EricNeural
110 | ```
111 |
112 | - Use a custom YouTube video as the background video:
113 |
114 | ```bash
115 | python -m whisper_tiktok.main create --background-url https://www.youtube.com/watch?v=dQw4w9WgXcQ
116 | ```
117 |
118 | - Modify the font color of the subtitles:
119 |
120 | ```bash
121 | python -m whisper_tiktok.main create --font_color FFF000
122 | ```
123 |
124 | - Generate a TikTok video with a random TTS voice:
125 |
126 | ```bash
127 | python -m whisper_tiktok.main create --random_voice --gender Male --language en-US
128 | ```
129 |
130 | - List all available voices:
131 |
132 | ```bash
133 | python -m whisper_tiktok.main list-voices
134 | ```
135 |
136 | you will find a list of available voices together with some information about each voice, such as the tone, style, and suitable scenarios.
137 |
138 | ## Additional Resources
139 |
140 | ### Code of Conduct
141 |
142 | Please review our [Code of Conduct](./CODE_OF_CONDUCT.md) before contributing to Whisper-TikTok.
143 |
144 | ### Contributing
145 |
146 | We welcome contributions from the community! Please see our [Contributing Guidelines](./CONTRIBUTING.md) for more information.
147 |
148 | ## Acknowledgments
149 |
150 | - We'd like to give a huge thanks to [@rany2](https://www.github.com/rany2) for their [edge-tts](https://github.com/rany2/edge-tts) package, which made it possible to use the Microsoft Edge Cloud TTS API with Whisper-TikTok.
151 | - We also acknowledge the contributions of the Whisper model by [@OpenAI](https://github.com/openai/whisper) for robust speech recognition via large-scale weak supervision
152 | - Also [@jianfch](https://github.com/jianfch/stable-ts) for the stable-ts package, which made it possible to use the OpenAI Whisper model with Whisper-TikTok in a stable manner with font color and subtitle format options.
153 |
154 | ### License
155 |
156 | Whisper-TikTok is licensed under the [Apache License, Version 2.0](https://github.com/MatteoFasulo/Whisper-TikTok/blob/main/LICENSE).
157 |
--------------------------------------------------------------------------------
/docs/index.md:
--------------------------------------------------------------------------------
1 | # Introducing Whisper-TikTok 🤖🎥
2 |
3 | ## Star History
4 |
5 | [](https://star-history.com/#MatteoFasulo/Whisper-TikTok&Date)
6 |
7 | ## Table of Contents
8 |
9 | - [Introduction](#introduction)
10 | - [Video (demo)](#demo-video)
11 | - [Command-Line](#command-line)
12 | - [Usage Examples](#usage-examples)
13 | - [Additional Resources](#additional-resources)
14 | - [Code of Conduct](#code-of-conduct)
15 | - [Contributing](#contributing)
16 | - [Acknowledgments](#acknowledgments)
17 | - [License](#license)
18 |
19 | ## Introduction
20 |
21 | Discover Whisper-TikTok, an innovative AI-powered tool that leverages the prowess of **Edge TTS**, **OpenAI-Whisper**, and **FFMPEG** to craft captivating TikTok videos. Harnessing the capabilities of OpenAI's Whisper model, Whisper-TikTok effortlessly generates an accurate **transcription** from provided audio files, laying the foundation for the creation of mesmerizing TikTok videos through the utilization of **FFMPEG**. Additionally, the program seamlessly integrates the **Microsoft Edge Cloud Text-to-Speech (TTS) API** to lend a vibrant **voiceover** to the video. Opting for Microsoft Edge Cloud TTS API's voiceover is a deliberate choice, as it delivers a remarkably **natural and authentic** auditory experience, setting it apart from the often monotonous and artificial voiceovers prevalent in numerous TikTok videos.
22 |
23 | ## Demo Video
24 |
25 |
26 |
27 | ## Installation 🛠️
28 |
29 | Whisper-TikTok has been tested in Windows 10, Windows 11 and Ubuntu 24.04 systems equipped with **Python versions 3.11, and 3.12**.
30 |
31 | If you want to run Whisper-TikTok locally, you can clone the repository using the following command:
32 |
33 | ```bash
34 | git clone https://github.com/MatteoFasulo/Whisper-TikTok.git
35 | ```
36 |
37 | Install the required dependencies using pip:
38 |
39 | ```python
40 | pip install -r requirements.txt
41 | ```
42 |
43 | However, we encourage the adoption of astral [`uv`](https://docs.astral.sh/uv/) to install the required dependencies. If you are using `uv`, you can install the dependencies with the following command:
44 |
45 | ```bash
46 | uv sync
47 | ```
48 |
49 | Then, install the repository as a package:
50 |
51 | ```bash
52 | pip install -e .
53 | ```
54 |
55 | or
56 |
57 | ```bash
58 | uv pip install -e .
59 | ```
60 |
61 | Binaries for [**FFMPEG**](https://ffmpeg.org/) are not included in the repository and must be installed separately. Make sure to have FFMPEG installed and accessible in your system's PATH. For convenience, here are the installation instructions for various package managers:
62 |
63 | ```bash
64 | # on Ubuntu or Debian
65 | sudo apt update && sudo apt install ffmpeg
66 |
67 | # on Arch Linux
68 | sudo pacman -S ffmpeg
69 |
70 | # on MacOS using Homebrew ()
71 | brew install ffmpeg
72 |
73 | # on Windows using Chocolatey ()
74 | choco install ffmpeg
75 |
76 | # on Windows using Scoop ()
77 | scoop install ffmpeg
78 | ```
79 |
80 | Please note that for optimal performance, it's advisable to have a GPU when using the OpenAI Whisper model for Automatic Speech Recognition (ASR). However, the program will work without a GPU, but it will run more slowly due to CPU limitations.
81 |
82 | ## Command-Line
83 |
84 | To run the program from the command-line, execute the following command within your terminal:
85 |
86 | ```bash
87 | python -m whisper_tiktok.main --help
88 | ```
89 |
90 | which will provide you with a list of available commands.
91 |
92 | ### CLI Options
93 |
94 | Whisper-TikTok supports many command-line options to customize the generated TikTok video. Just to name a few, you can choose the Whisper model to use, the TTS voice, subtitle format, subtitle position, font size, font color, and many more.
95 |
96 | To browse all available options, run the following command:
97 |
98 | ```bash
99 | python -m whisper_tiktok.main create --help
100 | ```
101 |
102 | > If you use the --random_voice option, please specify both --gender and --language arguments. Whisper model will auto-detect the language of the audio file and use the corresponding model.
103 |
104 | ## Usage Examples
105 |
106 | - Generate a TikTok video using a specific TTS voice:
107 |
108 | ```bash
109 | python -m whisper_tiktok.main create --tts en-US-EricNeural
110 | ```
111 |
112 | - Use a custom YouTube video as the background video:
113 |
114 | ```bash
115 | python -m whisper_tiktok.main create --background-url https://www.youtube.com/watch?v=dQw4w9WgXcQ
116 | ```
117 |
118 | - Modify the font color of the subtitles:
119 |
120 | ```bash
121 | python -m whisper_tiktok.main create --font_color FFF000
122 | ```
123 |
124 | - Generate a TikTok video with a random TTS voice:
125 |
126 | ```bash
127 | python -m whisper_tiktok.main create --random_voice --gender Male --language en-US
128 | ```
129 |
130 | - List all available voices:
131 |
132 | ```bash
133 | python -m whisper_tiktok.main list-voices
134 | ```
135 |
136 | you will find a list of available voices together with some information about each voice, such as the tone, style, and suitable scenarios.
137 |
138 | ## Additional Resources
139 |
140 | ### Code of Conduct
141 |
142 | Please review our [Code of Conduct](https://github.com/MatteoFasulo/Whisper-TikTok/blob/main/CODE_OF_CONDUCT.md) before contributing to Whisper-TikTok.
143 |
144 | ### Contributing
145 |
146 | We welcome contributions from the community! Please see our [Contributing Guidelines](https://github.com/MatteoFasulo/Whisper-TikTok/blob/main/CONTRIBUTING.md) for more information.
147 |
148 | ## Acknowledgments
149 |
150 | - We'd like to give a huge thanks to [@rany2](https://www.github.com/rany2) for their [edge-tts](https://github.com/rany2/edge-tts) package, which made it possible to use the Microsoft Edge Cloud TTS API with Whisper-TikTok.
151 | - We also acknowledge the contributions of the Whisper model by [@OpenAI](https://github.com/openai/whisper) for robust speech recognition via large-scale weak supervision
152 | - Also [@jianfch](https://github.com/jianfch/stable-ts) for the stable-ts package, which made it possible to use the OpenAI Whisper model with Whisper-TikTok in a stable manner with font color and subtitle format options.
153 |
154 | ### License
155 |
156 | Whisper-TikTok is licensed under the [Apache License, Version 2.0](https://github.com/MatteoFasulo/Whisper-TikTok/blob/main/LICENSE).
157 |
--------------------------------------------------------------------------------
/app.py:
--------------------------------------------------------------------------------
1 | import asyncio
2 | import platform
3 | import shutil
4 | from pathlib import Path
5 |
6 | import pandas as pd
7 | import streamlit as st
8 |
9 | from whisper_tiktok.config.logger_config import setup_logger
10 | from whisper_tiktok.container import Container
11 | from whisper_tiktok.main import Application
12 | from whisper_tiktok.utils.color_utils import rgb_to_bgr
13 | from whisper_tiktok.voice_manager import VoicesManager
14 |
15 |
16 | def _setup_event_loop():
17 | """Setup event loop for Windows if needed."""
18 | if platform.system() == "Windows":
19 | asyncio.set_event_loop_policy(asyncio.WindowsSelectorEventLoopPolicy())
20 |
21 |
22 | def json_to_df(json_file):
23 | return pd.read_json(json_file)
24 |
25 |
26 | def df_to_json(df):
27 | try:
28 | json_str = df.to_json(orient="records", indent=4, force_ascii=False)
29 |
30 | if df.shape[0] == 0:
31 | st.error("You must add at least one video to the JSON")
32 | return
33 |
34 | with open("video.json", "w", encoding="UTF-8") as f:
35 | f.write(json_str)
36 |
37 | st.success("JSON saved successfully!")
38 |
39 | except Exception as e:
40 | st.error(f"An error occurred while saving the JSON: {e}")
41 |
42 |
43 | async def run_pipeline(
44 | model,
45 | background_url,
46 | tts_voice,
47 | random_voice,
48 | gender,
49 | language,
50 | font,
51 | font_size,
52 | font_color,
53 | sub_position,
54 | clean,
55 | verbose,
56 | ):
57 | """Run the video creation pipeline."""
58 | log_dir = Path.cwd() / "logs"
59 | log_level = "DEBUG" if verbose else "INFO"
60 | logger = setup_logger(log_dir, log_level)
61 |
62 | st_log = st.empty()
63 | st_log.info("Starting Whisper TikTok video creation pipeline...")
64 |
65 | try:
66 | # Validate model choice
67 | valid_models = ["tiny", "base", "small", "medium", "large", "turbo"]
68 | if model not in valid_models:
69 | st_log.error(f"Invalid model. Choose from: {', '.join(valid_models)}")
70 | return
71 |
72 | async def get_voice():
73 | nonlocal tts_voice, language
74 | # Handle random voice selection
75 | if random_voice:
76 | if not gender or not language:
77 | st_log.error(
78 | "Both --gender and --language required for random voice"
79 | )
80 | raise ValueError(
81 | "Gender and language are required for random voice."
82 | )
83 |
84 | voices_manager = VoicesManager()
85 | voices_obj = await voices_manager.create()
86 | voice_result = voices_manager.find(voices_obj, gender, language)
87 | tts_voice = voice_result.get("Name") or voice_result.get("ShortName")
88 | st_log.info(f"Selected random voice: {tts_voice}")
89 | else:
90 | # Validate specified voice
91 | voices_manager = VoicesManager()
92 | voices_obj = await voices_manager.create()
93 | extracted_language = "-".join(tts_voice.split("-")[0:2])
94 | voice_result = voices_obj.find(Locale=extracted_language)
95 |
96 | if not voice_result:
97 | st_log.error(
98 | "Voice not found. Run 'whisper-tiktok list-voices' to see available voices"
99 | )
100 | raise ValueError("Voice not found.")
101 |
102 | language = extracted_language
103 | st_log.info(f"Using voice: {tts_voice}")
104 |
105 | try:
106 | await asyncio.create_task(get_voice())
107 | except Exception as e:
108 | st_log.error(f"Voice validation failed: {e}")
109 | return
110 |
111 | # Process font color
112 | processed_font_color = font_color.lower()
113 | if processed_font_color.startswith("#"):
114 | processed_font_color = processed_font_color[1:]
115 | processed_font_color = rgb_to_bgr(processed_font_color)
116 |
117 | # Clean folders if requested
118 | if clean:
119 | st_log.info("Cleaning media and output folders...")
120 | media_path = Path.cwd() / "media"
121 | output_path = Path.cwd() / "output"
122 |
123 | if media_path.exists():
124 | shutil.rmtree(media_path)
125 | st_log.info(f"Removed {media_path}")
126 |
127 | if output_path.exists():
128 | shutil.rmtree(output_path)
129 | st_log.info(f"Removed {output_path}")
130 |
131 | # Setup DI container
132 | container = Container()
133 | config_dict = {
134 | "model": model,
135 | "background_url": background_url,
136 | "tts_voice": tts_voice,
137 | "Fontname": font,
138 | "Fontsize": font_size,
139 | "highlight_color": processed_font_color,
140 | "Alignment": sub_position,
141 | "BorderStyle": "1",
142 | "Outline": "1",
143 | "Shadow": "2",
144 | "Blur": "21",
145 | "MarginL": "0",
146 | "MarginR": "0",
147 | }
148 | container.config.from_dict(config_dict)
149 |
150 | # Run application
151 | app_instance = Application(container, logger)
152 |
153 | await app_instance.run()
154 | st_log.success("Pipeline completed successfully!")
155 |
156 | except Exception as e:
157 | logger.exception("Pipeline failed")
158 | st_log.error(f"Pipeline failed: {e}")
159 |
160 |
161 | async def main():
162 | """Main function to run the Streamlit app."""
163 | # Streamlit Config
164 | st.set_page_config(
165 | page_title="Whisper-TikTok",
166 | page_icon="💬",
167 | layout="wide",
168 | initial_sidebar_state="expanded",
169 | menu_items={
170 | "Get Help": "https://github.com/MatteoFasulo/Whisper-TikTok",
171 | "Report a bug": "https://github.com/MatteoFasulo/Whisper-TikTok/issues",
172 | "About": """
173 | # Whisper-TikTok
174 | Whisper-TikTok is an innovative AI-powered tool that leverages the prowess of Edge TTS, OpenAI-Whisper, and FFMPEG to craft captivating TikTok videos also with a web application interface!
175 |
176 | Mantainer: https://github.com/MatteoFasulo
177 |
178 | If you find a bug or if you just have questions about the project feel free to reach me at https://github.com/MatteoFasulo/Whisper-TikTok
179 | Any contribution to this project is welcome to improve the quality of work!
180 | """,
181 | },
182 | )
183 |
184 | st.page_link(
185 | "https://github.com/MatteoFasulo/Whisper-TikTok", label="GitHub", icon="🔗"
186 | )
187 |
188 | st.title("🏆 Whisper-TikTok 🚀")
189 | st.write(
190 | "Create a TikTok video with text-to-speech of Microsoft Edge's TTS and subtitles of Whisper model."
191 | )
192 |
193 | st.subheader(
194 | "JSON Editor",
195 | help="Here you can edit the JSON file with the videos. Copy-and-paste is supported and compatible with Google Sheets, Excel, and others. You can do bulk-editing by dragging the handle on a cell (similar to Excel)!",
196 | )
197 | st.write(
198 | "ℹ️ The JSON file is saved automatically when you click the button below. Every time you edit the JSON file, you must click the button to save the changes otherwise they will be lost."
199 | )
200 | edited_df = st.data_editor(
201 | json_to_df("video.json"),
202 | num_rows="dynamic",
203 | )
204 | st.button(
205 | "Save JSON",
206 | on_click=df_to_json,
207 | args=(edited_df,),
208 | help="Save the JSON file with the videos",
209 | )
210 |
211 | st.divider()
212 |
213 | st.subheader("🚀 Run Pipeline")
214 |
215 | col1, col2 = st.columns(2)
216 |
217 | with col1:
218 | st.write("#### General Settings")
219 | model = st.selectbox(
220 | "Whisper model size",
221 | ["tiny", "base", "small", "medium", "large", "turbo"],
222 | index=5,
223 | help="Whisper model size [tiny|base|small|medium|large|turbo]",
224 | )
225 | background_url = st.text_input(
226 | "Background Video URL",
227 | "https://www.youtube.com/watch?v=dQw4w9WgXcQ",
228 | help="YouTube URL for background video",
229 | )
230 | clean = st.checkbox(
231 | "Clean Folders", help="Clean media and output folders before processing"
232 | )
233 | verbose = st.checkbox("Verbose Logging", help="Enable verbose logging")
234 |
235 | with col2:
236 | st.write("#### Voice Settings")
237 | random_voice = st.checkbox("Use Random Voice", help="Use random TTS voice")
238 | if random_voice:
239 | gender = st.selectbox(
240 | "Gender", ["Male", "Female"], help="Gender for random voice"
241 | )
242 | language = st.text_input(
243 | "Language", "en-US", help="Language for random voice (e.g., en-US)"
244 | )
245 | tts_voice = ""
246 | else:
247 | tts_voice = st.text_input(
248 | "TTS Voice",
249 | "en-US-ChristopherNeural",
250 | help="TTS voice to use. See available voices with `whisper-tiktok list-voices`",
251 | )
252 | gender = None
253 | language = None
254 |
255 | st.write("#### Subtitle Settings")
256 | col3, col4, col5 = st.columns(3)
257 | with col3:
258 | font = st.text_input("Font", "Lexend Bold", help="Subtitle font")
259 | font_size = st.number_input("Font Size", value=21, help="Subtitle font size")
260 | with col4:
261 | font_color = st.color_picker(
262 | "Font Color", "#FFF000", help="Subtitle color (hex format)"
263 | )
264 | with col5:
265 | sub_position = st.slider(
266 | "Subtitle Position",
267 | min_value=1,
268 | max_value=9,
269 | value=5,
270 | help="Subtitle position (1-9), refer to FFMPEG documentation and ASS subtitle format for positioning",
271 | )
272 |
273 | if st.button("Run Video Creation Pipeline", type="primary"):
274 | _setup_event_loop()
275 | asyncio.run(
276 | run_pipeline(
277 | model,
278 | background_url,
279 | tts_voice,
280 | random_voice,
281 | gender,
282 | language,
283 | font,
284 | font_size,
285 | font_color,
286 | sub_position,
287 | clean,
288 | verbose,
289 | )
290 | )
291 |
292 |
293 | if __name__ == "__main__":
294 | asyncio.run(main())
295 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | Apache License
2 | Version 2.0, January 2004
3 | http://www.apache.org/licenses/
4 |
5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6 |
7 | 1. Definitions.
8 |
9 | "License" shall mean the terms and conditions for use, reproduction,
10 | and distribution as defined by Sections 1 through 9 of this document.
11 |
12 | "Licensor" shall mean the copyright owner or entity authorized by
13 | the copyright owner that is granting the License.
14 |
15 | "Legal Entity" shall mean the union of the acting entity and all
16 | other entities that control, are controlled by, or are under common
17 | control with that entity. For the purposes of this definition,
18 | "control" means (i) the power, direct or indirect, to cause the
19 | direction or management of such entity, whether by contract or
20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the
21 | outstanding shares, or (iii) beneficial ownership of such entity.
22 |
23 | "You" (or "Your") shall mean an individual or Legal Entity
24 | exercising permissions granted by this License.
25 |
26 | "Source" form shall mean the preferred form for making modifications,
27 | including but not limited to software source code, documentation
28 | source, and configuration files.
29 |
30 | "Object" form shall mean any form resulting from mechanical
31 | transformation or translation of a Source form, including but
32 | not limited to compiled object code, generated documentation,
33 | and conversions to other media types.
34 |
35 | "Work" shall mean the work of authorship, whether in Source or
36 | Object form, made available under the License, as indicated by a
37 | copyright notice that is included in or attached to the work
38 | (an example is provided in the Appendix below).
39 |
40 | "Derivative Works" shall mean any work, whether in Source or Object
41 | form, that is based on (or derived from) the Work and for which the
42 | editorial revisions, annotations, elaborations, or other modifications
43 | represent, as a whole, an original work of authorship. For the purposes
44 | of this License, Derivative Works shall not include works that remain
45 | separable from, or merely link (or bind by name) to the interfaces of,
46 | the Work and Derivative Works thereof.
47 |
48 | "Contribution" shall mean any work of authorship, including
49 | the original version of the Work and any modifications or additions
50 | to that Work or Derivative Works thereof, that is intentionally
51 | submitted to Licensor for inclusion in the Work by the copyright owner
52 | or by an individual or Legal Entity authorized to submit on behalf of
53 | the copyright owner. For the purposes of this definition, "submitted"
54 | means any form of electronic, verbal, or written communication sent
55 | to the Licensor or its representatives, including but not limited to
56 | communication on electronic mailing lists, source code control systems,
57 | and issue tracking systems that are managed by, or on behalf of, the
58 | Licensor for the purpose of discussing and improving the Work, but
59 | excluding communication that is conspicuously marked or otherwise
60 | designated in writing by the copyright owner as "Not a Contribution."
61 |
62 | "Contributor" shall mean Licensor and any individual or Legal Entity
63 | on behalf of whom a Contribution has been received by Licensor and
64 | subsequently incorporated within the Work.
65 |
66 | 2. Grant of Copyright License. Subject to the terms and conditions of
67 | this License, each Contributor hereby grants to You a perpetual,
68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable
69 | copyright license to reproduce, prepare Derivative Works of,
70 | publicly display, publicly perform, sublicense, and distribute the
71 | Work and such Derivative Works in Source or Object form.
72 |
73 | 3. Grant of Patent License. Subject to the terms and conditions of
74 | this License, each Contributor hereby grants to You a perpetual,
75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable
76 | (except as stated in this section) patent license to make, have made,
77 | use, offer to sell, sell, import, and otherwise transfer the Work,
78 | where such license applies only to those patent claims licensable
79 | by such Contributor that are necessarily infringed by their
80 | Contribution(s) alone or by combination of their Contribution(s)
81 | with the Work to which such Contribution(s) was submitted. If You
82 | institute patent litigation against any entity (including a
83 | cross-claim or counterclaim in a lawsuit) alleging that the Work
84 | or a Contribution incorporated within the Work constitutes direct
85 | or contributory patent infringement, then any patent licenses
86 | granted to You under this License for that Work shall terminate
87 | as of the date such litigation is filed.
88 |
89 | 4. Redistribution. You may reproduce and distribute copies of the
90 | Work or Derivative Works thereof in any medium, with or without
91 | modifications, and in Source or Object form, provided that You
92 | meet the following conditions:
93 |
94 | (a) You must give any other recipients of the Work or
95 | Derivative Works a copy of this License; and
96 |
97 | (b) You must cause any modified files to carry prominent notices
98 | stating that You changed the files; and
99 |
100 | (c) You must retain, in the Source form of any Derivative Works
101 | that You distribute, all copyright, patent, trademark, and
102 | attribution notices from the Source form of the Work,
103 | excluding those notices that do not pertain to any part of
104 | the Derivative Works; and
105 |
106 | (d) If the Work includes a "NOTICE" text file as part of its
107 | distribution, then any Derivative Works that You distribute must
108 | include a readable copy of the attribution notices contained
109 | within such NOTICE file, excluding those notices that do not
110 | pertain to any part of the Derivative Works, in at least one
111 | of the following places: within a NOTICE text file distributed
112 | as part of the Derivative Works; within the Source form or
113 | documentation, if provided along with the Derivative Works; or,
114 | within a display generated by the Derivative Works, if and
115 | wherever such third-party notices normally appear. The contents
116 | of the NOTICE file are for informational purposes only and
117 | do not modify the License. You may add Your own attribution
118 | notices within Derivative Works that You distribute, alongside
119 | or as an addendum to the NOTICE text from the Work, provided
120 | that such additional attribution notices cannot be construed
121 | as modifying the License.
122 |
123 | You may add Your own copyright statement to Your modifications and
124 | may provide additional or different license terms and conditions
125 | for use, reproduction, or distribution of Your modifications, or
126 | for any such Derivative Works as a whole, provided Your use,
127 | reproduction, and distribution of the Work otherwise complies with
128 | the conditions stated in this License.
129 |
130 | 5. Submission of Contributions. Unless You explicitly state otherwise,
131 | any Contribution intentionally submitted for inclusion in the Work
132 | by You to the Licensor shall be under the terms and conditions of
133 | this License, without any additional terms or conditions.
134 | Notwithstanding the above, nothing herein shall supersede or modify
135 | the terms of any separate license agreement you may have executed
136 | with Licensor regarding such Contributions.
137 |
138 | 6. Trademarks. This License does not grant permission to use the trade
139 | names, trademarks, service marks, or product names of the Licensor,
140 | except as required for reasonable and customary use in describing the
141 | origin of the Work and reproducing the content of the NOTICE file.
142 |
143 | 7. Disclaimer of Warranty. Unless required by applicable law or
144 | agreed to in writing, Licensor provides the Work (and each
145 | Contributor provides its Contributions) on an "AS IS" BASIS,
146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
147 | implied, including, without limitation, any warranties or conditions
148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
149 | PARTICULAR PURPOSE. You are solely responsible for determining the
150 | appropriateness of using or redistributing the Work and assume any
151 | risks associated with Your exercise of permissions under this License.
152 |
153 | 8. Limitation of Liability. In no event and under no legal theory,
154 | whether in tort (including negligence), contract, or otherwise,
155 | unless required by applicable law (such as deliberate and grossly
156 | negligent acts) or agreed to in writing, shall any Contributor be
157 | liable to You for damages, including any direct, indirect, special,
158 | incidental, or consequential damages of any character arising as a
159 | result of this License or out of the use or inability to use the
160 | Work (including but not limited to damages for loss of goodwill,
161 | work stoppage, computer failure or malfunction, or any and all
162 | other commercial damages or losses), even if such Contributor
163 | has been advised of the possibility of such damages.
164 |
165 | 9. Accepting Warranty or Additional Liability. While redistributing
166 | the Work or Derivative Works thereof, You may choose to offer,
167 | and charge a fee for, acceptance of support, warranty, indemnity,
168 | or other liability obligations and/or rights consistent with this
169 | License. However, in accepting such obligations, You may act only
170 | on Your own behalf and on Your sole responsibility, not on behalf
171 | of any other Contributor, and only if You agree to indemnify,
172 | defend, and hold each Contributor harmless for any liability
173 | incurred by, or claims asserted against, such Contributor by reason
174 | of your accepting any such warranty or additional liability.
175 |
176 | END OF TERMS AND CONDITIONS
177 |
178 | APPENDIX: How to apply the Apache License to your work.
179 |
180 | To apply the Apache License to your work, attach the following
181 | boilerplate notice, with the fields enclosed by brackets "[]"
182 | replaced with your own identifying information. (Don't include
183 | the brackets!) The text should be enclosed in the appropriate
184 | comment syntax for the file format. We also recommend that a
185 | file or class name and description of purpose be included on the
186 | same "printed page" as the copyright notice for easier
187 | identification within third-party archives.
188 |
189 | Copyright [2023] [Matteo Fasulo]
190 |
191 | Licensed under the Apache License, Version 2.0 (the "License");
192 | you may not use this file except in compliance with the License.
193 | You may obtain a copy of the License at
194 |
195 | http://www.apache.org/licenses/LICENSE-2.0
196 |
197 | Unless required by applicable law or agreed to in writing, software
198 | distributed under the License is distributed on an "AS IS" BASIS,
199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
200 | See the License for the specific language governing permissions and
201 | limitations under the License.
202 |
--------------------------------------------------------------------------------
/whisper_tiktok/main.py:
--------------------------------------------------------------------------------
1 | """Main module for the Whisper TikTok application."""
2 |
3 | import asyncio
4 | import json
5 | import logging
6 | import platform
7 | import shutil
8 | from pathlib import Path
9 | from typing import Optional
10 |
11 | import typer
12 | from rich.console import Console
13 | from rich.table import Table
14 |
15 | from whisper_tiktok.config.logger_config import setup_logger
16 | from whisper_tiktok.container import Container
17 | from whisper_tiktok.factories.video_factory import VideoCreatorFactory
18 | from whisper_tiktok.utils.color_utils import rgb_to_bgr
19 | from whisper_tiktok.voice_manager import VoicesManager
20 |
21 | # Create Typer app
22 | app = typer.Typer(
23 | name="whisper-tiktok",
24 | help="Whisper TikTok - Video generation pipeline",
25 | add_completion=False,
26 | )
27 | console = Console()
28 |
29 |
30 | @app.callback()
31 | def main():
32 | """Whisper TikTok - Create TikTok videos with AI-generated subtitles."""
33 |
34 |
35 | @app.command()
36 | def list_voices(
37 | language: Optional[str] = typer.Option(
38 | None,
39 | "--language",
40 | "-l",
41 | help="Filter by language (e.g., en-US)",
42 | ),
43 | gender: Optional[str] = typer.Option(
44 | None,
45 | "--gender",
46 | "-g",
47 | help="Filter by gender (Male/Female)",
48 | ),
49 | ):
50 | """List available TTS voices."""
51 |
52 | async def _list_voices():
53 | voices_manager = VoicesManager()
54 | voices_obj = await voices_manager.create()
55 |
56 | # Get all voices
57 | voices = voices_obj.voices
58 |
59 | # Apply filters
60 | if language:
61 | voices = [v for v in voices if v.get("Locale", "").startswith(language)]
62 | if gender:
63 | voices = [v for v in voices if v.get("Gender", "") == gender]
64 |
65 | # Display in a table
66 | table = Table(title="Available TTS Voices")
67 | table.add_column("Name", style="cyan")
68 | table.add_column("Locale", style="green")
69 | table.add_column("Gender", style="magenta")
70 | table.add_column("Voice Personalities", style="blue")
71 | table.add_column("Scenarios", style="yellow")
72 |
73 | for voice in sorted(voices, key=lambda x: x.get("Locale", "")):
74 | table.add_row(
75 | voice.get("ShortName", ""),
76 | voice.get("Locale", ""),
77 | voice.get("Gender", ""),
78 | ", ".join(voice["VoiceTag"].get("VoicePersonalities", [])),
79 | ", ".join(voice["VoiceTag"].get("TailoredScenarios", [])),
80 | )
81 |
82 | console.print(table)
83 | console.print(f"\n[dim]Total: {len(voices)} voices[/dim]")
84 |
85 | asyncio.run(_list_voices())
86 |
87 |
88 | @app.command()
89 | def create(
90 | model: str = typer.Option(
91 | "turbo",
92 | "--model",
93 | "-m",
94 | help="Whisper model size [tiny|base|small|medium|large|turbo]",
95 | ),
96 | background_url: str = typer.Option(
97 | "https://www.youtube.com/watch?v=intRX7BRA90",
98 | "--background-url",
99 | "-u",
100 | help="YouTube URL for background video",
101 | ),
102 | tts_voice: str = typer.Option(
103 | "en-US-ChristopherNeural",
104 | "--tts",
105 | "-v",
106 | help="TTS voice to use",
107 | ),
108 | random_voice: bool = typer.Option(
109 | False,
110 | "--random-voice",
111 | help="Use random TTS voice",
112 | ),
113 | gender: Optional[str] = typer.Option(
114 | None,
115 | "--gender",
116 | "-g",
117 | help="Gender for random voice (Male/Female)",
118 | ),
119 | language: Optional[str] = typer.Option(
120 | None,
121 | "--language",
122 | "-l",
123 | help="Language for random voice (e.g., en-US)",
124 | ),
125 | font: str = typer.Option(
126 | "Lexend Bold",
127 | "--font",
128 | "-f",
129 | help="Subtitle font",
130 | ),
131 | font_size: int = typer.Option(
132 | 21,
133 | "--font-size",
134 | help="Subtitle font size",
135 | ),
136 | font_color: str = typer.Option(
137 | "FFF000",
138 | "--font-color",
139 | "-c",
140 | help="Subtitle color (hex format)",
141 | ),
142 | sub_position: int = typer.Option(
143 | 5,
144 | "--sub-position",
145 | "-p",
146 | help="Subtitle position (1-9)",
147 | min=1,
148 | max=9,
149 | ),
150 | upload_tiktok: bool = typer.Option(
151 | False,
152 | "--upload-tiktok",
153 | help="Upload to TikTok",
154 | ),
155 | clean: bool = typer.Option(
156 | False,
157 | "--clean",
158 | help="Clean media and output folders before processing",
159 | ),
160 | verbose: bool = typer.Option(
161 | False,
162 | "--verbose",
163 | help="Enable verbose logging",
164 | ),
165 | ):
166 | """Create videos from text content."""
167 |
168 | # Setup logging
169 | log_dir = Path.cwd() / "logs"
170 | log_level = "DEBUG" if verbose else "INFO"
171 | logger = setup_logger(log_dir, log_level)
172 |
173 | async def _create():
174 | nonlocal tts_voice, language
175 |
176 | logger.info("=" * 60)
177 | logger.info("Starting Whisper TikTok video creation pipeline")
178 | logger.info("=" * 60)
179 |
180 | # Validate model choice
181 | valid_models = ["tiny", "base", "small", "medium", "large", "turbo"]
182 | if model not in valid_models:
183 | logger.error("Invalid model. Choose from: %s", ", ".join(valid_models))
184 | raise typer.Exit(code=1)
185 |
186 | # Handle random voice selection
187 | if random_voice:
188 | if not gender or not language:
189 | logger.error("Both --gender and --language required for random voice")
190 | raise typer.Exit(code=1)
191 |
192 | try:
193 | voices_manager = VoicesManager()
194 | voices_obj = await voices_manager.create()
195 | voice_result = voices_manager.find(voices_obj, gender, language)
196 | tts_voice = voice_result.get("Name") or voice_result.get("ShortName")
197 | logger.info("Selected random voice: %s", tts_voice)
198 | except Exception as e:
199 | logger.error("Failed to select random voice: %s", e)
200 | raise typer.Exit(code=1) from e
201 | else:
202 | # Validate specified voice
203 | try:
204 | voices_manager = VoicesManager()
205 | voices_obj = await voices_manager.create()
206 | extracted_language = "-".join(tts_voice.split("-")[0:2])
207 | voice_result = voices_obj.find(Locale=extracted_language)
208 |
209 | if not voice_result:
210 | logger.error(
211 | "Voice not found. Run 'whisper-tiktok list-voices' to see available voices"
212 | )
213 | raise typer.Exit(code=1)
214 |
215 | language = extracted_language
216 | logger.info("Using voice: %s", tts_voice)
217 |
218 | except Exception as e:
219 | logger.error("Voice validation failed: %s", e)
220 | raise typer.Exit(code=1) from e
221 |
222 | # Process font color
223 | processed_font_color = font_color.lower()
224 | if processed_font_color.startswith("#"):
225 | processed_font_color = processed_font_color[1:]
226 | processed_font_color = rgb_to_bgr(processed_font_color)
227 |
228 | # Clean folders if requested
229 | if clean:
230 | logger.info("Cleaning media and output folders...")
231 | media_path = Path.cwd() / "media"
232 | output_path = Path.cwd() / "output"
233 |
234 | if media_path.exists():
235 | shutil.rmtree(media_path)
236 | logger.info("Removed %s", media_path)
237 |
238 | if output_path.exists():
239 | shutil.rmtree(output_path)
240 | logger.info("Removed %s", output_path)
241 | # Display startup info
242 | console.print("\n[bold green]🎬 Starting video creation pipeline…[/bold green]")
243 | console.print(f" [cyan]Model:[/cyan] {model}")
244 | console.print(f" [cyan]Voice:[/cyan] {tts_voice}")
245 | console.print(f" [cyan]Language:[/cyan] {language}\n")
246 |
247 | # Setup DI container
248 | container = Container()
249 | config_dict = {
250 | "model": model,
251 | "background_url": background_url,
252 | "tts_voice": tts_voice,
253 | "upload_tiktok": upload_tiktok,
254 | "Fontname": font,
255 | "Fontsize": font_size,
256 | "highlight_color": processed_font_color,
257 | "Alignment": sub_position,
258 | "BorderStyle": "1",
259 | "Outline": "1",
260 | "Shadow": "2",
261 | "Blur": "21",
262 | "MarginL": "0",
263 | "MarginR": "0",
264 | }
265 | container.config.from_dict(config_dict)
266 |
267 | # Run application
268 | app_instance = Application(container, logger)
269 |
270 | try:
271 | await app_instance.run()
272 | console.print(
273 | "\n[bold green]✅ Pipeline completed successfully![/bold green]"
274 | )
275 | except Exception as e:
276 | logger.exception("Pipeline failed")
277 | console.print(f"\n[bold red]❌ Pipeline failed: {e}[/bold red]")
278 | raise typer.Exit(code=1) from e
279 |
280 | # Run the async function
281 | asyncio.run(_create())
282 |
283 |
284 | class Application:
285 | """Main application orchestrator."""
286 |
287 | def __init__(self, container: Container, logger: logging.Logger):
288 | self.container = container
289 | self.logger = logger
290 | self.factory = VideoCreatorFactory(container)
291 |
292 | def _load_video_data(self) -> list[dict]:
293 | """Load video data from JSON file."""
294 | video_json_path = Path.cwd() / "video.json"
295 |
296 | try:
297 | data: list[dict] = json.loads(video_json_path.read_text(encoding="utf-8"))
298 | self.logger.info(f"Loaded {len(data)} videos from video.json")
299 | return data
300 | except FileNotFoundError:
301 | self.logger.error(f"video.json not found at {video_json_path}")
302 | raise
303 | except json.JSONDecodeError as e:
304 | self.logger.error(f"Invalid JSON in video.json: {e}")
305 | raise
306 |
307 | def _build_config(self) -> dict:
308 | """Build configuration from container."""
309 | return dict(self.container.config())
310 |
311 | async def run(self):
312 | """Run the video creation pipeline."""
313 |
314 | # Load video data
315 | video_data = self._load_video_data()
316 | config = self._build_config()
317 |
318 | # Process each video
319 | for idx, video in enumerate(video_data, 1):
320 | self.logger.info(
321 | f"Processing video {idx}/{len(video_data)}: {video.get('series', 'Unknown')}"
322 | )
323 | await self._process_video(video, config)
324 |
325 | async def _process_video(self, video: dict, config: dict):
326 | """Process a single video."""
327 |
328 | processor = self.factory.create_processor(video, config)
329 |
330 | try:
331 | result = await processor.process()
332 | self.logger.info(f"✓ Video created: {result.output_path}")
333 | except Exception:
334 | self.logger.exception(
335 | f"✗ Failed to process video: {video.get('series', 'Unknown')}"
336 | )
337 | raise
338 |
339 |
340 | def _setup_event_loop():
341 | """Setup event loop for Windows if needed."""
342 | if platform.system() == "Windows":
343 | asyncio.set_event_loop_policy(asyncio.WindowsSelectorEventLoopPolicy())
344 |
345 |
346 | if __name__ == "__main__":
347 | _setup_event_loop()
348 | app()
349 |
--------------------------------------------------------------------------------