mirror of
https://github.com/Hommy-master/capcut-mate.git
synced 2026-08-28 23:27:50 +08:00
优化文档。
This commit is contained in:
@@ -156,397 +156,6 @@ curl -X POST https://capcut-mate.jcaigc.cn/openapi/capcut-mate/v1/add_sticker \
|
||||
}'
|
||||
```
|
||||
|
||||
### JavaScript 示例
|
||||
|
||||
```javascript
|
||||
const addSticker = async (draftUrl, stickerConfig) => {
|
||||
const response = await fetch('https://capcut-mate.jcaigc.cn/openapi/capcut-mate/v1/add_sticker', {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify({
|
||||
draft_url: draftUrl,
|
||||
...stickerConfig
|
||||
})
|
||||
});
|
||||
return response.json();
|
||||
};
|
||||
|
||||
// 基本贴纸添加
|
||||
const basicSticker = {
|
||||
sticker_id: "7326810673609018675",
|
||||
start: 0,
|
||||
end: 5000000
|
||||
};
|
||||
|
||||
// 自定义大小和位置的贴纸
|
||||
const customSticker = {
|
||||
sticker_id: "7326810673609018675",
|
||||
start: 3000000,
|
||||
end: 8000000,
|
||||
scale: 1.2,
|
||||
transform_x: 150,
|
||||
transform_y: -50
|
||||
};
|
||||
|
||||
// 小尺寸装饰贴纸
|
||||
const decorationSticker = {
|
||||
sticker_id: "7326810673609018675",
|
||||
start: 10000000,
|
||||
end: 15000000,
|
||||
scale: 0.6,
|
||||
transform_x: -200,
|
||||
transform_y: 200
|
||||
};
|
||||
|
||||
try {
|
||||
const result1 = await addSticker(draftUrl, basicSticker);
|
||||
const result2 = await addSticker(draftUrl, customSticker);
|
||||
const result3 = await addSticker(draftUrl, decorationSticker);
|
||||
|
||||
console.log('贴纸添加成功:', {
|
||||
basic: result1,
|
||||
custom: result2,
|
||||
decoration: result3
|
||||
});
|
||||
} catch (error) {
|
||||
console.error('添加失败:', error);
|
||||
}
|
||||
```
|
||||
|
||||
### 高级JavaScript示例
|
||||
|
||||
```javascript
|
||||
class StickerManager {
|
||||
constructor(baseUrl = 'https://capcut-mate.jcaigc.cn') {
|
||||
this.baseUrl = baseUrl;
|
||||
}
|
||||
|
||||
async addSticker(draftUrl, stickerConfig) {
|
||||
const response = await fetch(`${this.baseUrl}/openapi/capcut-mate/v1/add_sticker`, {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify({
|
||||
draft_url: draftUrl,
|
||||
...stickerConfig
|
||||
})
|
||||
});
|
||||
|
||||
if (!response.ok) {
|
||||
throw new Error(`贴纸添加失败: ${response.statusText}`);
|
||||
}
|
||||
|
||||
return response.json();
|
||||
}
|
||||
|
||||
// 创建贴纸序列
|
||||
createStickerSequence(stickers, intervalDuration = 1000000) {
|
||||
return stickers.map((sticker, index) => ({
|
||||
sticker_id: sticker.id,
|
||||
start: index * intervalDuration,
|
||||
end: (index + 1) * intervalDuration,
|
||||
scale: sticker.scale || 1.0,
|
||||
transform_x: sticker.x || 0,
|
||||
transform_y: sticker.y || 0
|
||||
}));
|
||||
}
|
||||
|
||||
// 创建动画贴纸效果
|
||||
createAnimatedSticker(stickerId, startTime, duration, keyframes) {
|
||||
const configs = [];
|
||||
const segmentDuration = duration / keyframes.length;
|
||||
|
||||
keyframes.forEach((keyframe, index) => {
|
||||
configs.push({
|
||||
sticker_id: stickerId,
|
||||
start: startTime + (index * segmentDuration),
|
||||
end: startTime + ((index + 1) * segmentDuration),
|
||||
scale: keyframe.scale || 1.0,
|
||||
transform_x: keyframe.x || 0,
|
||||
transform_y: keyframe.y || 0
|
||||
});
|
||||
});
|
||||
|
||||
return configs;
|
||||
}
|
||||
|
||||
// 创建随机位置贴纸
|
||||
createRandomPositionStickers(stickerId, count, startTime, duration) {
|
||||
const configs = [];
|
||||
const segmentDuration = duration / count;
|
||||
|
||||
for (let i = 0; i < count; i++) {
|
||||
configs.push({
|
||||
sticker_id: stickerId,
|
||||
start: startTime + (i * segmentDuration),
|
||||
end: startTime + ((i + 1) * segmentDuration),
|
||||
scale: 0.5 + Math.random() * 1.0, // 0.5-1.5倍缩放
|
||||
transform_x: (Math.random() - 0.5) * 400, // -200到200像素
|
||||
transform_y: (Math.random() - 0.5) * 400 // -200到200像素
|
||||
});
|
||||
}
|
||||
|
||||
return configs;
|
||||
}
|
||||
|
||||
// 批量添加贴纸
|
||||
async batchAddStickers(draftUrl, stickerConfigs) {
|
||||
const results = [];
|
||||
|
||||
for (const config of stickerConfigs) {
|
||||
try {
|
||||
const result = await this.addSticker(draftUrl, config);
|
||||
results.push(result);
|
||||
|
||||
// 添加延迟避免请求过快
|
||||
await new Promise(resolve => setTimeout(resolve, 100));
|
||||
} catch (error) {
|
||||
console.error('贴纸添加失败:', error);
|
||||
results.push({ error: error.message });
|
||||
}
|
||||
}
|
||||
|
||||
return results;
|
||||
}
|
||||
|
||||
// 创建贴纸模板
|
||||
createStickerTemplate(templateType, stickerId, baseTime, baseDuration) {
|
||||
switch (templateType) {
|
||||
case 'corner-decoration':
|
||||
return [
|
||||
{
|
||||
sticker_id: stickerId,
|
||||
start: baseTime,
|
||||
end: baseTime + baseDuration,
|
||||
scale: 0.3,
|
||||
transform_x: -400,
|
||||
transform_y: -300
|
||||
},
|
||||
{
|
||||
sticker_id: stickerId,
|
||||
start: baseTime,
|
||||
end: baseTime + baseDuration,
|
||||
scale: 0.3,
|
||||
transform_x: 400,
|
||||
transform_y: -300
|
||||
},
|
||||
{
|
||||
sticker_id: stickerId,
|
||||
start: baseTime,
|
||||
end: baseTime + baseDuration,
|
||||
scale: 0.3,
|
||||
transform_x: -400,
|
||||
transform_y: 300
|
||||
},
|
||||
{
|
||||
sticker_id: stickerId,
|
||||
start: baseTime,
|
||||
end: baseTime + baseDuration,
|
||||
scale: 0.3,
|
||||
transform_x: 400,
|
||||
transform_y: 300
|
||||
}
|
||||
];
|
||||
|
||||
case 'center-highlight':
|
||||
return [{
|
||||
sticker_id: stickerId,
|
||||
start: baseTime,
|
||||
end: baseTime + baseDuration,
|
||||
scale: 2.0,
|
||||
transform_x: 0,
|
||||
transform_y: 0
|
||||
}];
|
||||
|
||||
case 'floating-animation':
|
||||
return this.createAnimatedSticker(stickerId, baseTime, baseDuration, [
|
||||
{ x: -100, y: -100, scale: 0.8 },
|
||||
{ x: 100, y: -100, scale: 1.0 },
|
||||
{ x: 100, y: 100, scale: 1.2 },
|
||||
{ x: -100, y: 100, scale: 1.0 },
|
||||
{ x: -100, y: -100, scale: 0.8 }
|
||||
]);
|
||||
|
||||
default:
|
||||
return [{
|
||||
sticker_id: stickerId,
|
||||
start: baseTime,
|
||||
end: baseTime + baseDuration,
|
||||
scale: 1.0,
|
||||
transform_x: 0,
|
||||
transform_y: 0
|
||||
}];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 使用示例
|
||||
const stickerManager = new StickerManager();
|
||||
|
||||
// 创建贴纸序列
|
||||
const stickerSequence = stickerManager.createStickerSequence([
|
||||
{ id: "7326810673609018675", scale: 1.0, x: 0, y: 0 },
|
||||
{ id: "7326810673609018676", scale: 1.2, x: 100, y: -50 },
|
||||
{ id: "7326810673609018677", scale: 0.8, x: -100, y: 50 }
|
||||
], 2000000);
|
||||
|
||||
// 创建动画贴纸
|
||||
const animatedSticker = stickerManager.createAnimatedSticker(
|
||||
"7326810673609018675",
|
||||
5000000,
|
||||
3000000,
|
||||
[
|
||||
{ x: -200, y: 0, scale: 0.5 },
|
||||
{ x: 0, y: 0, scale: 1.0 },
|
||||
{ x: 200, y: 0, scale: 1.5 },
|
||||
{ x: 0, y: 0, scale: 1.0 }
|
||||
]
|
||||
);
|
||||
|
||||
// 创建模板贴纸
|
||||
const cornerDecorations = stickerManager.createStickerTemplate(
|
||||
'corner-decoration',
|
||||
"7326810673609018675",
|
||||
0,
|
||||
10000000
|
||||
);
|
||||
|
||||
// 批量添加
|
||||
const allConfigs = [...stickerSequence, ...animatedSticker, ...cornerDecorations];
|
||||
await stickerManager.batchAddStickers(draftUrl, allConfigs);
|
||||
```
|
||||
|
||||
### Python 示例
|
||||
|
||||
```python
|
||||
import requests
|
||||
import time
|
||||
import random
|
||||
from typing import List, Dict
|
||||
|
||||
class StickerProcessor:
|
||||
def __init__(self, base_url: str = "https://capcut-mate.jcaigc.cn"):
|
||||
self.base_url = base_url
|
||||
|
||||
def add_sticker(self, draft_url: str, sticker_config: Dict) -> Dict:
|
||||
response = requests.post(
|
||||
f'{self.base_url}/openapi/capcut-mate/v1/add_sticker',
|
||||
headers={'Content-Type': 'application/json'},
|
||||
json={
|
||||
"draft_url": draft_url,
|
||||
**sticker_config
|
||||
}
|
||||
)
|
||||
response.raise_for_status()
|
||||
return response.json()
|
||||
```
|
||||
def create_sticker_sequence(self, stickers: List[Dict], interval_duration: int = 1000000) -> List[Dict]:
|
||||
"""创建贴纸序列"""
|
||||
configs = []
|
||||
for i, sticker in enumerate(stickers):
|
||||
configs.append({
|
||||
"sticker_id": sticker["id"],
|
||||
"start": i * interval_duration,
|
||||
"end": (i + 1) * interval_duration,
|
||||
"scale": sticker.get("scale", 1.0),
|
||||
"transform_x": sticker.get("x", 0),
|
||||
"transform_y": sticker.get("y", 0)
|
||||
})
|
||||
return configs
|
||||
|
||||
def create_animated_sticker(self, sticker_id: str, start_time: int,
|
||||
duration: int, keyframes: List[Dict]) -> List[Dict]:
|
||||
"""创建动画贴纸效果"""
|
||||
configs = []
|
||||
segment_duration = duration // len(keyframes)
|
||||
|
||||
for i, keyframe in enumerate(keyframes):
|
||||
configs.append({
|
||||
"sticker_id": sticker_id,
|
||||
"start": start_time + (i * segment_duration),
|
||||
"end": start_time + ((i + 1) * segment_duration),
|
||||
"scale": keyframe.get("scale", 1.0),
|
||||
"transform_x": keyframe.get("x", 0),
|
||||
"transform_y": keyframe.get("y", 0)
|
||||
})
|
||||
|
||||
return configs
|
||||
|
||||
def create_random_stickers(self, sticker_id: str, count: int,
|
||||
start_time: int, duration: int) -> List[Dict]:
|
||||
"""创建随机位置贴纸"""
|
||||
configs = []
|
||||
segment_duration = duration // count
|
||||
|
||||
for i in range(count):
|
||||
configs.append({
|
||||
"sticker_id": sticker_id,
|
||||
"start": start_time + (i * segment_duration),
|
||||
"end": start_time + ((i + 1) * segment_duration),
|
||||
"scale": 0.5 + random.random() * 1.0, # 0.5-1.5倍缩放
|
||||
"transform_x": int((random.random() - 0.5) * 400), # -200到200像素
|
||||
"transform_y": int((random.random() - 0.5) * 400) # -200到200像素
|
||||
})
|
||||
|
||||
return configs
|
||||
|
||||
def batch_add_stickers(self, draft_url: str, sticker_configs: List[Dict]) -> List[Dict]:
|
||||
"""批量添加贴纸"""
|
||||
results = []
|
||||
|
||||
for config in sticker_configs:
|
||||
try:
|
||||
result = self.add_sticker(draft_url, config)
|
||||
results.append(result)
|
||||
time.sleep(0.1) # 避免请求过快
|
||||
except Exception as e:
|
||||
print(f"贴纸添加失败: {e}")
|
||||
results.append({"error": str(e)})
|
||||
|
||||
return results
|
||||
|
||||
# 使用示例
|
||||
processor = StickerProcessor()
|
||||
|
||||
# 基本贴纸
|
||||
basic_config = {
|
||||
"sticker_id": "7326810673609018675",
|
||||
"start": 0,
|
||||
"end": 5000000,
|
||||
"scale": 1.0
|
||||
}
|
||||
|
||||
# 贴纸序列
|
||||
sequence_configs = processor.create_sticker_sequence([
|
||||
{"id": "7326810673609018675", "scale": 1.0, "x": 0, "y": 0},
|
||||
{"id": "7326810673609018676", "scale": 1.2, "x": 100, "y": -50},
|
||||
{"id": "7326810673609018677", "scale": 0.8, "x": -100, "y": 50}
|
||||
], interval_duration=2000000)
|
||||
|
||||
# 动画贴纸
|
||||
animated_configs = processor.create_animated_sticker(
|
||||
"7326810673609018675",
|
||||
start_time=10000000,
|
||||
duration=4000000,
|
||||
keyframes=[
|
||||
{"x": -200, "y": 0, "scale": 0.5},
|
||||
{"x": 0, "y": 0, "scale": 1.0},
|
||||
{"x": 200, "y": 0, "scale": 1.5},
|
||||
{"x": 0, "y": 0, "scale": 1.0}
|
||||
]
|
||||
)
|
||||
|
||||
# 批量添加
|
||||
draft_url = "YOUR_DRAFT_URL"
|
||||
all_configs = [basic_config] + sequence_configs + animated_configs
|
||||
results = processor.batch_add_stickers(draft_url, all_configs)
|
||||
|
||||
for i, result in enumerate(results):
|
||||
if "error" not in result:
|
||||
print(f"贴纸 {i+1} 添加成功: {result['segment_id']}")
|
||||
else:
|
||||
print(f"贴纸 {i+1} 添加失败: {result['error']}")
|
||||
```
|
||||
|
||||
## 错误码说明
|
||||
|
||||
| 错误码 | 错误信息 | 说明 | 解决方案 |
|
||||
|
||||
Reference in New Issue
Block a user