视频裁剪
更新时间:2026-06-15
简介
视频裁剪
功能描述
- 支持多种裁剪模式:指定分辨率、宽高比、中心正方形、横图、竖图
- 支持基于边界框(bbox)的精确裁剪
- 可控制视频质量和编码参数
- 保持音频流不受影响
- 支持路径输入、二进制输入和BOS输出
- MP4 (.mp4)
- AVI (.avi)
- MOV (.mov)
- MKV (.mkv)
- 其他常见视频格式
- CPU编码 (libx264)**:质量优先,压缩效率高,速度较慢
- GPU编码 (h264_nvenc)**:速度快,适合批量处理,质量略低于libx264(相同码率下)
- 边界框(bbox) > 目标分辨率(target_width/target_height) > 宽高比(aspect_ratio)
算子参数
输入
| 输入 | 含义 |
|---|---|
| video_paths | 视频文件路径列(本地、BOS、HTTP等),与video_binaries二选一 |
| video_binaries | 视频二进制数据列,与video_paths二选一 |
| video_formats | 视频格式字符串列,配合video_binaries使用 |
| output_basenames | 输出文件基础名称列(不含扩展名) |
输出
| 输出 | 含义 |
|---|---|
| result | 裁剪后的视频路径列 |
参数
| 参数名称 | 类型 | 默认值 | 描述 |
|---|---|---|---|
| output_tos_dir | str | '' | 将裁剪后的视频保存到该BOS目录中,如果为空则不保存。 格式:"bos://bucket/path/" 默认值:"" |
| target_width | int | None | 目标视频宽度(像素)。 与target_height配合使用,指定具体分辨率 优先级:bbox > target_width/target_height > aspect_ratio 默认值:None |
| target_height | int | None | 目标视频高度(像素)。 与target_width配合使用,指定具体分辨率 优先级:bbox > target_width/target_height > aspect_ratio 默认值:None |
| aspect_ratio | float | None | 目标宽高比。 格式:宽度/高度(如16/9=1.7778) 优先级:bbox > target_width/target_height > aspect_ratio 默认值:None |
| bbox | tuple[int, int, int, int] | None | 边界框参数,格式为(x, y, width, height)。 x, y: 裁剪区域左上角坐标 width, height: 裁剪区域的宽度和高度 优先级高于target_width/target_height和aspect_ratio 注意:bbox参数会覆盖其他裁剪参数 默认值:None |
| crop_mode | str | 'center' | 裁剪模式。 center: 中心裁剪 top: 顶部裁剪 bottom: 底部裁剪 left: 左侧裁剪 right: 右侧裁剪 可选值:["center", "top", "bottom", "left", "right"] 仅在不使用bbox参数时生效 默认值:"center" |
| force_divisible_by | int | 2 | 像素对齐步长,确保宽高能被该值整除。 默认值:2 |
| crf | float | 23.0 | libx264编码器的恒定质量因子。 适用:仅在CPU编码(libx264)时使用 范围:0.0-51.0,数值越小质量越高文件越大 推荐:18(高质量) 23(平衡) 28(压缩优先) 默认值:23.0 |
| preset | str | 'medium' | libx264编码器的编码速度预设。 适用:仅在CPU编码(libx264)时使用 权衡:速度 ↔ 压缩效率 可选值:["ultrafast", "superfast", "veryfast", "faster", "fast", "medium", "slow", "slower", "veryslow"] 推荐:"medium"(平衡) "fast"(速度优先) "slow"(质量优先) 默认值:"medium" |
| cq | float | 0 | NVENC编码器的质量控制参数。 适用:仅在GPU编码(h264_nvenc)时使用 范围:0-51,0表示自动质量控制 推荐:0(自动) 或 18-28(手动控制) 默认值:0 |
| rc | str | 'vbr' | NVENC编码器的码率控制模式。 适用:仅在GPU编码(h264_nvenc)时使用 constqp: 恒定量化参数,质量稳定 vbr: 变码率,平衡质量和文件大小 cbr: 恒定码率,适合流媒体传输 推荐:"vbr"(通用) "cbr"(直播) 默认值:"vbr" |
| rank | int | None | 指定使用的GPU设备编号(多卡环境有效)。 说明:0表示第一张GPU,1表示第二张GPU,None表示自动选择 适用:仅在GPU编码时有效 默认值:None |
调用示例
Python
1from __future__ import annotations
2
3import os
4
5import daft
6from daft import col
7
8from daft.aihc.common.udf import aihc_udf
9from daft.aihc.functions.video.video_crop import VideoCrop
10
11if __name__ == "__main__":
12 if os.getenv("DAFT_RUNNER", "native") == "ray":
13 import ray
14 ray.init(dashboard_host="0.0.0.0", ignore_reinit_error=True)
15 daft.set_runner_ray()
16 daft.set_execution_config(actor_udf_ready_timeout=6000, min_cpu_per_task=0)
17
18 # TODO: 根据实际场景准备样本数据
19 samples = {"video_paths": [...], "video_binaries": [...]}
20 ds = daft.from_pydict(samples)
21 constructor_kwargs = {
22 "output_tos_dir": '',
23 "target_width": None,
24 "target_height": None,
25 "aspect_ratio": None,
26 "bbox": None,
27 }
28 ds = ds.with_column(
29 "result",
30 aihc_udf(
31 VideoCrop,
32 construct_args=constructor_kwargs,
33 num_cpus=1,
34 concurrency=4,
35 batch_size=8,
36 )(col("video_paths"), col("video_binaries"), col("video_formats")),
37 )
38 ds.show()
评价此篇文章
