每个配方都以两种有意义的方式显示:作为 concat-cli api 的行(粘贴到 .jsonl 文件中,或一次键入一个),以及使用来自 JSON-RPC → Python 客户端 的 Concat 类针对套接字的 Python。
食谱: 从文件夹中粗剪 · 导出并显示进度 · 标题卡 · 每个剪辑的外观 · 框架联系表 · 模板项目 · [风景的垂直剪切]编辑](#7-a-vertical-cut-of-a-landscape-edit) · 撤消、重做、保存 · 检查项目
从 src/ 文件夹运行 CLI:cargo run -p concat-cli -- api < file.jsonl。
1. 从剪辑文件夹中粗剪
导入每个文件并将它们首尾相连地放置在第一个轨道上。
行(显示两个文件;重复该模式):
{"jsonrpc":"2.0","id":1,"method":"project.create","params":{"location":"/edits","name":"Rough cut"}}
{"jsonrpc":"2.0","id":2,"method":"media.import","params":{"path":"/edits/Rough cut","file":"/footage/a.mp4"}}
{"jsonrpc":"2.0","id":3,"method":"media.import","params":{"path":"/edits/Rough cut","file":"/footage/b.mp4"}}
{"jsonrpc":"2.0","id":4,"method":"edit.apply","params":{"path":"/edits/Rough cut","command":{"op":"addClip","mediaId":"m1","trackId":"T1","start":0}}}
{"jsonrpc":"2.0","id":5,"method":"project.get","params":{"path":"/edits/Rough cut"}}从 project.get 回复中读取剪辑的 duration,然后将下一个剪辑放置在该 start 处。 Python 进行簿记:
import glob, os
c = Concat(token=TOKEN)
folder = "/edits/Rough cut"
c.call("project.create", location="/edits", name="Rough cut")
at = 0.0
for file in sorted(glob.glob("/footage/*.mp4")):
view = c.call("media.import", path=folder, file=file)
media_id = view["createdId"]
view = c.call("edit.apply", path=folder,
command={"op": "addClip", "mediaId": media_id, "trackId": "T1", "start": at})
clip = next(cl for cl in view["project"]["timelines"][0]["clips"] if cl["id"] == view["createdId"])
at += clip["duration"]
c.call("project.save", path=folder)[!提示]
media.import对已经在 bin 中的文件是无操作并且不返回createdId。如果重新运行,请通过path在project.media中查找 id。
2.导出并显示进度
线路:
{"jsonrpc":"2.0","id":1,"method":"project.open","params":{"path":"/edits/Reel"}}
{"jsonrpc":"2.0","id":2,"method":"export.run","params":{"path":"/edits/Reel","output":"/edits/reel.mp4","crf":18,"preset":"slow","codec":"h264"}}CLI 持续运行直到作业结束,并打印每个事件。
Python,带有进度线:
def show(event):
p = event["params"]
if event["method"] == "export.progress":
print(f'\r{p["stage"]:5} {p["frame"]}/{p["total"]}', end="")
elif event["method"] == "cutout.progress":
print(f'\rcutout {p["mediaId"]} {"fetching" if p["fetching"] else "analysing"} {p["fraction"]:.0%}', end="")
c = Concat(token=TOKEN, on_event=show)
c.call("project.open", path="/edits/Reel")
started = c.call("export.run", path="/edits/Reel", output="/edits/reel.mp4", crf=18)
end = c.wait_for_job(started["job"])
print()
if end["method"] == "export.done":
print("wrote", end["params"]["output"])
else:
print("failed:", end["params"]["error"]["message"])要从另一个连接停止它:
{
"jsonrpc": "2.0",
"id": 9,
"method": "export.cancel",
"params": { "job": "j1" }
}然后作业以 export.failed 和 error.code = cancelled 结束。
3. 标题卡
镜头上方自己的车道上有一个三秒的标题,然后是下方的三分之一。
{"jsonrpc":"2.0","id":1,"method":"edit.apply","params":{"path":"/edits/Reel","command":{"op":"addTextClip","above":true,"start":0,"duration":3,"style":{"content":"Summer 2026","fontSize":0.14}}}}
{"jsonrpc":"2.0","id":2,"method":"edit.apply","params":{"path":"/edits/Reel","command":{"op":"addTextClip","above":true,"start":5,"duration":4,"offsetY":0.35,"style":{"content":"Ada Lovelace\nEngine designer","fontSize":0.05,"align":"left","background":"#00000099","maxWidth":0.5}}}}仅您设置的样式字段发生变化;其余的是窗口的默认设置(白色、粗体、居中、带阴影)。所有这些都在 类型:TextStyle.
稍后用补丁更改标题。缺席字段保留,null 清除:
{
"op": "updateClip",
"clipId": "c7",
"patch": { "text": { "content": "Summer 2026", "color": "#ffcc00" } }
}4. 查看每个剪辑
找到一个过滤器,然后在每个视频剪辑上设置它。
looks = c.call("catalogue.list", kind="filter")
print([p["id"] for p in looks]) # e.g. concat.warm, concat.film, concat.mono …
view = c.call("project.get", path="/edits/Reel")
timeline = next(t for t in view["project"]["timelines"] if t["id"] == view["project"]["activeTimelineId"])
commands = [
{"op": "updateClip", "clipId": clip["id"],
"patch": {"videoEffects": clip["videoEffects"] + [{"id": "concat.film", "params": {}}]}}
for clip in timeline["clips"] if clip["kind"] == "video"
]
c.call("edit.apply", path="/edits/Reel", command={"op": "batch", "commands": commands})注意事项:
- 补丁中的
videoEffects替换链条,因此请附加到剪辑所具有的内容。 - 空
params表示包的默认值。键和范围位于catalogue.list回复中。 - 1 个
batch表示整个过程的一个撤消步骤。
一次对所有内容都相同,作为一个图层而不是每个剪辑:
{
"op": "addLayerClip",
"start": 0,
"duration": 60,
"effectId": "concat.film",
"name": "Film look"
}5. 框架联系表
每秒一张 PNG,保存到文件夹中。
view = c.call("project.open", path="/edits/Reel")
timeline = next(t for t in view["project"]["timelines"] if t["id"] == view["project"]["activeTimelineId"])
length = max((cl["start"] + cl["duration"] for cl in timeline["clips"]), default=0)
t = 0.0
while t < length:
c.call("preview.frame", path="/edits/Reel", time=t, output=f"/edits/frames/{t:06.1f}.png", width=320, height=180)
t += 1.0或者将一帧直接存入内存:
import base64
pic = c.call("preview.frame", path="/edits/Reel", time=2.5)
png_bytes = base64.b64decode(pic["png"])6. 模板项目
templates = c.call("template.list")
intro = next(t for t in templates if t["name"] == "Intro")
for slot in intro["slots"]:
print(slot["mediaId"], slot["name"], slot["kind"], f'{slot["seconds"]:.1f}s')
fills = [
{"mediaId": intro["slots"][0]["mediaId"], "file": "/footage/logo.png"},
{"mediaId": intro["slots"][1]["mediaId"], "file": "/footage/take1.mp4"},
]
view = c.call("template.instantiate", template=intro["path"], location="/edits", name="My intro", fills=fills)每个槽都必须被填满,并且在制作任何东西之前都要探测每个文件。要从您打开的项目制作模板:
{
"jsonrpc": "2.0",
"id": 1,
"method": "template.save",
"params": { "path": "/edits/Reel", "name": "Reel template" }
}7. 横向编辑的垂直剪切
同一项目中的第二条时间线,1080×1920,具有重新构图的相同剪辑。
{"jsonrpc":"2.0","id":1,"method":"edit.apply","params":{"path":"/edits/Reel","command":{"op":"addTimeline"}}}
{"jsonrpc":"2.0","id":2,"method":"project.setVideo","params":{"path":"/edits/Reel","video":{"width":1080,"height":1920,"rateNum":30,"rateDen":1}}}
{"jsonrpc":"2.0","id":3,"method":"edit.apply","params":{"path":"/edits/Reel","command":{"op":"addClipAtFirstFree","mediaId":"m1","start":0}}}
{"jsonrpc":"2.0","id":4,"method":"edit.apply","params":{"path":"/edits/Reel","command":{"op":"setClipTransform","clipId":"c9","scale":1.8,"offsetX":-0.1}}}
{"jsonrpc":"2.0","id":5,"method":"export.run","params":{"path":"/edits/Reel","output":"/edits/reel-vertical.mp4"}}addTimeline 使新时间线处于活动状态,因此 project.setVideo 和随后的编辑会落在该时间线上。使用 {"op":"selectTimeline","timelineId":"TL1"} 切换回来。上面的剪辑 ID c9 是从添加返回的 createdId。
8.撤消、重做、保存
{"jsonrpc":"2.0","id":1,"method":"edit.apply","params":{"path":"/edits/Reel","command":{"op":"removeClips","clipIds":["c2"],"ripple":true}}}
{"jsonrpc":"2.0","id":2,"method":"edit.undo","params":{"path":"/edits/Reel"}}
{"jsonrpc":"2.0","id":3,"method":"edit.redo","params":{"path":"/edits/Reel"}}
{"jsonrpc":"2.0","id":4,"method":"project.save","params":{"path":"/edits/Reel"}}
{"jsonrpc":"2.0","id":5,"method":"project.close","params":{"path":"/edits/Reel"}}每个回复的 canUndo / canRedo 都会告诉您历史的情况。容忍的空操作(未知的 ID、已保存的值)不会记录撤消步骤。
9. 检查项目而不更改它
cd src
cargo run -p concat-cli -- api '{"method":"project.open","path":"/edits/Reel"}' | python3 -c '
import json, sys
view = json.loads(sys.stdin.readline())["result"]
p = view["project"]
print(view["settings"])
for t in p["timelines"]:
print(t["id"], t["name"], f'{t["video"]["width"]}x{t["video"]["height"]}')
for clip in sorted(t["clips"], key=lambda c: c["start"]):
print(f' {clip["id"]:5} {clip["kind"]:5} {clip["start"]:7.2f} +{clip["duration"]:6.2f} {clip["name"]}')
'开启不改变项目文档;除非您调用 project.save,否则文件夹中不会写入任何内容。唯一的副作用是最近列表,它将项目移到前面。