HunyuanVideo-Foley
repository·main·Indexed 21 days ago
https://github.com/tencent-hunyuan/hunyuanvideo-foleyA 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.
What's inside HunyuanVideo-Foley
- 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.
Technical Architecture of HunyuanVideo-Foley
mainHunyuanVideo-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.
Project structure overview
mainThe 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 constantsCoding standards for HunyuanVideo-Foley
mainWhen 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
typingmodule.
Documentation
- Use Google-style docstrings for all public functions and classes.
- Document parameters, return values, and exceptions.
Install HunyuanVideo-Foley
mainFollow 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)
- XXL model: 20GB (or 12GB with
Installation Steps
- Clone the repository:
git clone https://github.com/Tencent-Hunyuan/HunyuanVideo-Foley cd HunyuanVideo-Foley - Install dependencies:
pip install -r requirements.txt - 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.txtRun tests and coverage
mainTests should be placed in the
tests/directory and named using thetest_*.pypattern. Usepytestto 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- Run all tests:
Verify HunyuanVideo-Foley installation
mainAfter 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 --helppython -c "import hunyuanvideo_foley; print(hunyuanvideo_foley.__version__)"ComfyUI Integration for HunyuanVideo-Foley
mainThe community has developed several ComfyUI nodes to integrate HunyuanVideo-Foley into existing workflows:
- if-ai/ComfyUI_HunyuanVideoFoley: Supports CPU offloading and FP8 quantization.
- phazei/ComfyUI-HunyuanVideo-Foley: An alternative implementation supporting different precision modes.
Manually format and check code style
mainIf 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.
Task Command Format Python files black --line-length 120 .Sort imports isort --profile black --line-length 120 .Check style (Lint) flake8 --max-line-length 120Static type check mypy --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 .Install and use pre-commit hooks
mainThe project uses
pre-committo automatically format code before commits. This ensures compliance with the project's style guidelines (Black, isort, flake8, mypy).To install the hooks, run:
pip install pre-commit pre-commit installSet up the development environment
mainTo begin developing on HunyuanVideo-Foley, create a virtual environment, activate it, and install the project in editable mode along with its dependencies.
Steps:
- Create and activate a virtual environment.
- Install requirements via
pip install -r requirements.txt. - 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 .Launch the Gradio Web Interface
mainRun
gradio_app.pyto 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 toxlfor the memory-friendly model.ENABLE_OFFLOAD: Set totrueto reduce VRAM usage.
Examples
Launch with XXL model (Default):
export HIFI_FOLEY_MODEL_PATH=PRETRAINED_MODEL_PATH_DIR python3 gradio_app.pyLaunch with XL model:
export HIFI_FOLEY_MODEL_PATH=PRETRAINED_MODEL_PATH_DIR MODEL_SIZE=xl python3 gradio_app.pyLaunch with memory offloading enabled:
export HIFI_FOLEY_MODEL_PATH=PRETRAINED_MODEL_PATH_DIR ENABLE_OFFLOAD=true python3 gradio_app.pyexport HIFI_FOLEY_MODEL_PATH=PRETRAINED_MODEL_PATH_DIR MODEL_SIZE=xl ENABLE_OFFLOAD=true python3 gradio_app.py