Video resolution adjustment
Input column names | Description |
|---|---|
video_paths | Column for video file paths (local, TOS, HTTP, and so on); use either video_paths or video_binaries |
video_binaries | Column for video binary data; use either video_binaries or video_paths |
video_formats | Column for video format strings, used with video_binaries |
output_basenames | Column for output file base names (without extension) |
Column for adjusted video paths
If a parameter does not have a default value, it is required
Parameter name | Type | Default value | Description |
|---|---|---|---|
output_tos_dir | str | Save the video with adjusted resolution to this TOS directory. If empty, do not save. Format: "tos://bucket/path/" Default value: "" | |
min_width | int | 1280 | Minimum video width. If less than this value, it will be adjusted. Unit: pixels Default value: 1280 |
max_width | int | 2560 | Maximum video width. If greater than this value, it will be adjusted. Unit: pixels Default value: 2560 |
min_height | int | 1280 | Minimum video height. If less than this value, it will be adjusted. Unit: pixels Default value: 1280 |
max_height | int | 2560 | Maximum video height. If greater than this value, it will be adjusted. Unit: pixels Default value: 2560 |
force_original_aspect_ratio_type | str | disable | Aspect ratio preservation strategy. disable: Do not force preservation of the original aspect ratio, which may cause stretching or distortion increase: Preserve aspect ratio, adjust to be greater than or equal to the target size decrease: Preserve aspect ratio, adjust to be less than or equal to the target size Optional values: ["disable", "increase", "decrease"] Default value: "disable" |
force_divisible_by | int | 2 | Pixel alignment step size, ensures width and height are divisible by this value. Default value: 2 |
crf | float | 23.0 | Constant quality factor for the libx264 encoder. Applicable: Used only for CPU encoding (libx264). Range: 0.0–51.0; the lower the value, the higher the quality and the larger the file size. Recommended: 18 (high quality), 23 (balanced), 28 (compression prioritized). Default: 23.0 |
preset | str | medium | Encoding speed preset for the libx264 encoder. Applicable: Used only for CPU encoding (libx264). Trade-off: speed ↔ compression efficiency. Options: ["ultrafast", "superfast", "veryfast", "faster", "fast", "medium", "slow", "slower", "veryslow"]. Recommended: "medium" (balanced), "fast" (speed prioritized), "slow" (quality prioritized). Default: "medium" |
cq | float | 0 | Quality control parameter for the NVENC encoder. Applicable: Used only for GPU encoding (h264_nvenc). Range: 0–51; 0 indicates automatic quality control. Recommended: 0 (automatic) or 18–28 (manual control). Default: 0 |
rc | str | vbr | Bitrate control mode for the NVENC encoder. Applicable: Used only for GPU encoding (h264_nvenc). constqp: constant quantization parameter, stable quality. vbr: variable bitrate, balances quality and file size. cbr: constant bitrate, suitable for streaming. Recommended: "vbr" (general use), "cbr" (live streaming). Default: "vbr" |
rank | int or None | Specifies the GPU device number to use (effective in multi-card environments). Note: 0 indicates the first GPU, 1 indicates the second GPU, None indicates automatic selection. Applicable: Only effective for GPU encoding. Default: None |
The following code demonstrates how to use Daft (for distributed scenarios) to run the operator for intelligent video resolution adjustment.
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 VideoResizeResolution if __name__ == "__main__": # Videos with modified resolution 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_DIR = os.getenv("TOS_TEST_DIR", "tos_bucket") output_tos_dir = f"tos://{TOS_DIR}/video/video_resize_resolution" 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.%s".format(), ) 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) tos_dir_url = os.getenv("TOS_DIR_URL", "las-cn-beijing-public-online.tos-cn-beijing.volces.com") samples = { "video_path": [ f"https://{tos_dir_url}/public/shared_video_dataset/eating_56.mp4" ] } ds = daft.from_pydict(samples) constructor_kwargs = { "output_tos_dir": output_tos_dir, "min_width": 1280, "max_width": 2560, "min_height": 720, "max_height": 1440, "force_original_aspect_ratio_type": "decrease", "rank": None, } ds = ds.with_column( "resized_video_path", las_udf(VideoResizeResolution, construct_args=constructor_kwargs, num_gpus=1, batch_size=1, concurrency=1)( col("video_path") ), ) ds.show() # ╭────────────────────────────────┬──────────────────────────────────╮ # │ video_path ┆ resized_video_path │ # │ --- ┆ --- │ # │ Utf8 ┆ Utf8 │ # ╞════════════════════════════════╪══════════════════════════════════╡ # │ https://las-cn-beijing-publi-… ┆ tos://tos_bucket/video_resize… │ # ╰────────────────────────────────┴──────────────────────────────────╯