HunyuanVideo-Foley

repository·main·Indexed 21 days ago

https://github.com/tencent-hunyuan/hunyuanvideo-foley

A professional-grade AI tool using multimodal diffusion to generate high-fidelity (48kHz) Foley audio synchronized with video content. It features a hybrid architecture with Multimodal and Unimodal Transformer blocks and a Synchformer-based synchronization mechanism. Available in XXL and XL model sizes, it supports single video generation, batch processing via CSV, and a Gradio web interface.

Tokens
4.1K
Snippets
14
Records
23
Agent score
77%

What's inside HunyuanVideo-Foley

  1. Overview of HunyuanVideo-Foley

    main
    HunyuanVideo-Foley is an end-to-end multimodal diffusion model designed for high-fidelity Foley audio generation. It generates professional-grade sound effects (48kHz) that are semantically and temporally synchronized with video content. The model balances visual and textual information to ensure high-quality audio-visual alignment, making it suitable for film production, gaming, and short video creation.
  2. Technical Architecture of HunyuanVideo-Foley

    main

    HunyuanVideo-Foley uses a hybrid architecture to achieve high-fidelity audio-visual synchronization:

    • Multimodal Transformer Blocks: Process visual and audio streams simultaneously.
    • Unimodal Transformer Blocks: Focus on refining the audio stream.
    • Visual Encoding: Uses a pre-trained encoder to extract features from video frames.
    • Text Processing: Extracts semantic features via a pre-trained text encoder.
    • Audio Encoding: Uses latent representations with Gaussian noise perturbation.
    • Temporal Alignment: Employs a Synchformer-based frame-level synchronization mechanism with gated modulation.
  3. Project structure overview

    main

    The repository is organized as follows:

    • hunyuanvideo_foley/models/: Model implementations (e.g., hifi_foley.py, dac_vae/, synchformer/).
    • hunyuanvideo_foley/utils/: Utilities for configuration, features, model loading, and media processing.
    • hunyuanvideo_foley/constants.py: Centralized project constants.
    hunyuanvideo_foley/
    ├── models/                 # Model implementations
    │   ├── hifi_foley.py      # Main model
    │   ├── nn/                # Neural network layers
    │   ├── dac_vae/           # Audio VAE
    │   └── synchformer/       # Synchronization model
    ├── utils/                 # Utilities
    │   ├── config_utils.py    # Configuration handling
    │   ├── feature_utils.py   # Feature extraction
    │   ├── model_utils.py     # Model loading/saving
    │   └── media_utils.py     # Audio/video processing
    └── constants.py           # Project constants
  4. Coding standards for HunyuanVideo-Foley

    main

    When contributing code, adhere to these standards:

    Error Handling

    • Use custom exceptions for domain-specific errors.
    • Validate inputs at function boundaries.
    • Log errors using appropriate levels (ERROR, WARNING, INFO).
    • Provide helpful error messages.

    Type Hints

    • Add type hints to all function parameters and return values.
    • Use Optional[Type] for nullable parameters.
    • Import types from the typing module.

    Documentation

    • Use Google-style docstrings for all public functions and classes.
    • Document parameters, return values, and exceptions.
  5. Install HunyuanVideo-Foley

    main

    Follow these steps to set up the environment for generating Foley audio from video.

    System Requirements

    • CUDA: 12.4 or 11.8 recommended
    • Python: 3.8+
    • OS: Linux (primary support)
    • VRAM Requirements:
      • XXL model: 20GB (or 12GB with --enable_offload)
      • XL model: 16GB (or 8GB with --enable_offload)

    Installation Steps

    1. Clone the repository:
      git clone https://github.com/Tencent-Hunyuan/HunyuanVideo-Foley
      cd HunyuanVideo-Foley
    2. Install dependencies:
      pip install -r requirements.txt
    3. Download Pretrained Models (from Huggingface):
      # Using git-lfs
      git clone https://huggingface.co/tencent/HunyuanVideo-Foley
      
      # OR using huggingface-cli
      huggingface-cli download tencent/HunyuanVideo-Foley
    git clone https://github.com/Tencent-Hunyuan/HunyuanVideo-Foley
    cd HunyuanVideo-Foley
    pip install -r requirements.txt
  6. Run tests and coverage

    main

    Tests should be placed in the tests/ directory and named using the test_*.py pattern. Use pytest to execute them.

    Commands:

    • Run all tests: python -m pytest
    • Run a specific test file: python -m pytest tests/test_feature_utils.py
    • Run with coverage: python -m pytest --cov=hunyuanvideo_foley
    # Run all tests
    python -m pytest
    
    # Run specific test file
    python -m pytest tests/test_feature_utils.py
    
    # Run with coverage
    python -m pytest --cov=hunyuanvideo_foley
  7. Verify HunyuanVideo-Foley installation

    main

    After installation, verify that the package is correctly installed and the CLI tool is accessible.

    # Check package installation and version
    python -c "import hunyuanvideo_foley; print(hunyuanvideo_foley.__version__)"
    
    # Check CLI tool help
    hunyuanvideo-foley --help
    python -c "import hunyuanvideo_foley; print(hunyuanvideo_foley.__version__)"
  8. Manually format and check code style

    main

    If you need to format code or run quality checks manually without pre-commit, use the following commands. Note that the project uses a 120 character line length.

    TaskCommand
    Format Python filesblack --line-length 120 .
    Sort importsisort --profile black --line-length 120 .
    Check style (Lint)flake8 --max-line-length 120
    Static type checkmypy --ignore-missing-imports .
    # Format all Python files
    black --line-length 120 .
    
    # Sort imports
    isort --profile black --line-length 120 .
    
    # Check code style
    flake8 --max-line-length 120
    
    # Type checking
    mypy --ignore-missing-imports .
  9. Set up the development environment

    main

    To begin developing on HunyuanVideo-Foley, create a virtual environment, activate it, and install the project in editable mode along with its dependencies.

    Steps:

    1. Create and activate a virtual environment.
    2. Install requirements via pip install -r requirements.txt.
    3. Install the package in editable mode using pip install -e ..
    python -m venv venv
    source venv/bin/activate  # Linux/Mac
    # or
    venv\Scripts\activate     # Windows
    
    pip install -r requirements.txt
    pip install -e .
  10. Launch the Gradio Web Interface

    main

    Run gradio_app.py to interact with the model through a browser-based UI. You can configure the model size and memory offloading via environment variables.

    Environment Variables

    • HIFI_FOLEY_MODEL_PATH: Path to the pretrained model weights.
    • MODEL_SIZE: Set to xl for the memory-friendly model.
    • ENABLE_OFFLOAD: Set to true to reduce VRAM usage.

    Examples

    Launch with XXL model (Default):

    export HIFI_FOLEY_MODEL_PATH=PRETRAINED_MODEL_PATH_DIR
    python3 gradio_app.py

    Launch with XL model:

    export HIFI_FOLEY_MODEL_PATH=PRETRAINED_MODEL_PATH_DIR
    MODEL_SIZE=xl python3 gradio_app.py

    Launch with memory offloading enabled:

    export HIFI_FOLEY_MODEL_PATH=PRETRAINED_MODEL_PATH_DIR
    ENABLE_OFFLOAD=true python3 gradio_app.py
    export HIFI_FOLEY_MODEL_PATH=PRETRAINED_MODEL_PATH_DIR
    MODEL_SIZE=xl ENABLE_OFFLOAD=true python3 gradio_app.py