跳转到内容
EN

动效、转场与变速

以下完整脚本可直接放入 execute 的 script。每段创建自己的轨道;文字、颜色与形状不需要素材。

关键帧与逐字出现

animate 可设置 x、y、scale、rotation、opacity、blur。关键帧 at 相对片段起点,单位秒;对齐到帧后时间必须不同且有序。动画覆盖静态变换;x、y 是归一化画布坐标,0.5 为中心。缓动属于目标关键帧,可用 linear、in、out、inOut、bezier 或 spring。

const track = AddTrack("video", { name: "Text reveal" });
Insert(track, {
type: "text", text: "Hello Cut", start: 0, length: 4,
reveal: { by: "char", delay: 0.2, stagger: 0.08, duration: 0.4,
from: { y: 0.5, opacity: 0 }, ease: "out" }
});

by 也接受 word 或 line;每个单位从 delay + index * stagger 开始,用 duration 秒完成。from.y 相对字体尺寸,不是画布高度。

const track = AddTrack("video", { name: "Flying title" });
Insert(track, {
type: "text", text: "A new perspective", start: 0, length: 4,
style: { font: { size: 72, weight: 700 }, align: { vertical: "bottom" }, margin: 0.1 },
animate: {
x: [{ at: 0, value: 0.1 }, { at: 1, value: 0.5,
ease: { spring: { stiffness: 100, damping: 14, mass: 1 } } }],
opacity: [{ at: 0, value: 0 }, { at: 0.5, value: 1, ease: "out" }]
}
});

动画预设

先调用 ListAnimations() 发现目录。每项返回 id、version、phase(in/out/loop)、target(any/text)、name、description、defaultDuration(秒)和 params(参数名、类型、默认值/范围/枚举值及描述,按适用字段返回)。每个槽使用 { preset, duration?, ...params };例如 duration 单位为秒。

Insert 可传 animation: { in?, out?, loop? },Update 可设置同样的对象;传 animation: null 清除预设及其生成的 animate/reveal。三个槽各至多一个预设。预设应用时会替换片段原有的 animate 与 reveal。片段长度或 transform 改变时,预设会自动重新展开:变短时入场、出场时长按比例压缩,变长后恢复请求的时长。Split 后左段保留 in,右段保留 out,loop 两段都保留。

in 与 out 规定入场、出场;loop 只在入场和出场之间运行。同一属性由多个槽动画时,loop 只占据中段。text target 仅用于文字入场;它的 duration 控制每个字符/词/行的出现节奏,不是全文总时长。出场和循环作用于整段,不支持逐字/逐词/逐行。

直接写 animate 或 reveal 会断开预设;同一次 Insert/Update 同时传 animation 和任一字段会报错。每个属性最多 600 个关键帧;循环周期至少 4 帧。错误码:UNKNOWN_ANIMATION(未知预设)、INVALID_ANIMATION_PARAM(槽位/参数无效或关键帧限制)、ANIMATION_TOO_LONG(入场与出场总时长超过片段长度)、ANIMATION_TARGET_MISMATCH(目标不符或非视觉片段)、ANIMATION_PHASE_MISMATCH(预设放错槽位)。

以下示例无需素材,可直接运行:

Print(ListAnimations());
const track = AddTrack("video", { name: "Preset motion" });
const id = Insert(track, { type: "text", text: "Hello Cut", start: 0, length: 4,
animation: { in: { preset: "typewriter", duration: 0.6 }, out: { preset: "fade_out" }, loop: { preset: "pulse" } }
});
Update(id, { length: 5 }); // preset effects are regenerated from their source

衔接相邻镜头

先用 ListTransitions() 查效果、默认参数和范围。Join 仅连接同一视觉轨道上首尾相邻的片段,不接受音频或字幕。默认重叠 0.5 秒,必须为正且小于两端片段时长。

