视频按场景切分
更新时间:2026-07-29
简介
视频按场景切分算子,用 scenedetect 检测场景切换并把视频切分为多个片段。
功能描述
• 支持 ContentDetector / AdaptiveDetector / ThresholdDetector 三种场景检测器,可通过 **kwargs 透传各检测器专属参数。
• 用 scenedetect 检测场景列表后调用 ffmpeg 切分;无场景切换时复制原视频到输出目录。
• 输出每个视频切出的片段路径列表(本地路径),可选回传到 BOS。
• 内置提交 / 成功 / 失败计数器;单行失败时按 fail_on_error 决定抛异常或返回空列表。
算子参数
输入
| 输入 | 含义 |
|---|---|
| video | 视频(bos/http url、base64 或 binary,由 video_src_type 决定),单个输入列 |
输出
| 输出 | 含义 |
|---|---|
| result | pa.list_(pa.large_string()),每个视频切出的片段路径列表(本地或 BOS) |
参数
| 参数名称 | 类型 | 默认值 | 描述 |
|---|---|---|---|
| video_src_type | str | "video_url" | 视频输入类型,可选 "video_url"/"video_base64"/"video_binary" |
| detector | str | "ContentDetector" | 场景检测器,可选 "ContentDetector"/"AdaptiveDetector"/"ThresholdDetector" |
| threshold | float | 27.0 | 场景检测阈值 |
| min_scene_len | int | 15 | 最小场景长度(帧) |
| show_progress | bool | False | 是否显示进度条 |
| output_dir | str | "/tmp/aihc_scene_split" | 切分片段的本地输出目录 |
| output_bosdir | str | "" | 非空时片段回传到该 BOS 目录并返回 BOS 路径 |
| ffmpeg_extra_args | str | "-movflags frag_keyframe+empty_moov" | 传给 ffmpeg 的额外参数 |
| fail_on_error | bool | False | 单行出错是否立即抛异常,默认 False 返回空列表 |
调用示例
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_split_by_scene import VideoSplitByScene
10
11VIDEO = "bos://your-bucket/sample.mp4" # MOCK
12OUTPUT_DIR = "/tmp/aihc_scene_split" # MOCK
13
14os.environ.setdefault("BOS_ENDPOINT", "http://bj.bcebos.com")
15os.environ.setdefault("BOS_REGION", "bj")
16
17if __name__ == "__main__":
18 if os.getenv("DAFT_RUNNER", "native") == "ray":
19 import ray
20
21 ray.init(ignore_reinit_error=True)
22 daft.set_runner_ray()
23 daft.set_execution_config(actor_udf_ready_timeout=6000, min_cpu_per_task=0)
24
25 samples = {"video": [VIDEO]}
26 ds = daft.from_pydict(samples)
27 ds = ds.with_column(
28 "result",
29 aihc_udf(
30 VideoSplitByScene,
31 construct_args={
32 "video_src_type": "video_url",
33 "detector": "ContentDetector",
34 "threshold": 27.0,
35 "min_scene_len": 15,
36 "output_dir": OUTPUT_DIR,
37 },
38 num_cpus=1,
39 concurrency=1,
40 batch_size=1,
41 )(col("video")),
42 )
43 ds.show()
评价此篇文章
