InfiniteTalk Documentation

repository·main·Indexed 27 days ago

https://github.com/meigen-ai/infinitetalk

An audio-driven video generation framework for creating unlimited-length talking videos. It supports video-to-video dubbing and image-to-video generation with accurate lip synchronization, identity preservation, and stable body movements. The framework includes support for single and multi-GPU inference, LoRA integration (e.g., FusionX, Lightx2v), and a Gradio UI.

Tokens
4.7K
Snippets
8
Records
19
Agent score
93%

What's inside InfiniteTalk

  1. Overview of InfiniteTalk

    main

    InfiniteTalk is an audio-driven video generation model designed for sparse-frame video dubbing. It supports two primary modes of operation:

    1. Video-to-Video: Given an input video and an audio track, it synthesizes a new video with accurate lip synchronization while aligning head movements, body posture, and facial expressions with the audio.
    2. Image-to-Video: Uses an image and an audio track as input to generate a talking video.

    Key capabilities include infinite-length video generation, consistent identity preservation, and improved stability in body/hand movements compared to previous methods like MultiTalk.

  2. Install InfiniteTalk dependencies

    main

    Follow these steps to set up the environment using Conda and Pip:

    1. Create Conda environment and install PyTorch/xformers:
    conda create -n multitalk python=3.10
    conda activate multitalk
    pip install torch==2.4.1 torchvision==0.19.1 torchaudio==2.4.1 --index-url https://download.pytorch.org/whl/cu121
    pip install -U xformers==0.0.28 --index-url https://download.pytorch.org/whl/cu121
    1. Install Flash-attn and related packages:
    pip install misaki[en]
    pip install ninja 
    pip install psutil 
    pip install packaging
    pip install wheel
    pip install flash_attn==2.7.4.post1
    1. Install other dependencies:
    pip install -r requirements.txt
    conda install -c conda-forge librosa
    1. Install FFmpeg: Using Conda:
    conda install -c conda-forge ffmpeg

    Or using yum:

    sudo yum install ffmpeg ffmpeg-devel
    conda create -n multitalk python=3.10
    conda activate multitalk
    pip install torch==2.4.1 torchvision==0.19.1 torchaudio==2.4.1 --index-url https://download.pytorch.org/whl/cu121
    pip install -U xformers==0.0.28 --index-url https://download.pytorch.org/whl/cu121
    pip install misaki[en]
    pip install ninja 
    pip install psutil 
    pip install packaging
    pip install wheel
    pip install flash_attn==2.7.4.post1
    pip install -r requirements.txt
    conda install -c conda-forge librosa
    conda install -c conda-forge ffmpeg
  3. Download InfiniteTalk models

    main

    Download the required weights using huggingface-cli. You need the base model, the audio encoder, and the InfiniteTalk weights.

    Required models:

    • Wan-AI/Wan2.1-I2V-14B-480P (Base model)
    • TencentGameMate/chinese-wav2vec2-base (Audio encoder)
    • MeiGen-AI/InfiniteTalk (Audio condition weights)
    huggingface-cli download Wan-AI/Wan2.1-I2V-14B-480P --local-dir ./weights/Wan2.1-I2V-14B-480P
    huggingface-cli download TencentGameMate/chinese-wav2vec2-base --local-dir ./weights/chinese-wav2vec2-base
    huggingface-cli download TencentGameMate/chinese-wav2vec2-base model.safetensors --revision refs/pr/1 --local-dir ./weights/chinese-wav2vec2-base
    huggingface-cli download MeiGen-AI/InfiniteTalk --local-dir ./weights/InfiniteTalk
  4. Run InfiniteTalk via CLI

    main

    You can run the InfiniteTalk generation pipeline from the command line using app.py. The script supports both single-image driven tasks and video dubbing. It requires a checkpoint directory and a task specification.

    Key CLI Arguments:

    • --task: The task to run (e.g., infinitetalk-14B).
    • --size: The bucket size of the generated video (e.g., infinitetalk-480, infinitetalk-720).
    • --ckpt_dir: Path to the Wan checkpoint directory.
    • --infinitetalk_dir: Path to the InfiniteTalk checkpoint directory.
    • --wav2vec_dir: Path to the wav2vec checkpoint directory.
    • --mode: Generation mode: clip (single chunk) or streaming (long video generation).
    • --sample_steps: Number of sampling steps (defaults to 40 for I2V and 50 for T2V).
    • --offload_model: Whether to offload the model to CPU to save VRAM.
    • --use_teacache: Enable TeaCache for faster video generation.
    • --use_apg: Enable Adaptive Projected Guidance (APG).

    Example Command:

    python app.py --task infinitetalk-14B --size infinitetalk-480 --ckpt_dir ./weights/Wan2.1-I2V-14B-480P
  5. Run InfiniteTalk inference (Single GPU)

    main

    To run inference on a single GPU for 480P streaming video, use the following command. Ensure you have downloaded the weights to the specified directories.

    python generate_infinitetalk.py \
        --ckpt_dir weights/Wan2.1-I2V-14B-480P \
        --wav2vec_dir 'weights/chinese-wav2vec2-base' \
        --infinitetalk_dir weights/InfiniteTalk/single/infinitetalk.safetensors \
        --input_json examples/single_example_image.json \
        --size infinitetalk-480 \
        --sample_steps 40 \
        --mode streaming \
        --motion_frame 9 \
        --save_file infinitetalk_res
  6. Run InfiniteTalk with Gradio UI

    main

    Launch a Gradio web interface for interactive inference. You can use either single-person or multi-person weights.

    python app.py \
        --ckpt_dir weights/Wan2.1-I2V-14B-480P \
        --wav2vec_dir 'weights/chinese-wav2vec2-base' \
        --infinitetalk_dir weights/InfiniteTalk/single/infinitetalk.safetensors \
        --num_persistent_param_in_dit 0 \
        --motion_frame 9
  7. Run InfiniteTalk inference (Multi-GPU)

    main

    For multi-GPU inference, use torchrun with FSDP and Ulysses parallelism. Set GPU_NUM to your available GPU count.

    GPU_NUM=8
    torchrun --nproc_per_node=$GPU_NUM --standalone generate_infinitetalk.py \
        --ckpt_dir weights/Wan2.1-I2V-14B-480P \
        --wav2vec_dir 'weights/chinese-wav2vec2-base' \
        --infinitetalk_dir weights/InfiniteTalk/single/infinitetalk.safetensors \
        --dit_fsdp --t5_fsdp \
        --ulysses_size=$GPU_NUM \
        --input_json examples/single_example_image.json \
        --size infinitetalk-480 \
        --sample_steps 40 \
        --mode streaming \
        --motion_frame 9 \
        --save_file infinitetalk_res_multigpu
  8. Run InfiniteTalk with LoRA (FusionX/Lightx2v)

    main

    To use LoRA (like FusionX which requires 8 steps or lightx2v which requires 4), provide the --lora_dir and adjust the guide scales. Note that when using LoRA, the recommended --sample_text_guide_scale is 1.0 and --sample_audio_guide_scale is 2.0.

    python generate_infinitetalk.py \
        --ckpt_dir weights/Wan2.1-I2V-14B-480P \
        --wav2vec_dir 'weights/chinese-wav2vec2-base' \
        --infinitetalk_dir weights/InfiniteTalk/single/infinitetalk.safetensors \
        --lora_dir weights/Wan2.1_I2V_14B_FusionX_LoRA.safetensors \
        --input_json examples/single_example_image.json \
        --lora_scale 1.0 \
        --size infinitetalk-480 \
        --sample_text_guide_scale 1.0 \
        --sample_audio_guide_scale 2.0 \
        --sample_steps 8 \
        --mode streaming \
        --motion_frame 9 \
        --sample_shift 2 \
        --num_persistent_param_in_dit 0 \
        --save_file infinitetalk_res_lora
  9. Inference CLI arguments reference

    main

    The generate_infinitetalk.py script supports the following arguments:

    ArgumentDescription
    --mode streamingLong video generation
    --mode clipGenerate short video with one chunk
    --use_teacacheRun with TeaCache acceleration
    --size infinitetalk-480Generate 480P video
    --size infinitetalk-720Generate 720P video
    --use_apgRun with APG
    --teacache_threshCoefficient used for TeaCache acceleration
    --sample_text_guide_scaleText guide scale. Optimal: 5 (without LoRA), 1 (with LoRA)
    --sample_audio_guide_scaleAudio guide scale. Optimal: 4 (without LoRA), 2 (with LoRA)
    --max_frame_numMax frame length (default: 40s / 1000 frames)
    --num_persistent_param_in_ditSet to 0 to run with very low VRAM
    --quant fp8Use FP8 quantization to reduce memory usage
    --lora_dirPath to LoRA weights (e.g., FusionX)
    --dit_fsdp / --t5_fsdpEnable FSDP for multi-GPU inference
    --ulysses_sizeSet for multi-GPU inference (e.g., --ulysses_size=8)
  10. Text-to-Speech (TTS) Processing Modes

    main

    InfiniteTalk supports two TTS modes via the Kokoro pipeline:

    1. Single Speaker TTS (process_tts_single): Generates audio from a single text prompt using a specified voice tensor. It saves the result as s1.wav in the provided directory.
    2. Multi-Speaker TTS (process_tts_multi): Parses text containing speaker tags in the format (s1) text (s2) text. It generates separate audio tracks for each speaker and a combined sum.wav track. This allows for complex dialogue generation within a single prompt.
  11. Enable VRAM management for a PyTorch model

    main

    Use enable_vram_management to wrap specific layers in the model with VRAM-aware wrappers. This allows for offloading weights to different devices or dtypes to manage memory usage.

    To use this, you must provide a module_map that maps existing module types (e.g., torch.nn.Linear) to their corresponding wrapper classes (e.g., AutoWrappedLinear).

    Arguments:

    • model: The torch.nn.Module to modify.
    • module_map: A dictionary mapping source module types to wrapper classes.
    • module_config: A dictionary of keyword arguments passed to the wrapper's __init__.
    • max_num_param (optional): A threshold for parameter count. If the cumulative parameter count exceeds this, overflow_module_config is used instead of module_config.
    • overflow_module_config (optional): Configuration used when the parameter threshold is exceeded.
  12. Use Wav2Vec2Model for audio feature extraction

    main

    The Wav2Vec2Model class extends the Hugging Face Wav2Vec2Model to provide specialized audio feature extraction capabilities. It includes methods for extracting features with specific sequence lengths and encoding those features through a transformer encoder.

    Key Methods

    • feature_extract(input_values, seq_len): Processes raw input_values through a feature extractor and applies linear interpolation to match the target seq_len.
    • encode(extract_features, attention_mask=None, mask_time_indices=None, output_attentions=None, output_hidden_states=None, return_dict=None): Takes extracted features and passes them through the feature projection and encoder layers. If an adapter is present, it is applied to the resulting hidden states.
    • forward(input_values, seq_len, attention_mask=None, ...): A standard PyTorch forward pass that combines feature extraction and encoding in a single step.