Print(ListTransitions());
const track = AddTrack("video", { name: "Joined shots" });
const left = Insert(track, { type: "color", color: "#172554", start: 0, length: 4 });
const right = Insert(track, { type: "color", color: "#7C3AED", start: 4, length: 4 });
const join = Join(left, right, { effect: { kind: "dissolve" } });
Print(GetTimeline().duration); // 7.5 seconds
UpdateJoin(join, { length: 0.8, effect: { kind: "push", direction: "left" } });
Print(GetTimeline().duration, GetTimeline().joins); // 7.2 seconds
Preview([GetItem(right).start + 0.4]);

第一次 Join 后轨道总长为 7.5 秒,调整后为 7.2 秒。右片段和同轨后续片段向左移动,其他轨道保持原位,所以字幕和配乐需要复查同步。每片段最多一个入转场和一个出转场,两者时长之和不能超过片段长度。DeleteJoin 恢复硬切并把后续片段移回。transitionIn、transitionOut 是单片段出入场效果,不是 Join。

变速与倒放

视频和音频的 playback 使用 {kind: "rate", rate, direction, audio},速率 0.25–4;direction 为 forward 或 reverse,audio 为 preservePitch、resample 或 mute。省略时为一倍速正放并保持音调。倒放必须显式 audio: "mute";{kind: "freeze"} 定格 sourceIn 并静音。

Update 修改 playback 默认保留输出时长。SetSpeed 必填 keep:duration 保留输出时长,source 保留消耗的来源区间并改变时长,返回实际 length 与 sourceOut。定格片段需先用 Update 改回 rate。变长冲突会拒绝;显式 {keep: "source", ripple: true} 可移动同轨后续片段。

const track = AddTrack("video", { name: "Speed" });
const id = Insert(track, {
type: "video", media: { libraryId: "<library_id>", assetId: "<video_asset_id>" },
start: 0, length: 4
});
Print(SetSpeed(id, 0.5, { keep: "source" })); // {length: 8, sourceOut: 4}
Print(SetSpeed(id, 2, { keep: "source" })); // {length: 2, sourceOut: 4}
Print(SetSpeed(id, 0.5, { keep: "duration" })); // {length: 2, sourceOut: 1}
Print(GetItem(id));
Preview([0, 1]);

上例需要绑定库中至少四秒的视频,替换两个素材占位符后执行。浏览器预览、服务端 Preview、帧接口与导出都支持倒放。变速音频在编辑提交后异步生成,execute 成功不表示音频已就绪;导出等待音频完成。

遮罩、效果与 LUT

视觉片段可使用形状遮罩(矩形、椭圆或 SVG path),或引用其他视觉轨道的 alpha/luma 通道。轨道遮罩不能自引用或形成循环;每帧最多两个 track matte。遮罩定义错误为 INVALID_MASK,删除仍被引用的轨道为 MASK_IN_USE,并发遮罩超过上限为 TOO_MANY_MASKS。

ListEffects() 会列出分类、参数范围、默认值、成本和边界;内置 lut 是预设色彩查找表。动画效果的 speed 单位为每秒周期数。CPU 导出会因效果增加耗时,用户 .cube LUT 暂不支持。

音频与响度

音频片段 gain 关键帧的值为相对增益 dB。音轨 role 可设为 voice、music 或 sfx。闪避根据 under 轨道上可听片段的占用时间触发,不检测波形音量。exports.create 可传 loudness: {target_lufs, true_peak_dbtp};导出完成后返回目标值与测得的 integrated LUFS、true peak。响度归一化会增加分析和渲染耗时。

文字目录与模板

用 ListTextStyles() 和 ListTextTemplates() 发现预设。文字或轨道样式可传 style.preset,读取时会带出 styleSource。先查看模板槽位,再用 InsertTemplate(track, {template, start, slots}) 插入多层组合。AlignCaptions(track, script) 会按脚本文字重排已有字幕,并返回替换、增加、删除数量。

下一步