transcribe-anything

repository·main·Indexed 23 days ago

https://github.com/zackees/transcribe-anything

A unified CLI tool and Python API for transcribing speech from video and audio files, including URLs from YouTube, Rumble, and BitChute. It provides a router to multiple Whisper-based backends—including WhisperX, MLX, Insanely Fast Whisper, SenseVoice, and XPU—each running in isolated environments to prevent dependency conflicts. Features include speaker diarization, phoneme-precise timestamps via forced alignment, and hardware optimizations for Mac Apple Silicon, NVIDIA GPUs, and Intel Arc GPUs.

Tokens
14.7K
Snippets
36
Records
88
Agent score
80%

What's inside transcribe-anything

  1. Overview of transcribe-anything

    main

    transcribe-anything

    transcribe-anything is a Python CLI tool and library that provides a unified interface for transcribing audio/video content using multiple Whisper AI backends. It supports GPU acceleration, speaker diarization, and multi-platform optimization (Mac/Linux/Win).

    Key Features:

    • Multiple Whisper backends: CPU, CUDA, "insane" (GPU-accelerated), WhisperX, and MLX (Mac Apple Silicon).
    • Speaker diarization: Generates speaker.json output on supported backends.
    • Flexible Inputs: Supports local files and online URLs (YouTube, Rumble, etc.).
    • Subtitle Support: Embedding subtitles into video files.
    • Customization: Custom vocabulary support via initial prompts.
    • Deployment: Docker containerization with GPU support and isolated environment management.
  2. Available Whisper Backends

    main

    The project supports several specialized backends, each optimized for different hardware and use cases:

    BackendDevice FlagUse CaseKey Features
    Standard Whispercpu / cudaCPU processing or universal CUDA compatibilityFull OpenAI Whisper argument support
    Insanely Fast WhisperinsaneHigh-speed GPU accelerationBatch processing, Flash Attention 2, speaker diarization
    WhisperXwhisperxAdvanced alignment and diarizationForced alignment, word timing, VAD controls, speaker diarization
    MLXmlxApple Silicon (Mac) optimization~4x faster than MPS backend, multi-language support

    WhisperX Specific Options: When using --device whisperx, you can control:

    • --compute_type
    • --batch_size
    • --vad_method
    • --chunk_size
    • Diarization: --diarize, --hf_token, --min_speakers, and --max_speakers.
  3. Configure Low-Latency Streaming Modes

    main

    When using the --live mode, you can select a latency profile to balance speed and accuracy.

    ModeModelLatencyAccuracyUse Case
    Ultra-Lowtiny + greedy200-400ms85%Live captions, gaming
    Lowbase + greedy300-600ms90%Meetings, dictation
    Balancedsmall + beam=3500-1000ms93%General use
    High Qualitylarge-v3 + beam=51000-2000ms96%Archival, accuracy-critical

    To achieve the lowest latency, use beam_size=1 (greedy decoding) and smaller models like tiny or base.

  4. Configure speaker diarization and speaker.json

    main

    To generate a speaker.json file (which contains de-chunkified speaker-assigned text), you must use a diarization-capable backend like --device insane or --device whisperx and provide a Hugging Face token.

    Requirements:

    1. Provide a Hugging Face token via --hf_token (CLI) or hugging_face_token (API).
    2. You must manually agree to the user policies for pyannote.audio at https://huggingface.co/pyannote/segmentation-3.0 to avoid runtime exceptions.

    Output Format: speaker.json provides an array of objects containing speaker (e.g., SPEAKER_00), timestamp, text, and reason (e.g., beginning or speaker-switch).

    [
      {
        "speaker": "SPEAKER_00",
        "timestamp": [0.0, 7.44],
        "text": "for that. But welcome, Zach Vorhees. Great to have you back on. Thank you, Matt. Craving me back onto your show. Man, we got a lot to talk about.",
        "reason": "beginning"
      }
    ]
  5. Understand the Isolated Environment Architecture

    main

    To manage complex and conflicting AI dependencies (like different versions of PyTorch or Whisper), transcribe-anything uses an isolated environment pattern via uv-iso-env:

    1. Parent Environment: Contains the main CLI and coordination logic.
    2. Backend Environments: Each Whisper backend (e.g., whisper, whisper_mlx) resides in its own isolated Python environment.
    3. Communication: The parent environment communicates with backend environments using subprocess-based IPC.

    This ensures that installing a new backend or updating a dependency for one backend does not break the others.

  6. Run transcribe-anything in Daemon Mode

    main

    Daemon mode runs a long-running FastAPI server to avoid the high cold-start costs of loading models and initializing CUDA/torch. This is ideal for batch workflows or shared GPU hosts.

    Deployment Modes

    • Local: Binds to 127.0.0.1:8765 with no authentication. Use for local development.
    • Public: Binds to --host 0.0.0.0. Authentication is required via --auth-token or --auth-token-env.

    Prefetch Policies

    Use the --prefetch flag to control model loading:

    • lazy (default): Boots immediately; the first request triggers the model download.
    • eager: Blocks /healthz until a warmup transcription completes. Best for hosted deployments.
    • none: Refuses requests until weights are already cached locally.

    Note: The daemon locks the backend, HF token, and prefetch policy at startup. To allow clients to request different Whisper variants, use the --allow-client-model flag.

    # Start a local daemon with eager prefetching
    transcribe-anything serve --device insane --model large-v3 --prefetch eager
    
    # Start a public daemon with auth via environment variable
    TRANSCRIBE_ANYTHING_TOKEN=$(openssl rand -hex 32) \
      transcribe-anything serve --host 0.0.0.0 --auth-token-env TRANSCRIBE_ANYTHING_TOKEN
  7. How transcribe-anything works

    main

    The application uses a single CLI and a router mechanism to manage different transcription backends. When you run a command, the --device flag (or auto-detection) routes the task to the appropriate backend.

    To prevent dependency conflicts (like CUDA or Torch version mismatches), each backend runs in its own isolated environment (using uv virtual environments) which is built automatically on the first use.

    Workflow:

    1. Input: Accepts local files, YouTube links, or any URL.
    2. Fetch: Uses yt-dlp and static-ffmpeg to normalize audio to 16-bit WAV.
    3. Router: Selects the backend via --device.
    4. Execution: Runs the transcription in an isolated environment.
    5. Output: Generates out.txt, out.srt, out.vtt, out.json, and speaker.json (containing speaker partitions).
  8. Select a transcription backend using --device

    main

    Choose a backend based on your hardware and specific needs (alignment, diarization, or speed).

    • MLX (--device mlx): Optimized for Mac Apple Silicon. Focused feature set.
    • Insanely Fast (--device insane): Best for Windows/Linux GPUs. Uses a transformer-based architecture.
    • Insane Flash (--device insane-flash): For CUDA GPUs with verified FlashAttention2. Use this instead of manually passing --flash True to ensure correct setup.
    • WhisperX (--device whisperx): Best for alignment, diarization, and word-level timing. An additive backend, not a replacement for insane.
    • SenseVoice (--device sensevoice): Multilingual (zh/en/yue/ja/ko) with built-in VAD and emotion detection. Uses FunASR. Use --hub hf to use HuggingFace instead of ModelScope.
    • CPU (--device cpu): Universal compatibility; supports full OpenAI Whisper arguments.
  9. Understand the Live Transcription data flow

    main

    Live transcription follows a producer-consumer pattern to ensure real-time performance. The flow is as follows:

    1. Hotkey Press: Triggers the process.
    2. Permission Check: Verifies microphone access.
    3. Audio Capture: A dedicated thread captures audio from the microphone into an Audio Buffer.
    4. VAD Detection: A Voice Activity Detector (Silero VAD) monitors the buffer. If speech is detected, audio is moved to an Audio Queue.
    5. Transcription: A transcription thread consumes the queue, sending audio to the Whisper Backend (e.g., faster-whisper).
    6. Output: The resulting text is sent to stdout or a file.
  10. Understand the isolated backend environment architecture

    main

    The project uses a unique architecture where complex AI model dependencies are siloed into separate environments using uv-iso-env pyproject.toml files for each backend.

    Key architectural constraints:

    • Communication: The parent environment and backend environments communicate exclusively via subprocess.Popen. You cannot call functions in a sub-environment directly from the parent environment.
    • Backend Isolation: Each backend (e.g., WhisperX, Insanely Fast Whisper) maintains its own virtual environment (e.g., venv/insanely_fast_whisper).
    • FlashAttention: The --device insane-flash backend uses a specific environment (venv/insanely_fast_whisper_flash) that forces and verifies FlashAttention2. This dependency is pinned via direct wheel URL and sha256 in src/transcribe_anything/flash_attention_wheels.py to ensure stability.
  11. Understand the NVIDIA detection cache mechanism

    main

    To ensure consistent environment generation and avoid repeated 2.2GB+ torch downloads, transcribe-anything uses a caching mechanism for NVIDIA detection.

    How it works:

    1. The system generates a fingerprint based on the platform (Windows, Linux, Darwin), machine architecture (AMD64, x86_64, arm64), platform version, and the existence of the nvidia-smi executable.
    2. The result of the has_nvidia_smi() check is stored in ~/.transcribe_anything_nvidia_cache.json mapped to that fingerprint.
    3. Subsequent runs use the cached result if the fingerprint matches, ensuring the generated pyproject.toml remains identical and avoids triggering a uv-iso-env reinstall.

    Cache File Format:

    {
      "Windows-AMD64-10.0.19041-nvidia_smi:true": true,
      "Linux-x86_64-5.4.0-nvidia_smi:false": false
    }
  12. Optimize transcription performance

    main

    To speed up processing, increase the --batch_size (if GPU memory permits), enable Flash Attention 2 for insane mode, use smaller models, or use distilled models.

    # Increase batch size
    transcribe-anything video.mp4 --device mlx --batch_size 24
    transcribe-anything video.mp4 --device insane --batch-size 16
    transcribe-anything video.mp4 --device whisperx --batch_size 16
    
    # Enable Flash Attention 2 for insane mode
    transcribe-anything video.mp4 --device insane --flash True --batch-size 16
    
    # Use smaller model for speed
    transcribe-anything video.mp4 --device insane --model small
    
    # Use distilled models for even faster processing
    transcribe-anything video.mp4 --device insane --model distil-whisper/large-v2 --flash True