You need to enable JavaScript to run this app.
Lake AI Service

Lake AI Service

Copy page
Download PDF
Video processing
Video and audio extraction
Copy page
Download PDF
Video and audio extraction

Operator introduction

Description

Video and audio extraction processor, supports multi-stream separation

Key features

  • Supports extracting audio streams from videos via multiple paths or binary input, including local, TOS, HTTP, and more
  • Supports selecting multiple audio streams, extracting only the first stream, or all streams
  • Supports outputting audio to TOS, returning binary data, sample rate, and more
  • Supports extracting audio within a specified time interval (start_second, end_second)
  • All output audio is in mp3 format

Daft invocation

Operator parameters

Input

Input column name

Note

video_paths

Array of video file paths (local, TOS, HTTP, and more)

video_binaries

Array of video binary data (optional)

video_formats

Array of video format strings (optional)

output_basenames

Optional, array of output subdirectory or file names

Output

Array of structs includes:

  • audio_paths: List of audio TOS paths
  • audio_arrays: List of audio array data (optional)
  • binaries: List of audio binary data (optional)
  • original_audio_sampling_rates: Always returned, contains the original sampling rate of each audio stream, corresponds one-to-one to audio_paths and binaries

Parameters

If a parameter does not have a default value, it is required

Parameter name

Type

Default value

Description

output_tos_dir

str

Save the extracted audio from the video to this TOS directory. If empty, audio will not be saved

output_audio_binary

bool

False

Whether to return the binary content of the audio. Default is False

output_audio_array

bool

False

Whether to return the audio in NumPy array format. Default is False

stream_indexes

list or None

None

Specify which audio streams to extract. Default is all Indices out of range will be automatically ignored

return_first_stream

bool

True

If True, only the first audio stream is returned; otherwise, a list of all streams is returned. Default is True

start_second

float or None

None

Start extracting audio from the specified second in the video. None means start from the beginning. Unit: seconds

end_second

float or None

None

End extracting audio at the specified second in the video. None means until the end. Unit: seconds

output_format

str

mp3

Output audio format. Default is "mp3"

output_sample_rate

int

48000

Output audio sample rate. Default is 48000

Examples

The following code demonstrates how to use Daft (for distributed scenarios) to run the operator for audio extraction from video.

from __future__ import annotations

import os

import daft
from daft import col
from daft.las.functions.udf import las_udf
from daft.las.functions.video import VideoExtractAudio

if __name__ == "__main__":
    # After extracting the audio, it will be saved to the specified TOS path. Therefore, you need to set the environment variables to ensure you have permission to access and write to TOS, including: ACCESS_KEY, SECRET_KEY, TOS_ENDPOINT, TOS_REGION, TOS_TEST_DIR_URL
    TOS_TEST_DIR_URL = os.getenv("TOS_TEST_DIR_URL", "las-cn-beijing-public-online.tos-cn-beijing.volces.com")
    TOS_TEST_DIR = os.getenv("TOS_TEST_DIR", "tos_bucket")

    if os.getenv("DAFT_RUNNER", "native") == "ray":
        import logging

        import ray

        def configure_logging():
            logging.basicConfig(
                level=logging.INFO,
                format="%(asctime)s - %(name)s - %(levelname)s - %(message)s",
                datefmt="%Y-%m-%d %H:%M:%S",
            )
            logging.getLogger("tracing.span").setLevel(logging.WARNING)
            logging.getLogger("daft_io.stats").setLevel(logging.WARNING)
            logging.getLogger("DaftStatisticsManager").setLevel(logging.WARNING)
            logging.getLogger("DaftFlotillaScheduler").setLevel(logging.WARNING)
            logging.getLogger("DaftFlotillaDispatcher").setLevel(logging.WARNING)

        ray.init(dashboard_host="0.0.0.0", runtime_env={"worker_process_setup_hook": configure_logging})
        daft.set_runner_ray()

    daft.set_execution_config(actor_udf_ready_timeout=600)
    daft.set_execution_config(min_cpu_per_task=0)

    samples = {
        "video_path": [f"https://{TOS_TEST_DIR_URL}/video_extract_audio/sample.mp4"],
    }
    ds = daft.from_pydict(samples)

    # Using Daft to extract audio from video
    output_tos_dir = f"tos://{TOS_TEST_DIR}/video_extract_audio"
    constructor_kwargs = {
        "output_tos_dir": output_tos_dir,
        "output_audio_binary": True,
        "output_sampling_rate": 16000,
        "output_audio_format": "mp3",
    }

    ds = ds.with_column(
        "extract_results",
        las_udf(
            VideoExtractAudio,
            construct_args=constructor_kwargs,
            num_cpus=1,
            concurrency=1,
            batch_size=1,
        )(col("video_path")),
    )

    ds.show()
    # ╭────────────────────────────────┬──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╮
    # │ video_path                     ┆ extract_results                                                                                                                          │
    # │ ---                            ┆ ---                                                                                                                                      │
    # │ Utf8                           ┆ Struct[audio_paths: List[Utf8], audio_arrays: List[List[Float32]], binaries: List[Binary], original_audio_sampling_rates: List[Float64]] │
    # ╞════════════════════════════════╪══════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════╡
    # │ tos://tos_bucket/video_extrac… ┆ {audio_paths: [tos://las-ai-c…                                                                                                           │
    # ╰────────────────────────────────┴──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯
Last updated: 2026.05.12 19:06:35