General video format conversion processor
Input column name | Note |
|---|---|
input_col | An array containing input video paths (supports local paths, HTTP/HTTPS URLs, TOS/S3 URLs) |
output_col | An array containing output video file paths |
Returns an array of output paths on success, or None on failure.
If a parameter does not have a default value, it is required.
Parameter name | Type | Default value | Description |
|---|---|---|---|
output_format | str | None | Output video format. Supported values: "mp4", "avi", "mov", "mkv", "flv", "webm" |
timeout | int or None | None | ffmpeg execution timeout (seconds). Default is None (no timeout). |
extra_params | list or None | None | Additional ffmpeg parameter list, directly appended to the command. For example: - Video quality: ["-crf", "23"] - Video bitrate: ["-b:v", "2M"] - Encoding preset: ["-preset", "medium"] - Video scaling: ["-vf", "scale=-2:720"] - Audio bitrate: ["-b:a", "192k"] - Audio sample rate: ["-ar", "48000"] - Specific encoder: ["-c:v", "libx265", "-c:a", "aac"] |
The following code demonstrates how to use Daft (for distributed scenarios) to run the operator for general video format conversion. Supports conversion to multiple formats, including MP4, AVI, MOV, MKV, WEBM, and more.
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 VideoConvert if __name__ == "__main__": # The converted video will be saved to the specified TOS path. Therefore, you need to set environment variables to ensure write permissions to TOS, including: ACCESS_KEY, SECRET_KEY, TOS_ENDPOINT, TOS_REGION, TOS_TEST_DIR 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 = { "input_path": [f"https://{TOS_TEST_DIR_URL}/public/archive/video_convert/music_sample.mov"], "output_path": [f"tos://{TOS_TEST_DIR}/video_convert/music_sample.mp4"], } ds = daft.from_pydict(samples) # Using Daft to convert video format constructor_kwargs = { "output_format": "mp4", "extra_params": ["-crf", "23", "-preset", "medium"], } ds = ds.with_column( "convert_result", las_udf( VideoConvert, construct_args=constructor_kwargs, num_cpus=1, concurrency=1, batch_size=1, )(col("input_path"), col("output_path")), ) ds.show() # ╭────────────────────────────────┬────────────────────────────────┬────────────────────────────────╮ # │ input_path ┆ output_path ┆ convert_result │ # │ --- ┆ --- ┆ --- │ # │ String ┆ String ┆ String │ # ╞════════════════════════════════╪════════════════════════════════╪════════════════════════════════╡ # │ https://las-public-data-qa.to… ┆ tos://tos_bucket/video_conver… ┆ tos://tos_bucket/video_conver… │ # ╰────────────────────────────────┴────────────────────────────────┴────────────────────────────────╯