FoleyCrafter

repository·main·Indexed 20 days ago

https://github.com/open-mmlab/foleycrafter

A video-to-audio generation framework that produces semantically relevant and synchronized sound effects for videos. It features a temporal adapter for audio-visual synchronization, support for positive and negative text prompts to guide generation, and a Gradio-based web interface. The framework includes the FoleyController.foley API for controlling parameters such as visual content scale, temporal align scale, and sampling methods.

Tokens
2.2K
Snippets
9
Records
11
Agent score
21%

What's inside FoleyCrafter

  1. Overview of FoleyCrafter

    main
    FoleyCrafter is a video-to-audio generation framework designed to produce realistic sound effects that are semantically relevant and synchronized with video content. It aims to enhance the audiovisual experience for cinema and gaming by bringing silent videos to life with lifelike sounds.
  2. Install FoleyCrafter via Conda

    main

    To set up the FoleyCrafter environment, use the provided environment.yaml file to create a new Conda environment and then activate it. You must also install and initialize git-lfs to ensure checkpoints can be downloaded correctly.

    # install conda environment
    conda env create -f requirements/environment.yaml
    conda activate foleycrafter
    
    # install GIT LFS for checkpoints download
    conda install git-lfs
    git lfs install
  3. Control Audio Generation with Prompts

    main

    You can guide the audio generation process using text prompts.

    • Positive Prompt: Use --prompt to specify desired sounds (e.g., 'seagulls').
    • Negative Prompt: Use --nprompt to specify sounds you want to exclude (e.g., 'river flows').
    • Seed: Use --seed to ensure reproducibility.
    # Example: Generating audio with a specific prompt
    python inference.py \
    --input=input/PromptControl/case1/ \
    --seed=10201304011203481429 \
    --prompt='noisy, people talking' \
    --save_dir=output/PromptControl/case1_prompt/
    
    # Example: Generating audio while excluding specific sounds
    python inference.py \
    --input=input/PromptControl/case3/ \
    --seed=10041042941301238011 \
    --nprompt='river flows' \
    --save_dir=output/PromptControl/case3_nprompt/
  4. Download FoleyCrafter Checkpoints

    main

    Checkpoints are automatically downloaded when running inference.py. However, you can download them manually using git clone.

    Manual Download Steps:

    1. Clone the Auffusion text-to-audio base model into checkpoints/auffusion.
    2. Clone the FoleyCrafter model into the checkpoints/ directory.

    Required Directory Structure: Ensure your checkpoints folder follows this structure:

    └── checkpoints
        ├── semantic
        │   ├── semantic_adapter.bin
        ├── vocoder
        │   ├── vocoder.pt
        │   ├── config.json
        ├── temporal_adapter.ckpt
        └── timestamp_detector.pth.tar
    # Download the text-to-audio base model (Auffusion)
    git clone https://huggingface.co/auffusion/auffusion-full-no-adapter checkpoints/auffusion
    
    # Download FoleyCrafter
    git clone https://huggingface.co/ymzhang319/FoleyCrafter checkpoints/
  5. Configure FoleyCrafter generation parameters

    main

    When using the FoleyCrafter interface, two key scales control the generation quality:

    1. Visual Content Scale (ip_adapter_scale): Determines the level of semantic alignment between the visual content of the video and the generated audio.
    2. Temporal Align Scale (temporal_scale): Determines how strongly the audio synchronizes with temporal visual cues. If the input video has strong temporal movements (e.g., a hammer hitting a nail), increasing this scale helps the audio match those moments.
  6. Run the FoleyCrafter Gradio Demo

    main

    FoleyCrafter provides a Gradio-based web interface for generating synchronized audio from silent videos. You can launch the server using the app.py entrypoint with several CLI arguments to configure the server and model paths.

    CLI Arguments:

    • --config: Path to the configuration file (default: example/config/base.yaml).
    • --server-name: The hostname to bind the server to (default: 0.0.0.0).
    • --port: The port number to listen on (default: 7860).
    • --share: Boolean flag to enable a public Gradio share link.
    • --save-path: Directory where generated samples are stored (default: samples).
    • --ckpt: Directory containing model checkpoints (default: checkpoints/).
    python app.py --config example/config/base.yaml --port 7860
  7. Perform Video-to-Audio Generation with Temporal Alignment

    main

    To ensure the generated audio aligns better with visual cues in the video, use the --temporal_align flag. This utilizes a temporal adapter to synchronize audio events with visual changes.

    python inference.py \
    --temporal_align \
    --input=input/avsync \
    --save_dir=output/avsync/
  8. Reference: inference.py CLI Arguments

    main

    The inference.py script accepts the following command-line arguments for controlling the audio generation process.

    options:
      -h, --help            show this help message and exit
      --prompt PROMPT       prompt for audio generation
      --nprompt NPROMPT     negative prompt for audio generation
      --seed SEED           ramdom seed
      --temporal_align TEMPORAL_ALIGN
                            use temporal adapter or not
      --temporal_scale TEMPORAL_SCALE
                            temporal align scale
      --semantic_scale SEMANTIC_SCALE
                            visual content scale
      --input INPUT         input video folder path
      --ckpt CKPT           checkpoints folder path
      --save_dir SAVE_DIR   generation result save path
      --pretrain PRETRAIN   generator checkpoint path
      --device DEVICE
  9. Use the FoleyController.foley API for video-to-audio generation

    main

    The FoleyController.foley method is the core API for generating synchronized audio. It takes a video and several control parameters to guide the generation process.

    Parameters:

    • input_video (str): Path to the input video file.
    • prompt_textbox (str): Text prompt describing the desired sound.
    • negative_prompt_textbox (str): Text prompt for sounds to avoid.
    • ip_adapter_scale (float): Visual Content Scale (0.0 to 1.0). Controls semantic alignment with visual content.
    • temporal_scale (float): Temporal Align Scale (0.0 to 1.0). Controls how well the audio aligns with temporal visual cues.
    • sampler_dropdown (str): The sampling method to use. Options: DDIM, Euler, PNDM.
    • sample_step_slider (int): Number of inference steps (10 to 100).
    • cfg_scale_slider (float): Classifier-Free Guidance (CFG) scale (7.5 to 20).
    • seed_textbox (str): Seed for reproducibility.

    Returns:

    • save_sample_path (str): Path to the generated .mp4 file containing the original video with the new synchronized audio.
    # Note: This is a conceptual usage of the controller method
    result_video_path = controller.foley(
        input_video="path/to/video.mp4",
        prompt_textbox="birds chirping",
        negative_prompt_textbox="",
        ip_adapter_scale=1.0,
        temporal_scale=0.2,
        sampler_dropdown="DDIM",
        sample_step_slider=25,
        cfg_scale_slider=7.5,
        seed_textbox="42"
    )