Video format conversion processor that converts various video formats to MP4
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 MP4 file paths |
An array containing the conversion result paths. If successful, returns the output paths; if failed, returns None
If a parameter does not have a default value, it is required
Parameter name | Type | Default value | Description |
|---|---|---|---|
video_codec | str | libx264 | Video encoder. Supports libx264, libx265, and more. Default value: "libx264" |
crf | int | 23 | Video quality control. Value range: 0–51; the smaller the value, the higher the quality. Default value: 23 |
preset | str | medium | Encoding speed preset. Supports ultrafast, superfast, veryfast, faster, fast, medium, slow, slower, veryslow. Default value: "medium" |
max_height | int or None | None | Maximum video height limit. Automatically scales if exceeded. No limit if set to None. Default value: None |
audio_codec | str | aac | Audio encoder. Supports aac and more. Default value: "aac" |
audio_bitrate | str | 192k | Audio bitrate, such as "192k", "128k". Default value: "192k" |
audio_sample_rate | int or None | None | Audio sample rate, such as 44100 or 48000. If set to None, the original sample rate is retained. Default value: None |
extra_params | list or None | None | Additional ffmpeg parameter list, such as ["-movflags", "+faststart"]. Default value: None |
timeout | int or None | None | Timeout for processing a single video (seconds). No limit if set to None. Default value: None |
The following code demonstrates how to use Daft (for distributed scenarios) to run the operator and convert videos to MP4 format.
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 VideoConvertToMp4 if __name__ == "__main__": # The converted video will be saved to the specified TOS path. Therefore, you need to set environment variables to ensure you have permission to write 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_to_mp4/sample.mp4"], "output_path": [f"tos://{TOS_TEST_DIR}/video_convert_to_mp4/sample_converted.mp4"], } ds = daft.from_pydict(samples) # Using Daft to convert video to MP4 format constructor_kwargs = { "video_codec": "libx264", "crf": 23, "preset": "medium", "max_height": 240, "audio_codec": "aac", "audio_bitrate": "192k", "select_audio": "auto", "extra_params": ["-movflags", "+faststart"], } ds = ds.with_column( "convert_result", las_udf( VideoConvertToMp4, 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… │ # ╰────────────────────────────────┴────────────────────────────────┴────────────────────────────────╯