Video and audio extraction processor, supports multi-stream separation
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 |
Array of structs includes:
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 |
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… │ # ╰────────────────────────────────┴──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